using System; // we’ll be using classes, structs, interfaces from pre-defined
using System.Linq; // System and System.Linq namespaces (ns), into our this code
namespace HelloWorld { // we’re packaging our this code inside HelloWorld user-defined ns
class Program {
string fieldName1 = "Var declared outside method inside class is called field";
string fieldName2;
Program() {
fieldName2 = "Fields and class methods must be initialised inside constructors";
}
static void StaticMethod(string param1, int param2 = 0) {
Console.WriteLine(param1 + "Method" + param2);
}
public void PublicMethod() {
Console.WriteLine("Access Public Methods via obj; Static Methods sans obj.");
}
static void Main(string[] args) {
double myNum1; myNum1 = 5.99D; // Declaration & Assignment (D&A) separate
double myNum2 = 5.99D; // Declaration & Assignment (D&A) clubbed
const double myNum3 = 3.14; // Constants’ D&A must be clubbed
float f1 = 35e3F; double d1 = 12E4D; // Scientific. E = power of 10
int x, y, z; x = y = z = 50;
string firstName = "John ";
string lastName = Console.ReadLine();
string fullName = firstName + lastName;
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("This won’t end with any implicit newline.");
Console.WriteLine("This will have implicit newline." + myNum1);
// Single Line Comment
/* Multi
Line Comment */
Console.WriteLine(Math.Max(5, 10));
Console.WriteLine(Math.Min(5, 10));
// Console.WriteLine(Math.MaxMagnitude(5, 10));
// Console.WriteLine(Math.MinMagnitude(5, 10));
Console.WriteLine(Math.Abs(-4.7));
Console.WriteLine(Math.Pow(-4.7, 3));
Console.WriteLine(Math.Exp(-4.7));
Console.WriteLine(Math.Sqrt(64));
Console.WriteLine(Math.Cbrt(64));
Console.WriteLine(Math.Round(9.45));
Console.WriteLine(Math.Floor(9.99));
Console.WriteLine(Math.Ceiling(9.01));
Console.WriteLine(Math.Truncate(34.3));
Console.WriteLine(Math.Sign(34.3));
Console.WriteLine(Math.Log(34.3, 19));
Console.WriteLine(Math.Log(34.3));
Console.WriteLine(Math.Log10(34.3));
// Console.WriteLine(Math.Log2(34.3));
// Console.WriteLine(Math.ILog(34.3));
Console.WriteLine(Math.Sin(34.3));
Console.WriteLine(Math.Sinh(34.3));
Console.WriteLine(Math.Asin(34.3));
Console.WriteLine(Math.Asinh(34.3));
Console.WriteLine(Math.Acos(34.3));
Console.WriteLine(Math.Acosh(34.3));
Console.WriteLine(Math.Tan(34.3));
Console.WriteLine(Math.Tanh(34.3));
Console.WriteLine(Math.Atan(34.3));
Console.WriteLine(Math.Atanh(34.3));
Console.WriteLine(Math.Atan2(34.3, 43.5)); // returns angle whose tangent = quotient of 2 #’s
// Console.WriteLine(Math.SinCos(34.3)); // returns sine and cosine of angle
Console.WriteLine(Math.BigMul(34, 17));
// Console.WriteLine(Math.BitIncrement(34.3));
// Console.WriteLine(Math.BitDecrement(34.3));
Console.WriteLine(Math.Clamp(34.3, 17, 61));
// Console.WriteLine(Math.CopySign(34.3, -7));
Console.WriteLine(Math.DivRem(34, 17, out var a)); // no decimal
// Console.WriteLine(Math.FusedMultiplyAdd(34.3, 14, 56.3));
Console.WriteLine(Math.IEEERemainder(34.3, 15));
// Console.WriteLine(Math.ReciprocalEstimate(34.3));
// Console.WriteLine(Math.ReciprocalSqrtEstimate(34.3));
// Console.WriteLine(Math.ScaleB(34.3));
Console.Write(Math.PI);
string txt = "ABCDEFGHIJKLMNOpqrstuvEwxyz";
Console.WriteLine(txt[4]);
Console.WriteLine(txt.Length);
Console.WriteLine(txt.ToUpper());
Console.WriteLine(txt.ToLower());
Console.WriteLine(txt.Clone());
Console.WriteLine(txt.Trim());
// Console.WriteLine(txt.Format());
Console.WriteLine(txt.ToCharArray());
Console.WriteLine(txt.GetType());
Console.WriteLine(txt.GetTypeCode());
Console.WriteLine(txt.GetHashCode());
Console.WriteLine(txt.IsNormalized());
Console.WriteLine(txt.StartsWith("A"));
Console.WriteLine(txt.EndsWith("z"));
Console.WriteLine(txt.Contains("BCD"));
Console.WriteLine(txt.Insert(3, "Hey"));
Console.WriteLine(txt.Remove(7));
Console.WriteLine(txt.Replace('q', 'E'));
Console.WriteLine(txt.Substring(4,9));
Console.WriteLine(txt.PadLeft(4));
Console.WriteLine(txt.PadRight(6));
Console.WriteLine(txt.Split('E'));
// Console.WriteLine(txt.Join(fullName));
// Console.WriteLine(Concat(txt, txt));
Console.WriteLine(txt.CompareTo(firstName));
// Console.WriteLine(txt.Compare(txt, firstName));
Console.WriteLine(txt.Equals(lastName));
Console.WriteLine(txt.IndexOf('E'));
Console.WriteLine(txt.LastIndexOf('E'));
string name = $"My full name is: {txt} {txt}"; //# $ is used for interpolation
Console.WriteLine(name);
int[] arr0;
//arr0 = {6,5,4,3,2,1}; //Throws error
arr0 = new int[] {6,5,4,3,2,1}; //No error
Array.Sort(arr0);
int[] arr1 = {1,2,3,4,5,6};
// int arr2 = new int[6];
// int arr3 = new int[6] {1,2,3,4,5,6};
// int arr4 = new int[] {1,2,3,4,5,6};
int[,] arr5 = {{1,2,3},{4,5,6}};
int[,,] arr6 = {{{1,1},{2,2}},{{3,3},{4,4}},{{5,5},{6,6}}};
Console.WriteLine(arr1.Min());
// Console.Write(arr2.Max());
// Console.Write(arr3.Sum());
// Console.Write(arr4.Rank);
// Console.Write(arr4.Length);
Console.WriteLine(arr5.GetLength(0));
Console.WriteLine(arr6.GetLength(2));
Console.WriteLine(arr5.GetLowerBound(0));
Program myObj = new Program(); // Program() of new Program() = call to constructor
Console.WriteLine(myObj.fieldName1); // access class members (fields & class methods)
myObj.PublicMethod(); // using dot convention
StaticMethod("Arg1", 1); // compulsory param always before optional param
// StaticMethod(param1 = "Arg2"); // optional param2 = 0 default value; named param1
}
}
}
Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C# and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
| Data Type | Description | Range | size |
|---|---|---|---|
| int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
| double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
| float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
| char | to store single characters | - | 2 bytes |
| string | to stores text | - | 2 bytes per character |
| bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}