Sei sulla pagina 1di 65

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

Console.WriteLine("This the range of Byte");


Console.WriteLine("Minimum value is"+byte.MinValue);
Console.WriteLine("Maximum value is" + byte.MaxValue);
Console.WriteLine("###############");
Console.WriteLine("This the range of Byte");
Console.WriteLine("Minimum value is" + int.MinValue);
Console.WriteLine("Maximum value is" + int.MaxValue);
Data Type size

Console.WriteLine("This the size of Byte");


Console.WriteLine("Size value is " + sizeof(byte));
Console.WriteLine("###############");

Console.WriteLine("This the size of Byte");


Console.WriteLine("Size value is " + sizeof(int));
• Variables decleration
class Program
{
static void Main(string[] args)
{
int x = 8; // Data Type Variable Name = Value; This variable declration
String Sname = "AHMED"; // " " is used to represent a string.
Char CC = 'A'; // ' ' is used to represent a Char.
Console.WriteLine(x); // used tp print the value of variable X
Console.WriteLine(Sname);
Console.WriteLine(CC);

}
}
}
• 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();

■ bool x =int.TryParse( r , out y);


■ bool w = double.TryParse(r, out z);
■ if (x)
■ Console.WriteLine(y.GetTypeCode());
■ else if (w)
■ Console.WriteLine(z.GetTypeCode());
■ else
■ Console.WriteLine(r.GetTypeCode());
• Conditional Operator

■ Console.WriteLine("Please Enter Num1 : ");


