C#.

NET C# with Network Enabled Technology

INTRODUCTION
C#.Net is a modern object 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{…} C{…} Class x{…}

Namespace B{…}

Namespace

Class x{…}

Class x{…}

  

If no namespace is specified => anonymous default namespace Namespaces may also contain structs, interfaces, delegates and enums Namespace may be "reopened" in other files

Predefined DataType 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)

        

Floating Point
Float (4B) False)  Double (8B) (DD/MM/YYYY) (EXAMPLE)

Character Related
Char (2B)

Other Data Type
Bool (1B) (True /

String (16 B to 2 billians) Date(2B)

 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# treats a number with decimal value as double.

Type Casting
int i=10; Long l=I; (ok) --- 4B -- 8B Short s=10; Byte b=s; (Error) --2B - 1B

Type casting is the 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 under control of CLR) 9. Explicit TypeCasting(Convert from higher to lower, Its under 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 With Parsing
 

As per DotNet all predefined data types are predefined structures. Structures is a collection of methods (function)

Max value Data Type Min value
  

Parse()

ToString()

Maxvalue return the max capicity and minvalue return min capacity of a datatype. Working with parse() is called as parsing. Parsing can be used to convert string into another datatypes only String-- int.Parse() String-- byte.Parse()

Ex 1: For printing the Limit of datatype MessageBox.Show(int.MinValue.ToString()); MessageBox.Show(int.MaxValue.ToString()); Ex 2: On Parsing:

private 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 into 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 boxing. 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) Statement 1; else Statement 2; if (condition) { Statement 1; Statement 2; } else { Statement 1; Statement 1; }

Example On If Condition:

A program to compare two number (i,j), If “i” is greater means display from back color with green otherwise BGcolor with red color. Code for button click event
int i = 10, j = 5; if (i > j) this.BackColor = Color.Green; else this.BackColor = Color.Red;

  

CLR creates one object to form automatically at runtime with the name “this”. This is a keyword. C# Contains around 61 keywords.

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 or Execute a project.

LOOPS: Loops are execute a set of statement repeatedly based on a condition. LOOPS

For Do_While
for(intialization; condition; ++/--;)

While 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:
 

Array is a collection of similar DataType Array will be stored in continuous memory.
10 20 30 40 50

x[0] x[1]- - - -

Subscription Index Operator  Array says same name with different 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.

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 Array: int x = new int[] {10,20,30}; Note: While initializing array size is not recommended to specifiy

 

Button Click Event:

int[] x = new int[3] ; MessageBox.Show(x.Length.ToString()); for (int i = 0; i < x.Length ; i++) { MessageBox.Show(x[i].ToString()); }

Note: Integral default value is Zero. Floating 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 Design

TextBox 1

TextBox 2

Button 1

Code For Button1_Click() { String[] ones = new string[] { "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", "Sixty", "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 0 1 2 0 1 2 Size : 3x3

   

x.getlength(0) -- Rows x.getlength(1) -- Columns 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 .rows no.of.columns). Every rows must contains same no.of .elements. Every columns must contain same no.of.elements.

x

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 Dimentions. 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 also 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: namespace 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

OOPS Language:

INTRODUCTION
 

In 1983 ANSI (American National Standard Institute) to introduce a new concept is called 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() { Datatype int sal = 400000; printf(“%d”,sal); } PROBLEM: (No boundaries for practically)

2. Main( ) { for int I = 500 * 500/500; printf(“%d”,i); } 3. Long sa = 5000; Main( ) { } f1( ) { } f2( ) { } f3 ( ) { long scale = 500000; sal = sale+sale; }

PROBLEM: (No proper machanism calculation)

PROBLEM: ( No security for data)

CONCLUTION:
 

To overcome above problems, ANSI defined a set of rules called as OOPS, When a Language follows these rules,then it is called as OOPL


    

C# USING OOPS CONCEPTS:
When ever a Language supports following rules then it is called as OOPL. Encapsulation Abstraction Polymorphism Inheritance

Encapsulation:
It is a concept of data hiding

Abstraction:
Providing full information about an entity

Polymorphism:
Writing more then one function .Polymorphism 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 full concept of oops, so it is a pure OOPL) ASP.Net (It is an object oriented serverside specification. To support above four rules class and object are required) 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 CLASS:
Default value is zero Class <class _Name> { private int x,y; Field (reference variable) private string s; public void print ( ) No default value { int i; Value type } } SYNTAX TO CREATE AN OBJECT:

<class_Name> obj = new < class-Name > ( );

Example 1: class sample { General declaration } private void button1_Click(object sender, EventArgs 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  GetType -- 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.

Class with method and fields: Class { Private int i; Private void mint( ) { ------} Test t = new test( )
 

Object can access public data and object can not access private data, it is called encapsulation.

Example:  Code 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 object ,a separate set of instance variable will be created. And a variable is under control of object , then it is called object variable. One object can be assigned 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 ways 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) { Formal Argument 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()); }

       

When formal args are modified ,if modification are reflected on actual args then the concept is called as pass by references. When formal args are modified , if modifications are not reflected on actual args then the concept 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 parameters.

Sign up to vote on this title
UsefulNot useful