C#
C#
C#
Basics
• Write & WriteLine
// Write methode can receive any kind of data string ,int , bool , Date
namespace all_projects
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello World"); //Write string on a console window
Console.WriteLine(1500); // write int on a console window and then go to new line.
}
}
}
• Data Type
Data Types Ranges
• Data Type Ranges
■ You can know the Min and Max value of each of the data type by using the below
methods
}
}
}
• Constant Decleration
■ // Constant is used to represent something which is canot change , for example the number of days per
week is 7 and canot be 8 or 6 and also the value of PI is 3.14 and so on ...
■ class Program
■ {
■ static void Main(string[] args)
■ {
■ const int week = 7;
■ const int Ydays = 365;
■ Ydays = 6; // if you try to change the value of an constant you will have an error
■ Console.WriteLine(week);
■ Console.WriteLine(Ydays);
■ }
■ }
■ }
• Hexadecimal Unicode representation
char cc = 'A';
char bb = '\u0041'; //you can use the hex unicode to represent any character you need & This also
will print the (A) character
Console.WriteLine(cc);
Console.WriteLine(bb);
Console.WriteLine("\u0041\u0048\u004D\u0045\u0044");
// This will print the name (AHMED)
Hexadecimal Unicode representation (cont.)
■ You can get the Hex unicode from below website
• Concatenate String دمج النصوص
int x = 10; ;
Console.WriteLine("The number of the student is:" + x); // we concatenate a variable of
int type with a string
string strname1 = "Ahmed";
string strname2 = "hamed";
bool bb = true;
char cc = '%';
Console.WriteLine(strname1 + strname2 + bb + cc+x);
// we concatenate a string with boolean with Char with INT
• Assignment operators
int y = 1; // this give the variable x the value 1
Console.WriteLine("The firist value of Y : "+y);
y += 5; // this increase the value of y by 5 ( it is same as y =y+5 so y = 1+5 =6
Console.WriteLine("Add result : " + y);
y -= 2; // this decrease the value of y by 2 y= y-2 y =6-2 = 4
Console.WriteLine("subtract result : " + y);
y *= 2; // this multiply the value of y by 2 y=y*2 y =4*2 = 8
Console.WriteLine("Multiply result : " + y);
y /= 2; // this divide the value of y by 2 y=y*2 y =8/2 = 4
Console.WriteLine("Div result : " + y);
y %= 3; // this take the modulas the value of y by 3 y=y%3 y = 4%3 = 1
Console.WriteLine("modulas result : "+y);
string x = "ahmed ";
x += "Hamed";
Console.WriteLine(x);
• Convert from string to Int and from int to string
//Convert STRING TO INT
string num1 = "500";
int num2 = 500;
int result;
result = Convert.ToInt32(num1) + num2; //this is the firist way to convert string to INT.
Console.WriteLine("The result is : " + result);
result = int.Parse(num1) + num2; //this is the second way to convert string to INT.
Console.WriteLine("The result is : " + result);
//Convert INT to STRING
string str="";
int num=5000;
str = num.ToString(); // This is the 1st way to convert INT to String
Console.WriteLine("This is the 1st way to convert INT to String " +str);
str = Convert.ToString(num); // This is the 2nd way to convert INT to String
Console.WriteLine("This is the 2nd way to convert INT to String "+str);
str = num + “ "; // This is the 3rd way to convert INT to String just make concatenation with any string even if it is blank
Console.WriteLine("This is the 3rd way to convert INT to String "+str);
• Type Casting
double dd = 15.58;
int x = (int)dd; // This the 1st way to convert
between numerical var ( Double to INT)
Console.WriteLine("This the 1st way to convert
between Double to INT " + x);
float y = 55;
int z = Convert.ToInt32(y); // This the 2nd way to
convert between numerical var ( float to INT)
Console.WriteLine("This the 1st way to convert
between Float to INT " + x);
object obj = "AHMED";
string str = (string)obj;
str = Convert.ToString(obj);
Console.WriteLine("string str value is " + str);
• Char Unicode using Cast
Console.WriteLine("The Decimal value of Char 'a' : "+(int)'a'); // this
casting will get the Decimal UNICODE for Char 'a'
Console.WriteLine("The Char the represent the DEC value of 97
is " +"'"+ (char)97 +"'");
// this casting will get the Char the represent the DEcimal
value 97
Console.WriteLine("The hex value of Char 'a' : " +
string.Format("{0:x}",(int)'a'));
//this get the hexadecimal value for Char 'a' , firist make casting from
Char to int to get the decimal value
//and then convert the Decimal to hexadecimal using string .format
• Pre-Fix & Post-Fix ( x++ ,x-- ,++x ,--x)
■ int y = 5;
■ int x = y++; // Post fix is calculated after assignment operator so
the value of x=5 Not 6
■ //and after the assignment operator '=' is done the value of Y
increase by 1 so the value of Y is 6
■ Console.WriteLine("The value of Y is :" +y);
■ Console.WriteLine("The value of x is :" + x);
■ int z = 5;
■ int w = ++z; // Pre fix is done before assignment operator so the
value of w=6 Not 5
■ Console.WriteLine("The value of Y is :" + z);
■ Console.WriteLine("The value of x is :" + w);
• Logical Operator ( AND && – OR || – NOT ! )
■ bool AND1 = true && true;
■ bool AND2 = true && false;
■ bool AND3 = false && false;
■ bool AND4 = false && true;
■ Console.WriteLine("AND1 = "+ AND1);
■ Console.WriteLine("AND2 = " + AND2);
■ Console.WriteLine("AND3 = " + AND3);
■ Console.WriteLine("AND4 = " + AND4);
■ Console.WriteLine("#################");
■ Console.WriteLine(string.Format("AND1 :{0} \nAND2 :{1} \nAND3 :{2} \nAND4 :{3}", AND1,
AND2, AND3, AND4));
■ // This is another way to make the print on one line using string.format
■ // \n is used to make new line
■ //{0},{1},{2},{3} is used to assign each variable after the ( , ) in In ascending order
• Comparison Operator ( > , < , >= ,<= , == ,!=)
■ int x = 10;
■ int y = 5;
■ bool z = x==y; // This assign False to the bool Z because X is Not equal to Y
■ Console.WriteLine("Check if X is equal to Y : " + z); // This print False
■ z = x > y; // This assign True to the bool Z because X is greater than to Y
■ Console.WriteLine("Check if X is greater than to Y : " + z); // This print
True
■ z = x != y; // This assign True to the bool Z because X is not equal to Y
■ Console.WriteLine("Check if X is Not equal to Y : " + z); // This print True
• Read from User(ReadLine)
■ Console.Write("Please Enter Your Name :");
■ String Name = Console.ReadLine();
■ Console.WriteLine("welcome "+Name);
■ //This receive the user Name and then print statement to welcome him
■ // console.Readline this method return String value
■ //so if you want to receive a number you will need to convert the string into number
Type(int ,double,...)
■ Console.Write("Please enter the firist number : ");
■ int x = int.Parse(Console.ReadLine()); // This will receive a number from the user
■ Console.Write("Please enter the second number : ");
■ int y = int.Parse(Console.ReadLine()); // This will receive another number from the
user
■ int result = x + y; // this add the 2 number received from the users
■ Console.WriteLine("The result of the addition is : " + result);
• IF Statement
■ // Each IF Statement , it check the condition , and if it Met the Condition ,
■ //it will execute the programming statements on the block
■ Console.Write("Please Enter the 1st Number :");
■ int x = int.Parse(Console.ReadLine());
■ Console.Write("Please Enter the 2nd Number :");
■ int y = int.Parse(Console.ReadLine());
■ if (x>y)
■ {
■ Console.WriteLine("Number 1 is Greater than Number 2 :");
■ }
■ if (x < y)
■ {
■ Console.WriteLine("Number 1 is Less than Number 2 :");
■ }
■ if (x == y) Console.WriteLine("Number 1 is Equal to Number 2 :");
■ // Note that when you write only one statement on the Block , So No need to write the block
■ if (x != y) Console.WriteLine("Number 1 is not equal to Number 2 :");
■ // Note that there is 2 IF statement can met the condition at the same time , so both of them will be
excuted
• IF – ELSE Statement
■ Console.Write("Please Enter the 1st Number :");
■ int x = int.Parse(Console.ReadLine());
■ Console.Write("Please Enter the 2nd Number :");
■ int y = int.Parse(Console.ReadLine());
■ if (x > y)
■ {
■ Console.WriteLine("Number 1 is Greater than Number 2 ");
■ }
■ if (x < y)
■ {
■ Console.WriteLine("Number 1 is Less than Number 2 "); The 1st and 2nd IF statement
■ } didnot Met the condition , So
■ else else Statement excuted
■ {
■ Console.WriteLine("Number 1 is Equal to Number 2 ");
■ }
■ // Check Each If firist , if one of its condition Met , It will be excuted and else will be ignored
■ //But if no one of the IF conditions Met , so Else statement will be excuted directly
• ELSE IF Statement The user entry Met 2 IF statements , but the
1st one that Met the condition will be
■ Console.Write("Please Enter the 1st Number :");
excuted and others will be ignored
■ int x = int.Parse(Console.ReadLine());
■ Console.Write("Please Enter the 2nd Number :");
You can add the Else statement at the
■ int y = int.Parse(Console.ReadLine());
end of your code , so if no IF statement
■ if (x > y)
MET the condition , the ELSE statement
■ {
will be excuted
■ Console.WriteLine("Number 1 is Greater than Number 2 ");
■ }
■ else if (x < y)
■ {
■ Console.WriteLine("Number 1 is Less than Number 2 ");
■ }
■ else if (x == y) Console.WriteLine("Number 1 is Equal to Number 2 ");
■ // Note that when you write only one statement on the Block , So No need to write the block
■ else if (x != y) Console.WriteLine("Number 1 is not equal to Number 2 ");
■ // Note that there is 2 IF statement can met the condition at the same time
■ //But because you used the ELSE IF statement , so the 1st one Met the condition , will be excuted and ignore the
others
■ else Console.WriteLine("your conditions didnoe MET user entry !!");
• IF Example to check the Data Type of
user entry
■ int y;
■ double z;
■ string r = Console.ReadLine();
■ Console.WriteLine(x);
■ }
• Multiplication Table for one Number
■ //Example 1:
■ Console.WriteLine(DateTime.Now);
■ //This show the current Date and Time on the PC system.
■ //Example 2:
■ DateTime dt = Convert.ToDateTime("5/25/2018");
■ //Define a variable of DateTime and write the Date as string
and then convert it to DATETIME
■ // This will print the Time also 12:00:00 even if you didnot
write it .
■ Console.WriteLine(dt);
DateTime String format
Console.WriteLine(Convert.ToDateTime("5/25/2018")); 5/25/2018 12:00:00 AM
Console.WriteLine(Convert.ToDateTime("5/25/2018").ToString("d")); 5/25/2018
Console.WriteLine(Convert.ToDateTime("5/25/2018").ToString("dd")); 25
Console.WriteLine(Convert.ToDateTime("5/25/2018").ToString("ddd")); Fri
Console.WriteLine(Convert.ToDateTime("5/25/2018").ToString("dddd")); Friday
Console.WriteLine(Convert.ToDateTime("5/25/2018").ToString(“M")); May 25
Console.WriteLine(Convert.ToDateTime("5/25/2018").ToString("MM")); 05
Console.WriteLine(Convert.ToDateTime("12/25/2018").ToString("yy")); 18
Console.WriteLine(Convert.ToDateTime("12/25/2018").ToString("yyy")); 2018
DateTime String format (cont.)
Console.WriteLine(Convert.ToDateTime("12/25/2018").Day); 25
Console.WriteLine(Convert.ToDateTime("12/25/2018").Month); 12
Console.WriteLine(Convert.ToDateTime("12/25/2018").year); 2018
Console.WriteLine(Convert.ToDateTime("12/25/2018").Minute); 0
Console.WriteLine(Convert.ToDateTime("12/25/2018").AddMonths(3 3/25/2019 12:00:00 AM
));
Console.WriteLine(Convert.ToDateTime("12/25/2018").AddDays(3)); 12/28/2018 12:00:00
AM
Console.WriteLine(Convert.ToDateTime("12/25/2018").AddYears(3)); 12/25/2021 12:00:00
AM
Console.WriteLine(Convert.ToDateTime("12/25/2018").AddHours(3)) 12/25/2018 3:00:00 AM
;
Print Months Name
■ }
While Loop
■ int x=0 ;
■ while(x<=10)
■ {
■ Console.WriteLine(x);
■ x++;
■ }
While loop (infinity loop)
■ while(true)
■ {
■ Console.Write(1);
■ }
While loop (infinity loop)
■ int x=0 ;
■ while(true)
■ {
■ Console.Write(x);
■ x++;
■ }
Do while Loop
■ int x=0 ;
■ do
■ {
■ Console.WriteLine(x);
■ x++;
■ }
■ while (x<=10);
Do while Loop
■ int x=0 ;
■ do
■ {
■ Console.WriteLine("welcome");
■ x++;
■ }
■ while (false);
■ // Note that the condition absolotly cannot be met , Howeve it
print the firist item "Welcome" one time
■ //and then when check the condition on the second item , the
condition cannot be met , so the code stopped running
Do While Loop (infinity loop)
■ do
■ {
■ Console.Write(1);
■ }
■ while (true);
Do while example
■ int x = 1;
■ do
■ {
■ Console.WriteLine("Please Enter the 1st Num");
■ int Num1 = int.Parse(Console.ReadLine());
■ Console.WriteLine("Please Enter the 2nd Num");
■ int Num2 = int.Parse(Console.ReadLine());
■ int result = Num1 + Num2;
■ Console.WriteLine("The sum of operation num "+x + " = " +result);
■ x++;
■ } while (true);
ARRAYS
ARRAYS
■ // Example 1
■ string[] Names = new string[5];
■ Names[0] = "ahmed";
■ Names[1] = "mohamed";
■ Names[2] = "Ali";
■ Names[3] = "omar";
■ Names[4] = "mustafa";
■ Console.WriteLine(Names[4]);
■ // Example 2
■ string[] foods = {"ta3mya","Fool","Flafl"};
■ Console.WriteLine(foods[0]);
■ // Example 3
■ int[] x = new int[5];
■ x[0] = 5;
■ x[1] = 6;
■ x[2] = 12;
■ x[3] = 7;
■ x[4] = x[2] * x[3];
■ Console.WriteLine(x[4]); // this give 84
ARRAYS
■ Console.WriteLine("all["+y+"] ="+all[y]);
■ }
ARRAYS length
■ Console.WriteLine("please Entert the Array Count :");
■
■ int c = int.Parse(Console.ReadLine());
■ int[] x = new int[c];// عدد عناصر المصفوفه
■ x[0] = 10; //اول عنصر في المصفوفه
■ x[c-1] = 100;// اخر عنصر في المصفوفه
■ for (int y = 0; y <= c-1; y++)
■ {
■ // example 2
■ Random rnd = new Random();
■ int x = rnd.Next(1,20); // يطبع رقم عشوائي محدود
في مجال الرقم العشوائي20 والحظ عدم دخول ال19 الي1 المجال من
■ Console.WriteLine(x);
Upper- Case from & to Lower-Case charcter convert
■ Console.WriteLine(Name);
String format
Sub string
■ //example 1
■ string text = Console.ReadLine();
■ Console.WriteLine(text.Replace(";"," ").Replace("a","A")); //
Thius replace any ; you entered into your text to " " space.
■ //and also replace any 'a' to 'A‘
■ //example 2
■ string name = "Ahmed Hamed Badr"; ;
■ Console.WriteLine(name.Replace("Ahmed","Mahmoud"));
String reverse
■ namespace ConsoleApplication3
■ {
■ class Program
■ {
■ static void Main(string[] args)
■ {
■ Regex reg = new Regex("^\\d{3}-\\d{7}$");
■ // firist make avaiable of type regex(regular expression)
■ // to add the regex you need firist to add the namespace which contains the regular
expression ( using System.Text.RegularExpressions;)
■ // inside the () of the regex you can add the wanted text formate.
■ //Note that to use the \ on the C# you need to add anoth \
■ Console.Write("Please enter your phone numnber : ");
■ String str = Console.ReadLine();
■ if (reg.IsMatch(str)) Console.WriteLine("Correct Phone number"); else
Console.WriteLine("Invalid phone number");
Check Name regular expression
■ using System.Text.RegularExpressions;
■ namespace ConsoleApplication3
■ {
■ class Program
■ {
■ static void Main(string[] args)
■ {
■ Regex reg = new Regex("^[A-Z][a-z]+\\s[a-z]+$");
■ Console.Write("Please enter your Name on formate firistname second name : ");
■ String str = Console.ReadLine();
■ if (reg.IsMatch(str)) Console.WriteLine("Correct Name"); else
Console.WriteLine("Invalid Name");
Check email regular expression
■ using System.Text.RegularExpressions;
■ namespace ConsoleApplication3
■ {
■ class Program
■ {
■ static void Main(string[] args)
■ {
■ Regex reg = new Regex(@"\w+([-._]\w+)*@\w+([-.]\w)*\.\w+([-.]\w+)*$");
Console.Write("Please enter your Name on formate firistname second name : ");
■ String str = Console.ReadLine();
■ if (reg.IsMatch(str)) Console.WriteLine("Correct Email"); else
Console.WriteLine("Invalid Email");