للبحث بين هذه الأكواد قم بضغط على المفتاحين
Control + F
----------------------------------------for loop & if condition
using System;
class Class1
{
public static void Main()
{
for (int i=0;i<5;i++){
if (i == 3) continue; // if (break

mean end loop
Console.WriteLine (i);}
}
} /// the output is : 0 1 2 4
---------------------------------------------while loop:
using System;
class Class1
{
public static void Main()
{int i=0;
while (i<5){i++;
if (i == 3) continue;
Console.WriteLine (i);}
}
}/* -------> the out put is : 1245 */
---------------------------------------------do while loop:
using System;
class Class1
{
public static void Main()
{
int i=0;
do
{
i++;
if (i == 3) continue;
Console.WriteLine (i);
}
while (i<5);
}
}/* -------> the out put is : 1245 */
---------------------------------------------- foreach & Array
using System; // this program count the odd and even numbers within array :
class MainClass
{
public static void Main()
{
int odd = 0, even = 0;
int[] arr = new int [] {0,1,2,5,7,8,11};
foreach (int i in arr)
{
if (i%2 == 0)
even++;
else
odd++;
}
Console.WriteLine("Found {0} Odd Numbers, and {1} Even Numbers.",
odd, even);
}
} //The Output is :Found 4 Odd Numbers, and 3 Even Numbers.
----------------------------------------enum:
using System;
class Class1
{
enum color {red,green,blue}
public static void Main()
{
color c;
c=color.blue;
Console.WriteLine (c);
}
}
----------------------------------------- Working With Functions
- functions & functions call
- Access Modifier ( public , privit , protected )
- call static function and static class
-------------------------------functions & functions call EX:
using System;
class A
{
// Public Global Variable You Can Call This Variable From Any Class
public static int q=3;
static void Main()
{
//A y=new A(); //Creat y Object
//B z=new B(); // Creat z Object
//z.xxx(); // Call Function xxx By Using z Object
Console.WriteLine (xxx ()+2); // function Call Without Object
}
// Use public to Allow Call This Function out main Class
// Use Static to Call Function Without Creat Object
public static int xxx()
{int z=10; return z;}
}
/*class B
{
public void xxx()
{A a=new A(); // Creat a Object
a.xxx(); // Call Function xxx By Using a Object
Console.WriteLine (a.q );} // Call Global Variable From Main Class
}*/
-----------------------------------------OOP :1-overload
وهي وجود اكثر من فنكشن تحمل نفس الأسم داخل الكلاس بحيث تختلف اما في الداتا تايب او في عدد الباراميترات
using System;
class a
{
static void Main()
{
C c=new C();
c.max(5,5);
c.max ('a','b');
}
public class d
{
public void yyy()
{
}
}
}
class b
{
void xxx()
{
a.d a=new a.d();
a.yyy ();
}
}
class C
{ // overload, tow functions have the same name
public int max(int x,int y)
{
if (x>y) return x; else return y;
}
public char max(char a,char b)
{
return a;
}
}
------------------------------------------------------- overload Ex:2
using System;
class A
{
static void Main()
{
int x=int.Parse (Console.ReadLine ());
int y=int.Parse (Console.ReadLine ());
Console.WriteLine (sum(x,y));
Console.WriteLine (sum(2.1f,2.5f));
}
static int sum(int x,int y)
{ return x+y; }
static float sum(float q,float w)
{return q+w;}
}
------------------------------------------------- boxing & Casting
كيفية تعريف اوبجكت وتحويلة الى اي نوع من الداتا تايب
using System;
class A
{
static void Main()
{
object o;
int y;
string s;
o=19;
y=(int) o;
o="main";
s=(string) o; Console.WriteLine (y + " " + s);
}
}
-------------------------------------------------OOP: 2-Inheritance
خاصية الوراثة مهمة جدا كونك تستطيع الوصول الى الميثودس الخاصة بكلاس الاب بدون الحاجة الى اعادة كتابة الكود مرة اخرى
using System;
class MC
{
static void Main()
{
A a=new A ();
B b=new B();
b.x=5;
b.y=3;
a.x=3;
}
}
class A
{
public A()
{
Console.WriteLine ("aaa");
}
public int x;
}
class B:A
{
public B()
{
Console.WriteLine ("bbb");
}
public int y;
}
-----------------------------------------------------interface Ex:1
using System;
class MC
{
static void Main()
{
A a=new A ();
B b=new B();
b.x=5;
b.y=3;
b.fun();
}
}
class A
{
public A()
{
Console.WriteLine ("aaa");
}
}
interface D
{
void fun();
}
class B:A,D
{
public B()
{
Console.WriteLine ("bbb");
}
public int y;
public int x;
public void fun()
{
Console.WriteLine ("Hello");
}
}
-----------------------------------------------interface EX:2
using System;
class MC
{
static void Main()
{
A a=new A ();
B b=new B();
b.x=5;
b.y=3;
b.fun();
b.fun2 ();
}
}
class A
{
public A()
{
Console.WriteLine ("aaa");
}
}
class B:A,D
{
public B()
{
Console.WriteLine ("bbb");
}
public int y;
public int x;
public void fun(){Console.WriteLine ("Hello Fun1");}
public void fun2(){Console.WriteLine ("Hello Fun2");}
}
interface D:F
{
void fun();
}
interface F
{
void fun2();
}
-----------------------------------------interface EX3:
using System;
class Class1
{
static void Main()
{
B b = new B();
b.xxx();
}
}
interface A
{
void xxx();
}
class B:A
{
public void xxx()
{
Console.WriteLine ("Hello");
}
}
-----------------------------// three interfaces inherited Ex4:
using System;
class Class1
{
static void Main()
{
B b = new B();
b.xxx1();
b.xxx2();
b.xxx3();
}
}
interface A
{
void xxx1();
}
interface AA:A
{
void xxx2();
}
interface AAA:AA
{
void xxx3();
}
class B:A
{
public void xxx1()
{Console.WriteLine ("Hello1");}
public void xxx2()
{Console.WriteLine ("Hello2");}
public void xxx3()
{Console.WriteLine ("Hello3");}
}
-------------------------------------------
نوع من الفنكشن لايمكن ان يورث
sealed -->this mean that class cannot be inheritened
Ex : sealed class a{ void main(){} }
------------------------------------------- abstract class
using System;
class MC
{
static void Main()
{
A a=new A ();
a.xxx (); // abstract class cannot get any object from it
} // if it was inherited or not
}
abstract class A // if we remove abstract word the program will run
{
public A()
{
}
public void xxx(){Console.WriteLine ("Hello");}
}
class B:A
{
public B()
{
}
} // The output here is Error msg Because we try to call function from abstract class
---------------------------------OOP :3-virtual function & override
يتم تنفيذ الفنكشن الأفتراضية في حالة عدم تطابق شروط الاوفر رايد
using System;
class MC
{
static void Main()
{
A a=new B (); // creat a object from A class that has the size of B clss
a.xxx(); // call virtual function or override function if existing
}
}
class A
{
public A() // Construct Function
{
}
public virtual void xxx() // Virtual Function Called in this Examble if no found override function
{ Console.WriteLine ("aaa");}
}
class B:A // Class B Inherited from A Class
{
public B() // Construct Function
{
}
public override void xxx()// override function that called in this //Examble
{Console.WriteLine ("bbb");}
}
-----------------------------------------------------Refrance
تبقى قيمة المتغير محفوظة في الذاكرة حتى بعد تنفيذ الفنكشن
using System;
class MC
{
static void Main()
{
int y=3;
MC m=new MC ();
m.xxx (ref y); // call 1 x=x+1
m.xxx (ref y);// call 2 x=x+1
m.xxx (ref y);// call 3 x=x+1
}
public void xxx(ref int x) // ref mean save the value of x when exit from fuction
{ x++; Console.WriteLine (x);}
// the output with ref is: 4 5 6
// the output without ref is: 4 4 4
}
--------------------------------------------------- out
using System;
class MC
{
static void Main()
{
int y;
MC m=new MC ();
m.xxx (out y); // print the value of y from variable x
// use out to Allow Compiler to installaize the variable in other function
}
public void xxx(out int x)
{ x=3; Console.WriteLine (x);}
}
--------------------------------------------------- params
using System;
class MC
{
static void Main()
{
MC m=new MC ();
m.xxx (10,20,30); // insert more then value in function that has one Parameter
}
public void xxx(params int[] x) // insert the values in Array
{
int z=0;
for (int i=0; i<x.Length; i++)
z=z+x[i];
Console.WriteLine (z);}}
-------------------------------------------------Args :
يمكن الأستفادة منها في حالة اذا اردنا اتباع اوامر اخرى بعد كتابة اسم الملف الذي نريد تنفيذه
using System;
class MC
{
static void Main(string [] args)
{
int q=0;
if (args.Length >0)
{
for (int i=0;i<args.Length;i++)
{
int z=int.Parse (args[i]);
q+=z;
}
Console.WriteLine (q);
}
else Console.WriteLine ("no input");
}
}
-------------------------------------------------Exception :
تستخدم الاستثنائات لسيطرة على الاخطاء التي يمكن ان تحدث اثناء تنفيذ الكود
using System; //Examble 1
class MC
{
static void Main()
{
int x,y,z;
x= int.Parse ( Console.ReadLine ());
y= int.Parse ( Console.ReadLine ());
try
{
z=x/y;
}
catch (Exception)
{
Console.WriteLine ("Error1");
}
}
}
----------------------------------------------------
using System; //Examble 2
class MC
{
static void Main()
{
int x,y,z;
x= int.Parse ( Console.ReadLine ());
y= int.Parse ( Console.ReadLine ());
try
{
z=x/y;
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
}
}
--------------------------------------------------
using System; //Examble 3
class MC
{
static void Main()
{
int x,y,z;
x= int.Parse ( Console.ReadLine ());
y= int.Parse ( Console.ReadLine ());
try
{
z=x/y;
}
catch (DivideByZeroException)
{
Console.WriteLine ("Error1");
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
finally
{
Console.WriteLine ("Error finally");
}
}
}
--------------------------------------------checked overflow
using System;
class MC
{
static void Main()
{
int x,y,z;
try
{
checked
{ z=int.MaxValue;
Console.WriteLine (z);
z++;
Console.WriteLine (z);}
}
catch (Exception e)
{
Console.WriteLine (e.Message); } } }
--------------------------------------------string
using System;
class MC
{
static void Main()
{
string a="main jitawi";
char f1= a[2];
Console.WriteLine(f1);
string b="c is great";
string b2=b.Insert(2,"sharp");
Console.WriteLine(b2);
string msg="hello";
int c=msg.Length;
Console.WriteLine(c);
string d="hello";
string d2=string.Copy (d);
Console.WriteLine(d2);
string e=string.Concat ("a","b","c","d");
Console.WriteLine(e);
string e2=string.Concat ("a"+"b"+"c"+"d");
Console.WriteLine(e2);
string e3 = "a"+"b"+"c"+"d";
Console.WriteLine(e3);
}
}
---------------------------------------------Refliction
using System; // use this program to know info about string methods
using System.Reflection;
class MC
{
static void Main()
{
Type t=typeof(byte);
Console.WriteLine("Type:{0}",t);
Type t1=typeof(string);
MethodInfo [] mi=t1.GetMethods();
foreach (MethodInfo m in mi)
{ Console.WriteLine("Method:{0}",m);}
}}
--------------------------------------------------File
القرائة و الكتابة على ملف تكست
using System;// get strings from Text File
using System.IO; // and write Strings on Text File
class A
{
static void Main()
{
StreamReader reder = new StreamReader("c:\\ali1.txt");
StreamWriter writer = new StreamWriter("c:\\ali2.txt");
string line;
while((line = reder.ReadLine())!=null)
{writer.WriteLine(line);}
reder.Close();
writer.Close();
}
}
-----------------------------------------namespace
التعامل مع النيم سبيس
using System;
namespace xxx
{
class A
{
static void Main()
{
yyy.zzz.B b=new yyy.zzz.B ();
b.fun ();
}
}
}
namespace yyy
{
namespace zzz
{
class B
{
public void fun(){Console.WriteLine("Hello");}
}
}}
-------------------------------------------unsafe code
استخدام البوينترز في السي شارب
using System;// Allow to useing pointers with C#
namespace xxx
{
class A
{
static void Main()
{
yyy.zzz.B b=new yyy.zzz.B ();
b.fun ();
}
}
}
namespace yyy
{
namespace zzz
{
class B
{
unsafe public void fun(){int a=5;int *p=&a;Console.WriteLine(*p);}
}
}}
---------------------------------------------Proparity Ex:1
كيفية عمل بروبرتي في السي شارب
using System;
class A
{
static void Main()
{
B b = new B();
b.fun();
Console.WriteLine (x);
}
static private int x;
public void set_xxx(int i) {x=i;}
}
class B
{
public void fun()
{
A a=new A();
a.set_xxx (5);
}}
----------------------------------------------------Proparity Ex:2
using System;
class A
{
static void Main()
{
B b = new B();
b.fun();
}
static private int x;
public void set_xxx(int i) {x=i;}
public int get_xxx(){return x;}
}
class B
{
public void fun()
{
A a=new A();
a.set_xxx (5);
Console.WriteLine (a.get_xxx());
}
}
-----------------------------------------------Proparity Ex:3
using System;
class A
{
static void Main()
{
B b = new B();
b.fun();
}
private int name;
public int Name{ set{name=value;} get{return name;}
}
}
class B
{
public void fun()
{
A a=new A();
a.Name=5;
Console.WriteLine (a.Name);
}}
-------------------------------------------------delegate
using System;
class A
{
public delegate void qq();
static void Main()
{
qq q=new qq (xxx);
q();
q=new qq (yyy);
q();
q=new qq (zzz);
q();
}
public static void xxx(){Console.WriteLine ("xxxxxx");}
public static void yyy(){Console.WriteLine ("yyyyyy");}
public static void zzz(){Console.WriteLine ("zzzzzz");}
}
----------------------------------------------- Delegate Ex:2
using System;
class A
{
public delegate void xxx(int a,int b);
static void Main()
{
xxx q=new xxx (fun2);
q(2,12);
A a =new A ();
q = new xxx (a.fun1 );
q(3,4);
D d=new D();
q= new xxx (d.fun3 );
q(8,9);
}
public void fun1(int x,int y)
{int z;z=x+y;Console.WriteLine (z);}
public static void fun2(int x,int y)
{int z;z=x*y;Console.WriteLine (z);}
}
class D
{
public void fun3(int x,int y)
{Console.WriteLine (x);}}
--------------------------------------------------------event
using System;
public delegate void A();
class B
{
public event A C;// declaration event C
public void D() {C();}
}
public class E
{
static private void f(){ Console.WriteLine ("Hello");}
static public void x() {Console.WriteLine ("Hello2");}
static void Main()
{
B b=new B ();
b.C +=new A

;// store the output of function f in event C
b.C +=new A(x);// store the output of function x in event C
b.D ();//call D function to Veiw the event stores
}
}
----------------------------------------------------------this
/*call any External object function or variable from the same class without make object to call it,cannot uses with static functions or static variables ,can be used to access members from within constructors.*/
using System;
public class A
{
int x=34;
public static void Main()
{
A a=new A ();
a.fun ();
}
public void fun()
{
Console.WriteLine (this.x );
}
}
----------------------------------------------------------index
using System;
class BookIndexor
{
private int [] bookcollection = new int [50];
//b[3]
public int this [int index]
{
get
{
if (index<0||index>=50) return 0;
else return bookcollection[index];
}
set
{if (!(index<0||index>=50))bookcollection[index]=value;}
} //3 256
}
public class MainClass
{
public static void Main()
{
BookIndexor b=new BookIndexor ();
b[3]=256;
for (int i=0;i<=3;i++)
{
Console.WriteLine ("Book Collection #{0}={1}",i,b[i]);
}
}
}
------------------------------------------------------------------------------------------------------------
التعامل مع قواعد البيانات
using System; ADO.NET Consol Example
using System.Data;
using System.Data.OleDb;
namespace ADODotNet
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=Test.MDB");
// open connection
myConnection.Open();
//to Create DataAdapter object for upadte or other operation
OleDbDataAdapter myAdapter=new OleDbDataAdapter("SELECT * FROM Table1",myConnection);
OleDbCommandBuilder myBuild=new leDbCommandBuilder(myAdapter);
//to Create DataSet to contain related data tables,rows and columns
DataSet myDataSet=new DataSet();
//to Fill DataSet from Table1
myAdapter.Fill(myDataSet,"Table1");
/*set up keys object for defining primary key
*Before you using find we have to set up a primary you'll use during searching */
DataColumn[] keys=new DataColumn[1];
keys[0]=myDataSet.Tables["Table1"].Columns["Column1"];
myDataSet.Tables["Table1"].PrimaryKey=keys;
DataRow findRow=myDataSet.Tables["Table1"].Rows.Find("mazy");
if(findRow==null)
{
Console.WriteLine("mazy,not found and I'll add it to table");
DataRow myRow=myDataSet.Tables["Table1"].NewRow();
myRow["Column1"]="mazy";
myRow["Column2"]="Hello everybody!";
myDataSet.Tables["Table1"].Rows.Add(myRow);
if((findRow=myDataSet.Tables["Table1"]
.Rows.Find("mazy"))!=null)
Console.WriteLine("mazy,successfully added to Table1");
}
Else
{
Console.WriteLine("mazy,already present in database");
}
myAdapter.Update(myDataSet,"Table1");
myConnection.Close();
}}}
-------------------------------------------------------------------
برنامج شات مبسط بستخدام الكونسول
using System; Socket Example – Server Code
using System.Text;
using System.Net;
using System.Net.Sockets;
public class serv
{
public static void Main()
{
Console.WriteLine("Finding Client...");
xxx:
try
{
IPAddress ipAd = IPAddress.Parse("192.168.0.2"); //use local m/c IP adds, and use the same in the client
TcpListener myList=new TcpListener(ipAd,8001);
myList.Start();
Socket s=myList.AcceptSocket();
Console.Write("Message From " + ipAd + ": ");
byte[] b=new byte[100];
int k=s.Receive(b);
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(b[i]));
Console.WriteLine ("");
ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
s.Close();
myList.Stop();
goto xxx;
}
catch (Exception)
{
goto xxx;
}
}
}
using System; Socket Example – Client Code
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
public class clnt
{
public static void Main()
{
Console.Write("Connecting.....");
xxx:
try
{
TcpClient tcpclnt = new TcpClient();
Console.Write(".");
tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter Your Message : ");
String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba,0,ba.Length);
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(bb[i]));
if (str=="end") tcpclnt.Close();
else
goto xxx;
}
catch (Exception)
{
goto xxx;
}
}
}