dynamic keyworddelete
keyword
$ md HelloCS
$ cd HelloCS
$ dotnet new console
$ dotnet run
using System;
namespace HelloCS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Main
Methodusing
directive brings other namespaces’ types into scope
int i;
float x, y;
string name = "Mohammad";
byte b = 30;
int i = 12000 + b;
i = 10 / 3;
short s = 762190; // Error: Constant value '762190'
// cannot be converted to a 'short'
double pi = 3.1415926535897932384; // 3.14159265358979
float fpi = (float)pi; // 3.141593
double x = 10 / 3; // 3!
int million = 1_000_000; // underscore can be
// inserted anywhere (C# 7)
int x = 0x7f; // 0x prefix: hexadecimal literal
byte b = 0b10000000; // 0b prefix: binary literal (C# 7)
byte b2 = 0b1000_1000;
double d = 1.5;
double million2 = 1e06;
float f = 1.2f;
decimal dec = -1.23M; // Will not compile without the M suffix.
int x = 12345; // int is a 32-bit integer
long y = x; // Implicit conversion to 64-bit integral type
short z = (short)x; // Explicit conversion to 16-bit integral type
int i = 10;
decimal d = i;
int j = (int)d;
i = (int)1.7; // 1 - fractional portion is truncated
float f = 1.1f;
d = (decimal)f;
double d2 = (double)d;
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder after division |
++ | Increment |
-- | Decrement |
int x = 0, y = 0;
Console.WriteLine (x++); // Outputs 0; x is now 1
Console.WriteLine (++y); // Outputs 1; y is now 1
float temperature = 20;
bool isHot = temperature > 35;
bool rainy = true;
bool sunny = false;
bool windy = true;
bool useUmbrella = !windy && (rainy || sunny);
char newLine = '\n';
char backSlash = '\\'
string a = "Here's a tab:\t";
string a1 = "\\\\server\\fileshare\\helloworld.cs";
// Verbatim string literal
string a2 = @"\\server\fileshare\helloworld.cs";
int l = s2.Length;
int x = 4;
Console.Write ($"A square has {x} sides");
[]
)
C# Type | .NET Framework Type | C# Type | .NET Framework Type |
---|---|---|---|
bool | System.Boolean | unit | System.UInt32 |
byte | System.Byte | long | System.Int64 |
sbyte | System.SByte | ulong | System.UInt64 |
char | System.Char | object | System.Object |
decimal | System.Decimal | short | System.Int16 |
double | System.Double | ushort | System.UInt16 |
float | System.Single | string | System.String |
int | System.Int32 |
case
labels are constantsdefault
label is optionalgoto case
/ goto default
Multiple variables under the same name
new
is used to create an arrayLength
propertyLength
property gives the total length.
GetLength(<dimension>)
char[] vowels; // Declare an array of characters
vowels = new char[5]; // Initialize with an array of 5 characters
// Fill with values
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
char[] vowels;
vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
or
char[] vowels;
vowels = new [] { 'a', 'e', 'i', 'o', 'u' };
or
char[] vowels = {'a','e','i','o','u'};
int[,] matrix =
{
{0,1,2},
{3,4,5},
{6,7,8}
};
int[][] matrix = new int[][]
{
new int[] {0,1,2},
new int[] {3,4,5},
new int[] {6,7,8,9}
};
void
if no return value
static float CalculateWeight(float mass, float gravity)
{
return mass * gravity;
}
static float CalculateWeight(float mass)
{
return CalculateWeight(mass, 9.807f);
}
static void Main()
{
float mass = 10;
float weightOnEarth = CalculateWeight(mass);
float weightOnMars = CalculateWeight(mass, 3.711f);
}
ref
modifier: "pass by reference" - in/out parameterout
modifier: output parameter (also "pass by reference")
static void Main()
{
float mass = 10;
float weightOnEarth = CalculateWeight(mass);
float weightOnMars = CalculateWeight(mass, 3.711f);
}
static float CalculateWeight(float mass, float gravity = 9.807f)
{
return mass * gravity;
}
void Foo(int x, int y)
{
Console.WriteLine(x + ", " + y);
}
void Test()
{
Foo(x: 1, y: 2); // 1, 2
Foo(y: 2, x: 1); // 1, 2
}
ref
Modifier
static void Foo(ref int p)
{
p = p + 1; // Increment p by 1
Console.WriteLine(p); // Write p to screen
}
static void Main()
{
int x = 8;
Foo(ref x); // Ask Foo to deal directly with x
Console.WriteLine(x); // x is now 9
}
ref
Modifier
static void Swap(ref string a, ref string b)
{
string temp = a;
a = b;
b = temp;
}
out
Modifierref
modifier, but:
Console.WriteLine("Please enter an integer:");
int number;
while(true)
{
string s = Console.ReadLine();
if(int.TryParse(s, out number))
break;
Console.WriteLine("Not a valid number! Try again:");
}
if(double.TryParse(str, out double d))
{
// do something
}
static bool IsDateValid(string dateString)
{
return DateTime.TryParse(dateString, out _);
}
var
keyword
var i = 10;
i = 1.1; // Error: Cannot implicitly convert type 'double' to 'int'.
var text = Console.ReadLine();
var words = text.Split();
var n; // Error!
var x = 1, y = 10; // Error!
params
Modifier
static int Sum(params int[] ints)
{
int sum = 0;
for (int i = 0; i < ints.Length; i++)
sum += ints[i]; // Increase sum by ints[i]
return sum;
}
static void Main()
{
int total = Sum(1, 2, 3, 4);
Console.WriteLine(total); // 10
}
static void Main(string[] args)
{
int a = 1;
{
int x = 10, y = 5;
Console.WriteLine(x + " " + y);
int a; // Error. Already defined in outer scope
}
{
int x = 30; // Valid.
Console.WriteLine(a); // Valid.
Console.WriteLine(x); // Valid.
Console.WriteLine(y); // Error. Not accessible
}
}
class <class_name>
{
// state and behavior
}
new
operatorpublic
: everyone from outside can see and use the memberprivate
: no access from outsideprotected
: only access from subclassesinternal
: only access from inside of the assemblyprotected internal
: access from
subclasses and others from inside of the assemblyprivate protected
(C# 7.2) :
access from subclasses within the assemblyobject
typeis
keyword is used to check that if on object is of some specified type. It shows that
if the cast is possible or notas
keyword check the possibility of a cast, and if possible, performs the cast,
otherwise returns null