■ int x = int.Parse(Console.ReadLine());
■ Console.WriteLine("Please Enter Num2 : ");
■ int y = int.Parse(Console.ReadLine());
■ int greater = (x > y) ? x : y ;
■ Console.WriteLine("The greater is " + greater);
• Switch Statement
Console.Write("Please Enter Num1 :");
int Num1 = int.Parse(Console.ReadLine());
Console.Write("Please Enter Num2 :");
int Num2 = int.Parse(Console.ReadLine());
Console.Write("Please Enter the operand + OR - OR * OR / OR % : ");
char operand = Convert.ToChar(Console.ReadLine());
double result;
switch (operand)
{
case '+':
result = Num1 + Num2;
Console.WriteLine(Num1+"+"+Num2+"= " + result);
break;
case '-':
result = Num1 - Num2;
Console.WriteLine(Num1 + "-" + Num2 + "= " + result);
break;
case '*':
result = Num1 * Num2;
Console.WriteLine(Num1 + "*" + Num2 + "= " + result);
• FOR Loop

■ String word = "welcome";


■ for (int x = 0; x <= 10; x++)
■ {
■ Console.WriteLine(x +":" + word);
■ }
■ //This print the word "welcome" 10 Times
• Print Character using For statement in
ascending order
■ for (char c ='a'; c<='z';c++)
■ {
■ Console.WriteLine(c);
■ }
• Print Character using For statement
in descending order
■ for (char c ='z'; c>='a';c--)
■ {
■ Console.WriteLine(c);
■ }
• Many variables in For Statement

■ for (int x = 0, y = 10, z = 100 ; x < 10; x++ ,y+=10,z+=100 )


■ // x will increase by 1 and y will increase by 10 and z will increase
by 100
■ {
■ Console.WriteLine(x+" : "+y+" : "+z);
■ }
• Infinity For Loop
■ //Example 1 to write the text 01 to infinty time
■ for (;;)
■ {
■ Console.Write("01");
■ }

■ //Example 2 to make a counter to count from 0 to infinity


■ for (int x = 0; ; x++)
■ {

■ Console.WriteLine(x);
■ }
• Multiplication Table for one Number

■ Console.WriteLine("Please enter the Number


that you want to show its multiplication
table");
■ int x =
int.Parse(Console.ReadLine());

Console.WriteLine("######################");
■ int result;
■ for (int y = 0; y <= 12; y++)
■ {
■ result = x * y;
■ Console.WriteLine(x+" X " + y
+ "= " + result);
■ }
• Nested For Loop

■ for (int x = 0; x <= 2; x++)


■ {
■ Console.WriteLine(x);
■ for (Char y = 'a'; y <= 'e'; y++)
■ {
■ Console.WriteLine(" " + y);
■ }
■ }
■ }
• Nested For Loop
(all multiplication table)
■ Console.WriteLine("All Multiplication Table");
■ int result;
■ for (int x = 0; x <= 12; x++)
■ {
■ Console.WriteLine("This is the multiplication Table of "+ x);
■ for (int y = 0; y <= 12; y++)
■ {
■ result = x * y;
■ Console.WriteLine(x + " X " + y + " = " + result);
■ // This Loop finish completly for each value on the firist For loop
■ //for example for X loop when it equal to 0 , the Y loop run completly from 0 to 12
■ }
■ }
• Date Time

■ //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("5/25/2018").ToString("MMM")); May (‫(مختصره‬

Console.WriteLine(Convert.ToDateTime("12/25/2018").ToString("y")); December 2018

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

■ for (int x = 1; x <= 12; x++)


■ {

Console.WriteLine(Convert.ToDateTime(x+"/25/2018").ToString("MMM
"));

■ }
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

■ object[] all = { "ahmed",44,'c',true,22.25 };


■ for (int y = 0; y <=4;y++)
■ {

■ 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++)
■ {

■ Console.WriteLine("x[" + y + "] =" + x[y]);



■ }
■ Console.WriteLine(x.Length);
Arrays (Char to string ) & (string to char)
■ // Char to String
■ char[] c = { 'a', 'b', 'c' };
■ string s = new string(c);
■ Console.WriteLine(s);
■ // String to Char
■ string Name = "AHMED";
■ char[] Cname = Name.ToCharArray();
■ for (int x = 0; x <= Cname.Length-1; x++)
■ {
■ Console.WriteLine(Cname[x]);
■ }
ForEach Loop

■ string Name = "AHMED";


■ char[] Cname = Name.ToCharArray();
■ foreach(char c in Cname)// define small variable
from the big variable
■ {
■ Console.WriteLine(c);
■ }
Random Numbers

■ Random rnd = new Random();


■ int x = rnd.Next(); // ‫يطبع رقم عشوائي غير محدود المجال‬
■ Console.WriteLine(x);

■ // 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

■ string name = "ahmed";


■ Console.WriteLine(name.ToUpper()); // ‫يقوم بتحويل‬
‫الحروف الصغيره الي حروف كبيره‬

■ string name = "AHMED";


■ Console.WriteLine(name.ToLower()); // ‫يقوم بتحويل‬
‫الحروف الكبيره الي حروف صغيره‬
String Character Length

■ Console.WriteLine("Please enter your name , Maximum size of your name is


10 character : ");
■ string name = Console.ReadLine();
■ if (name.Length > 10) // Note that spaces are counted into the
string length
■ {
■ Console.WriteLine("Invalid Name");
■ }
■ else {
■ Console.WriteLine(name);
■ }
String Format

■ string name1 = "ahmed";


■ string name2 = "hamed";
■ string lastname = "badr";
■ string Name = string.Format("Firist name is :{0}\nSecond name is :{1}\nLast name is :{2}",name1,name2,lastname);

■ Console.WriteLine(Name);
String format
Sub string

■ string name = "ahmed hamed badr";


■ Console.WriteLine(name);
■ Console.WriteLine(name.Substring(2) + " this
start cutting from start index 2 to the length of infinity");
■ Console.WriteLine(name.Substring(3,9) + " this
start cutting from start index and then take character length of
9");
Substring (delete the last 4 characters for file name)

■ Console.WriteLine("Please Enter the file name") ;


■ string name = Console.ReadLine();
■ if (name.Length > 4)
■ {
■ Console.WriteLine(name.Substring(0, name.Length - 4));
■ }
■ else { Console.WriteLine("invalid"); }

String Split

■ string word = "welcome to Egypt";


■ string[] split = word.Split(' ') ;// this split the
sentense at the space ' ' character
■ // Console.WriteLine(split[0]); // This print the firist
item on the array , which is result of the split method

■ foreach (string wor in split)


■ {
■ Console.WriteLine(wor);
■ }
String replace

■ //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

■ string name = "Ahmed Hamed Badr"; ;


■ char[] a = name.ToCharArray();
■ Array.Reverse(a); // This method only can work with
arrays
■ ‫تقوم بعكس عناصر المصفوفه من النهايه الي البدايه‬//
■ Console.WriteLine(a);
Check string if it is Empty

■ Console.WriteLine("please enter your name : ");


■ string Name = Console.ReadLine();
■ if (Name.Trim() == "")
■ Console.WriteLine("Empty Name");
■ else Console.WriteLine(Name);
‫‪Regular expression‬‬
‫‪\d‬‬ ‫رقم صحيح من ‪ 0‬الي ‪9‬‬
‫‪\w‬‬ ‫اي حرف كبير او صغير او رقم‬
‫‪\s‬‬ ‫مسافه‬
‫][‬ ‫تستخدم لتحديد شكل معين انت تريده‬
‫]‪For example: [ 0-5] , [ 6-9] , [a-tA-T‬‬
‫يبقي انت هنا بتقوله انا عايز في الخانه دي مثال‬
‫رقم من ‪ 0‬الي ‪ 5‬مثال‬
‫}{‬ ‫تستخدم في الخانات‬
‫}‪For example: \d {3‬‬
‫هنا انا بقوله انا عايز من التنسيق ده ‪ 3‬خانات‬
‫وهي كده كاني بقوله بالضبط ‪\d\d\d‬‬
‫*‬ ‫تستخدم في الخانات‬
‫*‪For example: \d‬‬
‫كده كاني بقوله يا مدخلشي من التنسيق ده‬
‫حاجه او دخل منه كتير‬

‫‪+‬‬ ‫تستخدم في الخانات‬


Check phone regular expression
■ using System.Text.RegularExpressions;

■ 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");

Potrebbero piacerti anche