Sie sind auf Seite 1von 8

C#.NET C# with Network Enabled Technology INTRODUCTION C#.

Net is a modern objec


t oriented programming language. Which introduced by Microsoft Corpration ?Very
similar to Java because ?70% Java
?10% C++
?
5% Visual Basic ? 15% new ?C#.Net version is 2.0
?It s belong to C family
?C#.Net is case sensitive
?C# .Net is architecture by HEZLSIBERG
?C#.Net is highly type safe language
?EXAMPLE VB:
Dim sal As Integer
msgbox(sal)
Output: zero ?C# Int sal
Sal=10
MessageBox.Show(sal)
Output : Error
?C#.Net is highly secured
?Every C# program must contain a main() method
?All these statement must be terminated with ;
?C# programs will be executed with the CSC compiler
Structure of C# Program Program File F1.cs File F2.cs File F3.cs Namespace A{ } N
amespace B{ } Namespace C{ } Class x{ } Class x{ } Class x{ } ?If no namespace is specifie
d => anonymous default namespace ?Namespaces may also contain structs, interface
s, delegates and enums ?Namespace may be "reopened" in other files Predefined D
ataType in C#.Net ?Integral Data type
?Floating Data Type
?Character Data Type
?Other Data Type
?Integral Data type
?Byte(1B)
?SByte(1B)
?Short(2B)
?UShort(2B)
?Int(4B)
?Uint(4B)
?Long(8B)
?ULong(8B)
oat ng o nt aracter eate t er ata ype ? Float (4B) Char (2B) Bool (1B) (True /
False) ? Double (8B) String (16 B to 2 billians) Date(2B) (DD/MM/YYYY) (EXAMPLE)
?Button Click Event ?? int sal=10; ? MessageBox.Show(sal.ToString()); ? float x
= 4.0f; ? MessageBox.Show(x.ToString()); ? byte b1 = 1, b2 = 0; ? int c = b1 +
b2; ? MessageBox.Show(c.ToString()) ?Messagebox.show() is capable to print only
string
?All the value types must be initialize before using
?While initializing float variable , value must be post fixed either
with f or F ?EX: float x=4.0; (Error) float x=4.0f; (ok) Reason: By default C# t
reats a number with decimal value as double. Type Casting int i=10; ---? 4B Sh
ort s=10; --?2B Long l=I; (ok) --? 8B Byte b=s; (Error) -? 1B Type casting is th
e concept of converting one Data Type into another datatype. C# support two type
of TypeCasting 8. Implicit TypeCasting(Convert from Lower to higher, It is unde
r control of CLR) 9. Explicit TypeCasting(Convert from higher to lower, Its unde
r control of programmer) ?Four Types 1. C++ style
2.Parsing
3.Converting
4.Boxing and UnBoxing
EXAMPLE: (C++ Style of Typecasting) int i=10; byte b= (byte) i ; Note: Draw back
of C++ TypeCasting. There is possibility for lossing original data Working Wit
h Parsing ?As per DotNet all predefined data types are predefined structures. ?S
tructures is a collection of methods (function) ?Maxvalue return the max capicit
y and minvalue return min capacity of a datatype. ?Working with parse() is calle
d as parsing. ?Parsing can be used to convert string into another datatypes only
String--? int.Parse() String--? byte.Parse() Data Type Max value Min value Pars
e() ToString() Ex 1: For printing the Limit of datatype MessageBox.Show(int.Min
Value.ToString()); MessageBox.Show(int.MaxValue.ToString()); Ex 2: On Parsing: p
rivate void button1_Click(object sender, EventArgs e) {
MessageBox.Show(txt1.Text + txt2.Text);
int sal = int.Parse(txt1.Text);
int inc = int.Parse(txt2.Text);
int total = sal + inc;
MessageBox.Show(total.ToString());
} ?Working with Converting: Converting can be used convert from any datatype i
nto any other datatype. Methods of Convert Class: Convert. ToChar(value)
Convert.ToByte(value)
Convert.ToBoolean(value)
Convert.ToString(value)
Convert.ToDateTime(value)
Convert.Toint16(value)
Convert.Toint32(value)
Convert.Toint64(value)
Ex: On_Converting(Button Click Event) int i = 97;
char c = Convert.ToChar(i);
MessageBox.Show(c.ToString());
Boxing and UnBoxing ?Converting value type into reference type is called as box
ing.
?Converting reference type into value type is called as unboxing
?BOXING
int i = 10;
object str = i;
MessageBox.Show(str.ToString());
?UNBOXING ? object str = 10; ? int i = (int)str; ? MessageBox.Show(i.ToString())
; Working With Condition Statement ?Sytax for IF Condition if(condition) Statem
ent 1; else Statement 2; if (condition) {
Statement 1;
Statement 2;
} else {Statement 1; Statement 1; } xampe n on ton: ?A program to compare two n
umber (i,j), If i is greater means display from back color with green otherwise BG
color with red color. ?Code for button click event ?CLR creates one object to fo
rm automatically at runtime with the name this . ?This is a keyword. ?C# Contains a
round 61 keywords. int i = 10, j = 5; if (i > j) this.BackColor = Color.Green; e
lse this.BackColor = Color.Red; Working With Switch ?SYNTAX: Switch(var) { Case
1: ---------;
break;
Case 2:
---------;
break;
default:
----------; break; } int i = int.Parse(textBox1.Text);
switch (i)
{
case 1: {
this.BackColor = Color.Red;
break;
} case 2: {
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
this.BackColor = cd.Color;
break;
} case 3: {
Application.Exit();
break;
} default: {
MessageBox.Show("Enter 1,2 (or) 3");
break;
} } ?Application is a predefined class , which is used to terminate a project o
r Execute a project. LOOPS: Loops are execute a set of statement repeatedly base
d on a condition. LOOPS For While Do_While for(intialization; condition; ++/--;)
While(condition) do { { ---------; ------; FOR LOOP (A Program to print from A
to Z) ?Ex1: for (int i = 65; i <= 90; i++) {
char c = Convert.ToChar(i);
MessageBox.Show(c.ToString());
} ?Ex2:int x = 10; for (int i = 1; i <= 10; i++) {
TextBox t = new TextBox();
t.Location = new Point(100, x);
this.Controls.Add(t);
x = x + 30;
FOR EACH ?Suntax foreach(object in collection) {------; } Note: Collection is a
group of similar type of objects. Ex:place n textboxes and buttons on the form,
when clicked on a button change all textbox backcolor to green. Code for Button
Click Event: foreach (Control c in this.Controls) { string s = c.GetType().Name
; if (s == "TextBox") c.BackColor = Color.Green; else Working With Array: ?Arra
y is a collection of similar DataType ?Array will be stored in continuous memory
. ? x[0] x[1]---- Subscription Index Operator ? Array says same name with differ
ent Index no
-? Always index start from zero.
-? Arrays are belongs to reference type.
-? As per DotNet array are instance of the predefined class
called as System.Array -? C# support three types of Array.
1. One Dimensional Array.
2 Two Dimensional Array.
3. Jagged Array.
10 20 30 40 50 Working With One Dimentional Array: ?Syntax: int[] x = new int[
size]; ?Ex: int[] x = new int[5]; ?Above statements creates five elements from x
[0],x[1],x[2],- - - x[4]; ?Arrays holds default value ?Syntax to Initialize Arra
y: int x = new int[] {10,20,30}; ?Note: ?While initializing array size is not re
commended to specifiy Button Click Event: ?int[] x = new int[3] ; MessageBox.Sh
ow(x.Length.ToString());
for (int i = 0; i < x.Length ; i++)
{
MessageBox.Show(x[i].ToString()); } Note:Integral default value is Zero. Floatin
g Default Value is Zero.
Bool default value is false.
Char default value is Null.
DateTime default value is -?1/1/0001 12:00:00AM
?Ex2:( A program to display the spelling of a given number(1 to 999) Form Desig
n TextBox 1 TextBox 2 Button 1 Code For Button1_Click() {String[] ones = new st
ring[] { "one", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten",
"Elevn", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen" };
String[] tens = new string[] { "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Si
xty", "Seventy", "Eighty", "Ninty" };
int n, i;
string result = " ";
n = int.Parse(textBox1.Text);
if (n > 99 && n < 1000) {
i = n / 100;
result = ones[i - 1] + "hundred";
n = n % 100;
}if (n > 19 && n < 100) {
i = n / 10;
result = result + tens[i - 1] + "";
n = n % 10;
}if (n < 20 && n > 0) { result = result + ones[n - 1]; }textBox2.Text = result;
} Working With Multi_D Arrays: x.getlength(0) --? Rows x.getlength(1) --? Colum
ns ? Arranging a set of values in rows and columns is called as Multi_D Arrays.
? Every array contains size which will be written as (no.of .rowsx no.of.columns
). ? Every rows must contains same no.of .elements. ? Every columns must contain
same no.of.elements. 01 2 0 1 2 x Size : 3x3 4. Syntax to declare: 1_D int[] x
= new int[5] 2_D int[,] x = new int[5,5] 3_D int[, ,] x = new int[5,5,5 ] 5. In
C# There is no limits for no.of.elements, where as VB.Net support only 32 Dimen
tions. 6. Syntax to Initialize 2D Arrays: int[,] x = new int[,] { {1,2,3,4},{5,6
,7,8},{9,10,11,12} }; Ex: 2D Arrays: Code for Button Click Event: {
int[,] x = new int[,]
{
{1,2,3,4},{5,6,7,8},{9,10,11,12}
};
MessageBox.Show("Total: " + x.Length.ToString());
MessageBox.Show("Rows: " + x.GetLength(0).ToString());
MessageBox.Show("Columns: " + x.GetLength(1).ToString());
String s = "";
for (int r = 0; r < x.GetLength(0); r++)
{
for (int c = 0; c < x.GetLength(1); c++) { s = s + " " + x[r, c].ToString(); }s
= s + "\n"; }MessageBox.Show(s); } Jagged Array (JA): ?Jagged Array (JA) is als
o called as dynamic array.
?JA is also called as array of arrays.
?JA array saves memory.
?JA are faster in Data accessing.
?In JA , each and every row may have discreate no.of.elements
?Syntax to declar JA int[ ] [ ] x = new int[rows] [ ];
x[0] = new [ ] {1,2,3,4};
x[1] = new [ ] {5,6,7};
x[2] = new [ ] {8,9};
Example For Jagged Array {
int[][] x = new int[3][];
x[0] = new int[] { 1, 2, 3, 4 };
x[1] = new int[] { 5, 6, 7 };
x[2] = new int[] { 8, 9 };
string s = "";
for (int i = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x[i].Length; j++) { s = s + " " + x[i][j].ToString(); }s = s
+ "\n"; }MessageBox.Show(s); ?ENUMARATOR(ENUM): Enum is a keyword.
Enum is a collection of integer constant.
Float values are not allowed in Enum.
By using integer constant can be maintain easily.
SYNTAX: Enum <name> {
value 1;
value 2;
value 3;
::Value n
} ?Enum must be declare in general Declaration Area. ?Example For Enum: namespa
ce Enum1 {
public partial class Form1 : Form
{
public Form1() { InitializeComponent(); }enum GPT {
ne = 21,
rel = 27,
fr = 12
} private void button1_Click(object sender, EventArgs e) { int i, j, k;
i = (int)GPT.ne;
j = (int)GPT.rel;
k = (int)GPT.fr;
MessageBox.Show(i.ToString() + " " + j.ToString() + " " +
k.ToString()); } } }Example For Enum Array OOPSLan guage: INTRODUCTION ?In 198
3 ANSI (American National Standard Institute) to introduce a new concept is call
ed as oops concept. ?If a language follow these oops concept ,then it is called
as OOPL (Object Oriented Programming Language ?PROBLEM IN C LANGUAGE ?1. Main() PR
OBLEM: { (No boundaries for Datatypeint sal = 400000; practically) printf( %d ,sal);
} ?2. Main( ) PROBLEM: { (No proper machanism for int I = 500 * 500/500; calcu
lation) printf( %d ,i); } 3. Long sa = 5000; PROBLEM: Main( ) { } ( No security for
data)
f1( ) { }
f2( ) { }
f3 ( )
{
long scale = 500000; sal = sale+sale; } CONCLUTION: ?To overcome above problems
, ANSI defined a set of rules called as OOPS, ?When a Language follows these rul
es,then it is called as OOPL ?C# USING OOPS CONCEPTS: ?When ever a Language supp
orts following rules then it is called as OOPL. ?Encapsulation
?Abstraction
?Polymorphism
?Inheritance
?Encapsulation: It is a concept of data hiding ?Abstraction: Providing full inf
ormation about an entity ?Polymorphism: Writing more then one function .Polymorp
hism is a concept of providing many functionality with single name. ?Inheritance
: It is a concept of deriving the features from one class into another class. ?C
++ (It support above 4 concept but partial) ?VB.Net (It support above 4 concept
but partial because, MsgBox It is not a class and object) ? C#.Net (It support fu
ll concept of oops, so it is a pure OOPL) ? ASP.Net (It is an object oriented se
rverside specification. To support above four rules class and object are require
d) ? What is a Class? ? General: Class is a logical representation of a physical
Entity. ? DotNet:Class is a collection of fields, properties, methods and event
. ? Primary data of a class is called as fields. ? Properties defined a shape of
an objects. ? Methods -? what an object can do. ? Event -? what user can dowith
an object. ? Instance of a class is called an objects. ?SYNTAX TO DECLARE A CL
ASS: ?Class <class _Name> {private int x,y; private string s; public void print
( ) { int i; } } ?SYNTAX TO CREATE AN OBJECT: ?<class_Name> obj = new < class-Na
me > ( ); Field (reference variable) Default value is zero No default value Valu
e type ?Example 1: class sample {}private void button1_Click(object sender, Eve
ntArgs e) {
sample s = new sample();
MessageBox.Show(s.GetType().Name);
MessageBox.Show(s.GetHashCode().ToString());
} ?In C# by default every class with the inherited from system.object class ?Get
Type --? It provides meta data of an object.
?GetHashCode --? Starting reference in Heap memory
?Equals --? For comparing two objects.
?ToString --? Convert into tostring
?After property name { is not required.
?After method name ( ) is required.
General declaration ?Class with method and fields: Class {Private int i; Privat
e void mint( ) {---- ---- }Test t = new test( ) ?Object can access public data a
nd object can not access private data, it is called encapsulation. ?Example: ?Co
de for GD: Class test {Private int i,j; public void read(int x,int y) {
i=x;
j=y;
}public void prin() { int k=i+j; MessageBox.Show(k.ToString()); }
Code for button click:
private void button1_Click(object sender, EventArgs e)
{
test t1 = new test();
test t2 = new test();
t1.read(5, 6);
t2 = t1;
t1.prin(); t2.prin(); } ?Object is also called instance variable ?For every obj
ect ,a separate set of instance variable will be created. And a variable is unde
r control of object , then it is called object variable. ?One object can be assi
gned to another object provided both must be same type. ?Using THIS keyword: ?This
is a predefined keyword which is under control of CLR.
?This keyword works like an object for current class.
?When instance variable and local variable names are same ,then
by default priority will given local variables. ?If the name are different then
this keyword is optional. ?Example Code for GD:
Class emp
{
public void increment(int sal) {
MessageBox.Show(sal.ToString());
MessageBox.Show(this.sal.ToString());
this.sal = this.sal + sal;
}public void print() { MessageBox.Show("Total : " + sal.ToString()); } } private
void button1_Click(object sender, EventArgs e) {
emp t = new emp();
t.increment(3000);
t.print();
} ? Passing Parameter to a Function: ? In C# parameter can be passed in three w
ays 3. Pass by value [call by value] 4. Pass by ref [call by ref] 5. Pass by out
[call by out] ? Example: class test {
public void add(int x)
{
x = x + x; } }private void button1_Click(object sender, EventArgs e) {
test t = new test();
int sal = 5000;
t.add(ref sal);
MessageBox.Show(sal.ToString());
} Formal Argument ?When formal args are modified ,if modification are reflected
on actual args then the concept is called as pass by references. ?When formal a
rgs are modified , if modifications are not reflected on actual args then the co
ncept is called as pass by value. ?By default all the variable will be passed by
values
? Ref is a keyword.
?To pass a variable by reference, ref keyword is required.
? Ref keyword must be used along with actual and formal args.
?Ref variable must be initialize before passing.
?Example: class test { public void swap(int x, ref int y) {
int t = x;
x = y;
y = t;
} } ? private void button1_Click(object sender, EventArgs e) {
int a = 10, b = 20;
test t = new test();
t.swap(a, ref b);
MessageBox.Show(a.ToString() + " " + b.ToString());
} ?Pass By Out:
?Out is a keyword .
?Out keyword must be used along with actual and formal args.
?Out is 99% same as ref.
?Out = ref initialization.
?Summary:
?Call by value : value will be passed not address.
?Call by reference : Address and value will be passed.
?Call by out : only address will be passed.
?Example: class test {
public void add(int x, int y, out int z)
{
z = x + y; } }private void button1_Click(object sender, EventArgs e) {
int a = 10, b = 20, c;
test t = new test();
t.add(a, b, out c);
MessageBox.Show(c.ToString());
} ?Note: ?One function can have any number of out parameters as well as ref para
meters.
C#.Net

Das könnte Ihnen auch gefallen