using System;
namespace BasicApp
{
class App
{
static void Main()
{
Console.WriteLine("Hi!");
}
}
}
static void Main()
{
// code ...
}
static int Main()
{
// code ...
return 0; // returns integer
}
static int Main(string[] args)
{
var t = args[0]; // process command-line arguments
// code ...
return 42;
}
int x = 5;
int y = x - 2; // compiler allows subtraction
bool foo = true;
int z = x + foo; // allowed?
// Explicit type
int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Implicit type
var limit = 100; // compiler infers integer
int x = 0;
float x = 3.0f; // redeclaration
int y = 2.3f; // incompatible type, requires cast
int z = (int) 4.4f; // ok, with explicit cast!
float w = 1; // auto type conversion
int x = 3;
object o = x; // box x into o, move from stack to heap
object o = 123;
int x = (int) o; // Explicit unboxing
// Variable declaration
double d;
// Declaration with assignment
double f = 2;
// Constant declaration statement
const double pi = 3.14159;
// Expression statement stores value in a
a = 3.14 * (radius * radius);
// what happens here?
b * 3;
// Method invocation
System.Console.WriteLine("Hi!");
// Expression statement create new object
List<string> names = new List<string>();
bool cond = false;
if(cond){
...
} else {
....
}
// Else statement is optional
if(cond) { ... }
// If statements are nestable
if(cond1) {
if(cond2) {
...
} else { ... }
} else { if(cond3) { ... } }
int curCase = 4; switch(curCase) { case 1: System.Console.WriteLine("Case 1!"); break; case 2: System.Console.WriteLine("Case 2!"); break; case 42: System.Console.WriteLine("Case 42!"); break;
default: System.Console.WriteLine("Any other case..."); break; }
// For loop
for(var i = 0; i < 10; i++) { ... }
// Foreach loop
int[] digits = { 1, 2, 3, 4, 5, 6 }
foreach(var i in digits) { ... }
// While loop
int i = 0;
while(i < 6) { ... ; i++; }
// Do-while loop
i = 0;
do { ...; i++; }
while(i < 6);
(x > 0) && (x < 25) // evaluates to what?
// Expression as method param
System.Convert.ToInt32("35");
int y = 0;
// unary operator (++)
y++;
// unary operator (new)
var t = new A();
// binary operator (+,-,*,/)
int z = 2 + y;
// conditional operator (:?) takes three operands
var b = z > 0 ? true : false;
// Array declaration of size 4
int[] array1 = new int[4];
// Set array elements
int[] array2 = new int[] { 1, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 5, 7, 9 };
// 2 dim array
int[,] array4 = new int[3,3];
// 2 dim array with init
int[,] array5 = new int[] { { 2, 4, 5}, { 1, 3, 6} };
// Jagged array
int[][] array6 = new int[6][];
array6[0] = new int[4] { 1, 2, 3, 4 };
Thank you for your attention!