Visual C# Programming
Visual C# Programming
Lan
-
.NET Framework
Langu
:-
printf("hello");
Version .... Release ... Visual Studio ................... Default in Window
1.0 ............ 2002 ....... Visual Studio .NET .............................-
1.1 ............ 2003 ....... Visual Studio .NET 2003 .......... Window Server 2003
2.0 ............ 2005 ....... Visual Studio 2005 ............................-
3.0 ............ 2006 ..................- .............................. Window Vista, Window Server 2008
3.5 ............ 2007 ....... Visual Studio 2008 .................. Window 7, Window Server 2008 RC2
4.0 Beta1 .... 2009 ....... Visual Studio 2010
[URL]http://www.microsoft.com/downloads/details.aspx?FamilyId=333325FD-AE52-4E35-B531-
508D977D32A6&displaylang=en[/URL]
java
class Hello
{
static void Main()
{
System.Console.Write("Hello World");
}
}
stati
semicomar(
class library
using System;
class Hello
{
static void Main()
{
Console.Write("Hello World");
}
}
using
C#
dir csc.exe /s
csc.exe C:\Test1.cs
dir Test1.exe
exe
Test1.exe
or
Test1
***********
[URL]http://ifile.it/38lwqa5[/URL]
-
- System, Console, Write, M
- ;
-
data types of C#
Variable
int num = 3;
int
string = њ ќ;
string
variable declaration
Code:
abstract event new struct as explicit null switch base extern object
this bool false operator throw break finally out true byte fixed
override try case float params typeof catch for private uint char
foreach protected ulong checked goto public unchecked class if
readonly unsafe const implicit ref ushort continue in return using
decimal int sbyte virtual default interface sealed volatile delegate
internal short void do is sizeof while double lock stackalloc else
long static enum namespace string
8
3 => int
3L => long
3.0 or 3.0D => double
3.0F => float
3.0M => decimal
'3' => char
"3" => string
string
Code:
string v1 = "3";
string v2 = "4";
Console.Write(v1+v2);
Code:
34
4
type conversati
int i = 123;
object o = i; // implicit boxing
object o = (object) i; // explicit boxing
int i = (int) o; // unboxing
string st = "123";
int i = Convert.ToInt32(st);
int i = Int32.Parse(st);
string st = Convert.ToString(i);
string st = i.ToString();
Code:
static void Main()
{
}
Code:
int num;
Code:
Console.Write("Enter a number : ");
њ ќ
Code:
string temp = Console.ReadLine();
num = Convert.ToInt32(temp);
Code:
num = Convert.ToInt32(Console.ReadLine());
Code:
Consol њ ќ ;
њ ќ
format s
Code:
Console њ њ ќ њ ;
Code:
њ\ ќ;
Console.Read();
\n
Code:
using System;
Code:
using System;
class Program
{
static void Main(string[] args)
{
string temp; int num;
Console.Write("Enter a number : ");
temp = Console.ReadLine();
num = Convert.ToInt32(temp);
Console.WriteLine("Square of {0} is {1}", num, num * num);
Console.Write("\npress enter to close...");
Console.Read();
}
}
Output
Code:
Enter a number : 4
Square of 4 is 16
- - - -
error handling in C#
1)Compile Error
semicolon (
2)Logic Error
њ ќ ;
њ ќ ;
3)Runtime Error
use
Exception Handling
Code:
try{
num = Convert.ToInt32(temp);
}
catch{
њ ќ;
num = 0;
}
Code:
try{
num = Convert.ToInt32(temp);
}
catch( FormatException ex )
{
њ ќ;
num = 0;
}
catch( OverFlowException ex )
{
њY ќ;
num = 0;
}
catch
{
њ ќ;
num = 0;
}
Y
Code:
using System;
class Program
{
static void Main(string[] args)
{
string temp; int num;
Console.Write("Enter a number : ");
temp = Console.ReadLine();
try
{
num = Convert.ToInt32(temp);
}
catch (FormatException ex)
{
Console.WriteLine("Please enter number value");
num = 0;
}
catch (OverflowException ex)
{
Console.WriteLine("Your number is too large");
num = 0;
}
catch
{
Console.WriteLine("Unknown error");
num = 0;
}
Console.WriteLine("Square of {0} is {1}", num, num * num);
Console.Write("\npress enter to close...");
Console.Read();
}
}
+
=
Code:
http://msdn.microsoft.com/en-us/library/6a71f45d(VS.71).aspx
1) Console.Write( 2+3*4 );
2) Console.Write( (2+3)*4 );
4
Precedence
Operators
Highest
+ - ! ~ ++x --x (T)x
*/%
+-
<< >>
< > <= >= is as
== !=
&
^
|
&&
||
Lowest
= *= /= %= += -= <<= >>= &= ^= |=
()
*/
+-
-
--
Code:
int num1 = 3;
int num2 = 4;
Console.Write( num1++ + ++num2 );
output = 7 or 8 or 9 ????
5 8
Code:
short v1 = 3;
short v2 = 4;
= ;
= ;
pro
Code:
using System;
public class Example1_6
{
static void Main()
{
int i1 = 5;
long l = i1 * 3L;
decimal d = l; // implicit conversation
int i2 = (int) d; // explicit conversation
Console.WriteLine("{0:C}\n{0:D5}\n{0:F2}\n",i2);
int i3 = i2 / 2;
Console.WriteLine("{0:C}\n{0:D5}\n{0:F2}\n",i3);
int i4 = i1 + i2 * 10;
Console.WriteLine("{0:C}\n{0:D5}\n{0:F2}\n",i4);
int i5 = i2++ + ++i3; // increment operator
Console.WriteLine("{0:C}\n{0:D5}\n{0:F2}\n",i5);
Console.Write("press enter to close...");
Console.Read();
}
}
: : 5
function
functi
Function
Code:
int AddTwoNumber( int p1, int p2 )
{
return p1 + p2;
}
funct
Code:
int AddTwoNumber( int p1, int p2 );
¦
Code:
int res = AddTwoNumber( 3 , 4 );
:
Parameter passing in C#
1. Value Parameter
2. Reference Parameter
4. Params Arra
1)Value Passing
Code:
using System;
class valuePassTest
{
static void Main()
{
int num = 10;
Console.WriteLine("before : " + num);
Inc(num);
Console.WriteLine("after : " + num);
Console.Read();
}
static void Inc(int num)
{
num = num + 1;
}
}
output
Code:
before : 10
after : 10
2)Reference Passing
Code:
using System;
class referencePassTest
{
static void Main()
{
int num = 10;
Console.WriteLine("before : " + num);
Inc(ref num);
Console.WriteLine("after : " + num);
Console.Read();
}
static void Inc(ref int num)
{
num = num + 1;
}
}
output
Code:
before : 10
after : 11
3) output parameter
Code:
using System;
class outputPassTest
{
static void Main()
{
int num = 10; int result;
Square(out result, num);
Console.WriteLine("result : " + result);
Console.Read();
}
static void Square(out int result, int num)
{
result = num * num;
}
}
output
Code:
result : 100
4) Params Array
Code:
using System;
class paramsTest
{
static void Main()
{
Show(1, 3, 5, 7, 9);
Console.Read();
}
static void Show(params int[] arr)
{
foreach (int x in arr)
{
Console.Write(x + " ");
}
}
}
output
Code:
13579
5
switch
conditional operator (?
Code:
using System;
class Program
{
static void Main()
{
int num;
Console.Write("enter number");
try
{
num = Convert.ToInt32(Console.ReadLine());
if (num % 2 == 0)
{
Console.WriteLine("Even");
}
else
{
Console.WriteLine("Odd");
}
}
catch
{
Console.WriteLine("Error");
}
Console.Read();
}
}
Code:
using System;
class Program
{
static void Main()
{
int num;
Console.Write("enter number");
try
{
num = Convert.ToInt32(Console.ReadLine());
switch (num % 2)
{
case 0: Console.WriteLine("Even");
break;
default: Console.WriteLine("Odd");
break;
}
}
catch
{
Console.WriteLine("Error");
}
Console.Read();
}
}
default
Code:
using System;
class Program
{
static void Main()
{
int num;
Console.Write("enter number");
try
{
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine( (num % 2 == 0) ? "Even" : "Odd" );
}
catch
{
Console.WriteLine("Error");
}
Console.Read();
}
}
-
-
if-else
Code:
if (num == 0)
Console.WriteLine("Zero");
else if (num == 1)
Console.WriteLine("One");
else if (num == 2)
Console.WriteLine("Two");
else if (num == 3)
Console.WriteLine("Three");
else if (num == 4)
Console.WriteLine("Four");
else if (num == 5)
Console.WriteLine("Five");
else if (num == 6)
Console.WriteLine("Six");
else if (num == 7)
Console.WriteLine("Seven");
else if (num == 8)
Console.WriteLine("Eight");
else if (num == 9)
Console.WriteLine("Nine");
else
Console.WriteLine("Greater than 9");
switch
Code:
switch(num)
{
case 0:Console.WriteLine("Zero");
break;
case 1:Console.WriteLine("One");
break;
case 2:Console.WriteLine("Two");
break;
case 3:Console.WriteLine("Three");
break;
case 4:Console.WriteLine("Four");
break;
case 5:Console.WriteLine("Five");
break;
case 6:Console.WriteLine("Six");
break;
case 7:Console.WriteLine("Seven");
break;
case 8:Console.WriteLine("Eight");
break;
case 9:Console.WriteLine("Nine");
break;
default:Console.WriteLine("Greater than 9");
break;
}
Conditional Operator
Code:
Console.WriteLine((num == 0) ? "Zero" : (num == 1) ? "One" : (num == 2) ? "Two" : (num == 3) ? "Three" : (num
== 4) ? "Four" : (num == 5) ? "Five" : (num == 6) ? "Six" : (num == 7) ? "Seven" : (num == 8) ? "Eight" : (num ==
9) ? "Nine" : "Greater than 9");
Code:
int val = 0;
if(num%2==val) // this is ok
switch(num%2)
{
case val: // this is not ok ( coz : val is a variable name, not a constant )
looping
њ ќ;
- while
start stop step for looping
Code:
looping
statements
Code:
start;
for( ; stop; )
{
statements;
step;
}
Example:
Code:
int i = 1;
for( ; i<=10; )
{
њ ќ;
i++;
}
while looping
Code:
start;
while ( stop )
{
statements;
step;
}
Example:
Code:
int i = 1;
while( i<=10 )
{
њ ќ;
i++;
}
do-while
Code:
start;
do{
statements;
step;
}while( stop );
Example:
Code:
int i = 1;
do{
њ ќ;
i++;
}while( i<=10 );
-
state
Code:
using System;
class forlooptest
{
static void Main()
{
bool stop = false;
do
{
Console.Write("Enter a char to see ASCII value of it (or) press ESC to close.");
ConsoleKeyInfo cki = Console.ReadKey();
if (cki.Key != ConsoleKey.Escape)
Console.WriteLine("\n{0} = {1}\n", cki.Key.ToString(), (int)cki.KeyChar);
else
stop = true;
} while (stop==false);
}
}
false
foreach looping
Code:
foreach( People p in MZ.Member )
{
= њ ќ;
}
in ke
Code:
using System;
class forlooptest
{
static void Main()
{
string st;
Console.Write("Enter a string : ");
st = Console.ReadLine();
foreach (char c in st)
{
Console.WriteLine("{0} = {1}", c, (int)c);
}
Console.Read();
}
}
single dimension array
Code:
int[ ] arr = new int[5];
Code:
arr[0] = 2;
arr[1] = 4;
arr[2] = 6;
arr[3] = 8;
arr[4] = 0;
array
Code:
int[ ] arr = new int[ ] { 2, 4, 6, 8, 0 };
Code:
int[ ] arr = { 2, 4, 6, 8, 0 };
Code:
int[ ] arr; // declaration statement
arr = new int[ ] { 2, 4, 6, 8, 0 }; // initialization statement
Code:
using System;
class forlooptest
{
static void Main()
{
int[] arr1 = { 1, 3, 5, 7, 9 };
int[] arr2 = { 2, 4, 6, 8, 0 };
int[] arr3 = new int[5];
for (int i = 0; i < arr1.Length; i++)
{
arr3[i] = arr1[i] + arr2[i];
}
Console.Write("Result array : ");
foreach (int v in arr3)
Console.Write(v + " ");
Console.Read();
}
}
multidimensional array
multi-dimension
Code:
Code:
arr[0,0] = 1;
arr[0,1] = 2;
arr[1,0] = 3;
arr[1,1] = 4;
arr[2,0] = 5;
arr[2,1] = 6;
mu
Code:
int[,] arr = { {1,2} , {3,4} , {5,6} } ;
Code:
for (int i = 0; i <= arr.GetUpperBound(0); i++)
{
for (int j = 0; j <= arr.GetUpperBound(1); j++)
Console.Write(arr[i, j]+" ");
Console.WriteLine();
}
GetUpperBound
Code:
using System;
class Program
{
static int GetInteger()
{
try
{
return Convert.ToInt32(Console.ReadLine());
}
catch
{
return 1;
}
}
static void Main(string[] args)
{
int r1, r2, c1, c2;
int[,] matrix1;
int[,] matrix2;
int[,] matrix3;
Console.WriteLine("First Matrix\n************");
Console.Write("row : ");
r1 = GetInteger();
Console.Write("col : ");
c1 = GetInteger();
matrix1 = new int[r1, c1];
Console.WriteLine();
Console.WriteLine("Second Matrix\n************");
Console.Write("row : ");
r2 = GetInteger();
Console.Write("col : ");
c2 = GetInteger();
matrix2 = new int[r2, c2];
Console.WriteLine();
for (int i = 0; i <= matrix1.GetUpperBound(0); i++)
{
for (int j = 0; j <= matrix1.GetUpperBound(1); j++)
{
Console.Write("Enter value for first matrix : row {0}, column {1} : ", i, j);
matrix1[i, j] = GetInteger();
}
}
Console.WriteLine();
for (int i = 0; i <= matrix2.GetUpperBound(0); i++)
{
for (int j = 0; j <= matrix2.GetUpperBound(1); j++)
{
Console.Write("Enter value for second matrix : row {0}, column {1} : ", i, j);
matrix2[i, j] = GetInteger();
}
}
if (c1 != r2)
{
if (r1 == 1 && c1 == 1)
{
matrix3 = new int[r2, c2];
for (int i = 0; i <= matrix2.GetUpperBound(0); i++)
for (int j = 0; j <= matrix2.GetUpperBound(1); j++)
matrix3[i, j] = matrix2[i, j] * matrix1[0, 0];
}
else
{
matrix3 = new int[r1, c1];
for (int i = 0; i <= matrix1.GetUpperBound(0); i++)
for (int j = 0; j <= matrix1.GetUpperBound(1); j++)
matrix3[i, j] = matrix1[i, j] * matrix2[0, 0];
}
}
else
{
matrix3 = new int[r1, c2];
for (int i = 0; i <= matrix1.GetUpperBound(1); i++)
{
for (int j = 0; j <= matrix1.GetUpperBound(0); j++)
{
for (int k = 0; k <= matrix2.GetUpperBound(1); k++)
matrix3[j, k] += matrix1[j, i] * matrix2[i, k];
}
}
}
Console.WriteLine("\nResult Matrix\n*************");
for (int i = 0; i <= matrix3.GetUpperBound(0); i++)
{
for (int j = 0; j <= matrix3.GetUpperBound(1); j++)
{
Console.Write(matrix3[i, j] + " ");
}
Console.WriteLine();
}
Console.Read();
}
}
ArrayList
ArrayList Class
Code:
using System;
using System.Collections;
class arraylistTest
{
static void Main()
{
object[] arr = new object[3];
arr[0] = 1;
arr[1] = 2.2;
arr[2] = "abcdefg";
Code:
using System;
using System.Collections;
class arraylistTest
{
static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("Name", "Sevenlamp");
ht.Add("Address", "Yangon");
ht.Add("Age", 28);
enumeration
variable
declaration
Code:
int num;
Code:
myOwnType var1;
-
Code:
enum myOwnType
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
de
Code:
enum myOwnType : byte
{
Sunday, Monday = 5, Tuesday, Wednesday, Thursday = 100, Friday, Saturday
}
Sunday = 0,
Monday = 5,
Tuesday = 6,
Wednesday = 7,
Thursday = 100,
Friday = 101,
Saturday = 102
Code:
using System;
class Program
{
enum myOwnType : byte
{
Sunday, Monday = 5, Tuesday, Wednesday, Thursday = 100, Friday, Saturday
}
structure
Enumeration
structure class
Code:
using System;
struct Student
{
public int roll;
public string name;
public int mark;
}
class structuretest
{
static void Main()
{
Student s1;
s1.roll = 1; s1.name = "Ag Ag"; s1.mark = 90;
Student s2 = s1;
s2.roll = 2; s2.name = "Mg Mg";
Student s3 = new Student();
s3.roll = 3; s3.name = "Zw Zw"; s3.mark = 80;
Code:
struct Student
{
public int roll;
public string name;
public int mark;
}
variabl
Code:
Student s1;
Code:
s1.roll = 1; s1.name = "Ag Ag"; s1.mark = 90;
њ ќ 9
Code:
Student s2 = s1;
Code:
s2.roll = 2; s2.name = "Mg Mg";
Code:
Student s3 = new Student();
s3.roll = 3; s3.name = "Zw Zw"; s3.mark = 80;
defa
membe
cl
varia
memory
њ ќ
constructors
Code:
Student s1 = new Student();
function name
Default
constructo
Code:
class Student
{
public int roll;
public string name;
public int mark;
public Student() // default constructor
{
roll = -1;
= њ ќ;
mark = 0;
}
public void Display()
{
њ\n Roll : {0} \n Name : {1} \ : ќ
roll, name, mark);
}
}
class StudentTest
{
static void Main()
{
Student s1 = new Student();
s1.Display();
Console.Read();
}
}
= = њ ќ
= = = њ ќ =
object
Code:
class Student
{
public int roll;
public string name;
public int mark;
public Student(string name) // parameterized constructor
{
roll = -1;
this.name = name; // assign local to member
mark = 0;
}
public void Display()
{
њ\n Roll : {0} \n Name : {1} \ : ќ
roll, name, mark);
}
}
class StudentTest
{
static void Main()
{
= њ ќ;
s1.Display();
Console.Read();
}
}
=- = њ ќ =
paramet
static constructor
cla
Code:
using System;
class Constant
{
private Constant()
{}
public static string DeveloperName = "Sevenlamp";
}
class Program
{
static void Main()
{
//Constant c = new Constant(); // error
Console.WriteLine(Constant.DeveloperName);
Console.Read();
}
}
static constructor
Code:
using System;
class Constant
{
private Constant()
{}
static Constant()
{
Console.WriteLine("print from static constructor");
DeveloperName = "Sevenlamp";
}
public static string DeveloperName;
}
class Program
{
static void Main()
{
Console.WriteLine(Constant.DeveloperName);
Console.WriteLine(Constant.DeveloperName);
Console.Read();
}
}
њ ќ њ ќ
њ
ќ
static member vs instant member
class
Code:
class Test
{
public static string clsVar; // static member
public string objVar; // instant member
public void Show() //instant member
{
Console.WriteLine(clsVar); // use class member from instant member
Console.WriteLine(objVar);
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
Test t2 = new Test();
t.objVar = "this is object member"; // call from object instant
Test.clsVar = "this is class member"; // call from class name
t.Show();
Console.WriteLine();
t2.Show();
Console.Read();
}
}
Code:
public static string clsVar; // static member
public string objVar; // instant member
Code:
Test t = new Test();
Test t2 = new Test();
Code:
t.objVar = "this is object member"; // call from object instant
Test.clsVar = "this is class member"; // call from class name
Code:
public void Show()
{
Console.WriteLine(clsVar); // call class member from object member
Console.WriteLine(objVar);
}
program
Code:
class Customer
{
string acno;
string name;
public Customer(string name)
{
this.acno = GetNewAcno(); //call static member function
this.name = name;
}
public override string ToString()
{
return acno + "-" + name;
}
property in C#
Code:
using System;
class Test_A
{
public int var;
}
class Program
{
static void Main()
{
Test_A obj = new Test_A();
obj.var = 1;
Console.WriteLine(obj.var);
Console.Read();
}
}
- data
hid
Code:
using System;
class Test_B
{
private int var;
public void SetData(int value)
{
var = value;
}
public int GetData()
{
return var;
}
}
class Program
{
static void Main()
{
Test_B obj = new Test_B();
obj.SetData(2);
Console.WriteLine(obj.GetData());
Console.Read();
}
}
member varia
Code:
using System;
class Test_C
{
private int var;
public int MyVar
{
get
{
return var;
}
set
{
var = value;
}
}
}
class Program
{
static void Main()
{
Test_C obj = new Test_C();
obj.MyVar = 3;
Console.WriteLine(obj.MyVar);
Console.Read();
}
}
Code:
using System;
class myNumber
{
private int num;
public string Num
{
get
{
return num.ToString();
}
set
{
try
{
num = Convert.ToInt32(value);
}
catch
{
num = 0;
}
}
}
public int Square
{
get
{
return num * num;
}
}
}
class Program
{
static void Main()
{
myNumber obj = new myNumber();
Console.Write("Enter a number : ");
obj.Num = Console.ReadLine();
Console.WriteLine("Square of {0} is {1}", obj.Num, obj.Square);
Console.Read();
}
}
oop
Inheritance
Code:
class People
{
string Name;
int Age;
string Address;
}
Code:
class Student
{
string Name;
int Age;
string Address;
int Roll;
int Class;
}
Code:
class Student : People
{
int Roll;
int Class;
}
-
-
-
-
-
Code:
class People
{
protected string Name;
protected int Age;
protected string Address;
}
Polymorphism
Code:
static void Main()
{
People p;
p = new Student();
}
Stude
Data Hiding
Encapsulation
Code:
static int ConvertToInteger(string st)
{
try
{
return Convert.ToInt32(st);
}
catch
{
return 0;
}
}
Code:
static int ConvertToInteger(decimal dec)
{
try
{
return Convert.ToInt32(dec);
}
catch
{
return 0;
}
}
static int ConvertToInteger(long l)
{
try
{
return Convert.ToInt32(l);
}
catch
{
return 0;
}
}
function name
over
Code:
class animal
{
public void eat()
{
Console.WriteLine("Eat something");
}
}
parent cla
Code:
class animal
{
public virtual void eat()
{
Console.WriteLine("Eat something");
}
}
override
Code:
class dog : animal
{
public override void eat()
{
Console.WriteLine("Eat Bone!");
}
}
Code:
class animal
{
public virtual void eat()
{
Console.WriteLine("Animal: Eat something");
}
}
class dog : animal
{
public override void eat() // function overriding
{
Console.WriteLine("Dog: Eat Bone!");
}
}
class ant : animal
{
public override void eat()
{
Console.WriteLine("Ant: Eat Sugar");
}
}
class Program
{
static void Main()
{
animal obj;
obj = new animal();
obj.eat();
Console.Read();
}
}