0% found this document useful (0 votes)
21 views

Visual C# Programming

C# is a programming language developed by Microsoft. It was initially released in 2002 as part of .NET framework. C# is influenced by other languages like C, C++ and Java. It is an object-oriented, type-safe and managed language. Some key points about C# are: it is compiled to CIL and runs on .NET framework virtual machine; it supports features like inheritance, polymorphism, abstraction etc; it has value types and reference types as data types. C# code is compiled using csc.exe compiler and can be executed on Windows, Linux and macOS platforms.

Uploaded by

Aung Ko Ko Zaw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Visual C# Programming

C# is a programming language developed by Microsoft. It was initially released in 2002 as part of .NET framework. C# is influenced by other languages like C, C++ and Java. It is an object-oriented, type-safe and managed language. Some key points about C# are: it is compiled to CIL and runs on .NET framework virtual machine; it supports features like inheritance, polymorphism, abstraction etc; it has value types and reference types as data types. C# code is compiled using csc.exe compiler and can be executed on Windows, Linux and macOS platforms.

Uploaded by

Aung Ko Ko Zaw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

HISTORY OF C#

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

hello world from C#


programming languag

[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

Value Type Reference Type

int num = 3;

int

string = њ ќ;

string

Type .................................... Range ......................................... Size


sbyte ................................ -128 to 127 ...................................... Signed 8-bit integer
byte ..................................... 0 to 255 ........................................Unsigned 8-bit integer
char ..................................... U+0000 to U+ffff ........................... Unicode 16-bit character
short .....................................-32,768 to 32,767 ........................ Signed 16-bit integer
ushort .................................0 to 65,535 ................................... Unsigned 16-bit integer
int ........................-2,147,483,648 to 2,147,483,647 .................... Signed 32-bit integer
uint .............................0 to 4,294,967,295 .................................. Unsigned 32-bit integer
long ......... -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807..... Signed 64-bit integer
ulong ..............0 to 18,446,744,073,709,551,615 ..............................Unsigned 64-bit integer

Type .........................Approximate range ..............Precision


В± 5 −45 В± 4 8 ....7 digits
В±5 − 4 В± 7 8 5-16 digits

Type .........................Approximate Range .................Precision


В± Г— − 8 В±7 9 Г— 1028 ..................28-29 significant digits

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

data types conversation

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

Data Type Conversation

type conversati

int int_var1 = 123;


long long_var1 = int_var1; // implicit conversation (widening)
long long_var2 = (long) int_var1; // explicit conversation (widening)
int int_var2 = (int) long_var1; // explicit conversation (narrowing)

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

sample program 1.01

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

selection control statement

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:

for( start; stop; step )


{
statements;
}
Example:
Code:

for( int i = 1; i<=10; i++ )


{
њ ќ;
}

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:

int[,] arr = new int[3,2];

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];

if (!((r1 == 1 && c1 == 1) || (r2 == 1 && c2 == 1) || (c1 == r2)))


{
Console.WriteLine("\nOut of result!");
Console.Read();
return;
}

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

ArrayList ar = new ArrayList();


ar.Add(1);
ar.Add(9.456);
ar.Add("ABC");
ar.Add('Z');
ar.Add(new arraylistTest());

for (int i = 0; i < ar.Count; i++)


{
Console.WriteLine("{0}\t( {1} )", ar[i], ar[i].GetType());
}
Console.WriteLine("\nActual Size of your Array : " + ar.Count);
Console.WriteLine("Total Capacity of your Array : " + ar.Capacity);
Console.Read();
}
}
*** ArrayList
Hashtable
Hashtable class key/value
integer type

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

foreach (DictionaryEntry de in ht)


{
Console.WriteLine("{0}\t\t: \t {1} ", de.Key, de.Value);
}
Console.WriteLine("\nThere are {0} element/s in your Hashtable Array", ht.Count);
Console.Read();
}
}

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
}

static void Main()


{
myOwnType wd = myOwnType.Friday;
int i = 7;
Console.WriteLine("{0} is {1}", wd, (int)wd);
Console.WriteLine("{0} is {1}", i, (myOwnType)i);
Console.ReadLine();
}
}
Output
Code:
Friday is 101
7 is Wednesday

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;

Console.WriteLine("\nRoll : {0} \nName : {1} \nMark : {2}", s1.roll, s1.name, s1.mark);


Console.WriteLine("\nRoll : {0} \nName : {1} \nMark : {2}", s2.roll, s2.name, s2.mark);
Console.WriteLine("\nRoll : {0} \nName : {1} \nMark : {2}", s3.roll, s3.name, s3.mark);
Console.Read();
}
}

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

structure test program


structure record
structure object-oriented programming
class
structure value type OO
structure reference type
class
OOP class class
data member member function
member data class
member function
class People class
(name) (age) member data
(eat()) (sleep()) member function
Code:
class People
{
string name; // member data
int age; // member data
void eat() // member function
{
// eat something…
}
void sleep() // member function
{
// sleep at night.
}
}
eop e ass atat pe ass
p og am ass
Code:
class Student
{
public int roll;
public string name;
public int mark;
public void Display()
{
Conso e.W iteLine(“\n Roll : {0} \n Name : {1} \n Ma k : {2}”,
roll, name, mark);
}
}
class StudentTest
{
static void Main()
{
Student s1 = new Student();
s1. o = 1; s1.name = “Ag Ag”; s1.ma k = 90;
Student s2 = new Student();
s2.ro = 2; s2.name = “Zw Zw”; s2.ma k = 88;
Student s3 = s2;
s3. o = 3; s3.name = “Ma Ma”;
// display
s1.Display();
s2.Display();
s3.Display();
Console.Read();
}
}
Output:
Code:
1 Ag Ag 90
3 Ma Ma 88
3 Ma Ma 88

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();
}
}
=- = њ ќ =

Student s2 = new Student(); // error code


= ;

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

public static int counter = 0;


public static string GetNewAcno()
{
counter++;
return "C" + counter.ToString("D3");
}
}
public class Program
{
static void Main()
{
Customer a = new Customer("Mg Mg");
Customer b = new Customer("Zw Zw");
Customer c = new Customer("Ag Ag");
Console.WriteLine("{0}", a);
Console.WriteLine("{0}", b);
Console.WriteLine("{0}", c);
Console.WriteLine("Customer Count is {0}", Customer.counter);
Console.Read();
}
}

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

function overloading vs function overriding


Function Overloading Vs Function Overriding

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

obj = new dog(); //polymorphism


obj.eat();

obj = new ant();


obj.eat();

Console.Read();
}
}

You might also like