Вы находитесь на странице: 1из 10

Arrays in C#

Declaration

<datatype> []<arrayname>=new <datatype>[<length>];

Ex.:

int []a = new int[10];

int []b = new int[20];

a
1 2 3 4 5 6 7 8 9 10

for(int i=0;i<=9;i++)
{
a[i] = (i*3);
}

a
0 3 6 9 12 15 18 21 24 27

Initializing array

int []arr = {1,2,3,4,5};

arr
1 2 3 4 5

string []month={"jan", "feb", "mar"};

month
jan feb Mar
string []month = new string[3];

Here (in bold) month is an array of three string references, which


do not have any memory for them. (here new creates array – all
compound variables are always created using new in C#).

Memory is allocated either by new operator or by direct value


assignment (which first allocates memory and then initializes it
with specified value)

month[0]="jan";
month[1] = "feb";
month[2] = "mar";

or
month[0] = new string();
month[1] = new string();
month[2] = new string();

or
for(int i=0;i<=2;i++) {
month[i] = new String();
}

or
month[0] = new string(“jan”);
month[1] = new string(“feb”);
month[2] = new string(“mar”);

string s; //it’s a reference


s=””; //gets memory
or
s=”PACE”; // also gets memory
2-D Arrays in C#

int [,]arr = new int [6,5];

arr

Initialization of 2-D Array

int [,]arr = {
{1,2,3},
{4,5,6},
{7,8,9}
};

1 2 3
4 5 6
7 8 9

Array of Objects:

class MyClass {
public int a;
public string s;
}

MyClass obj; //this is a reference of type MyClass


obj = new MyClass(); //allocated memory

MyClass []objarr = new MyClass[5]; //creates an array of 5 object reference


for class MyClass (red letters below)

for(int i=0;i<=4;i++) {
objarr[i] = new MyClass(); //(Black Letters below)
}

Ref1 Object1
4 byte int 10 byte string
Ref2 Object2
4 byte int 10 byte string
Ref3 Object3
4 byte int 10 byte string
Ref4 Object4
4 byte int 10 byte string
Ref5 Object5
4 byte int 10 byte string

Functions:

Calling a function

myf(3);

Defining function

void myf(int x)
{

Working with References

class MyClass
{
int a,b,c;
}

class YourClass
{
MyClass ref; // It is a reference will not get any memory
void myf()
{
MyClass a1 = new MyClass();
ref=a1;
//ref = new MyClass(); //memory allocation may be
done here
//a1 = new MyClass(); //allowed
}
}

System Area (conventional Memory) (640KB)

Heap (Free Area) (384KB )(expandable)

A1
ref ref
A1
Exte

Ex.

object obj;
string str="pace bureau";
obj=(object) str;

//object of any class can be assigned onto a object reference


//using type conversion

string m=(string)obj;

Enumerators
enum gender
{
male=1,
female=10
};

Enum System.Drawing.Color {
Red = 0xFF000,
Green = 0x00FF00,
----------
};

enum gender g;

g =1; // gender.male;

int t = (int) gender.male; // t=1

int t = (int) gender.female; // t=2

FOREACH LOOP

foreach(ListItem x in dropdownlist1.items)
{
x.Text="r"+x.Text;
}
int []a = new int[10];
Foreach(int t in a) {
-----
}

e.g.
string temp=””;
string temp1=””;
for(int I=0;I<=dropdownlist1.items.count-1;I++) {
temp = dropdownlist1.items[I].Text;
temp1 = dropdownlist1.items[I].Value;
response.write(temp);
}
foreach(ListItem item in dropdownlist1.items) {
temp = item.Text;
temp1 = item.Value;
response.write(temp);
}

foreach(object ref in MyCollection) {


if(ref is instanceof TextBox ) {
}
Else if(ref is instanceof CheckBox) {
}
------
}

Namespaces in C#
Namespace is a collection of classes which work as a library for
other programs. They are used for creating a new scope where
different visibilities can be achieved. We can create our own
namespace as follows
namespace <name of namespace>
{
Classes..
}

Using namespaces:
For using the namespace in a program we use the “using”
keyword as follows:

using <name of namespace>;

Ex.
namespace myns
{
public class Calculator
{
double a,b,c;
public void get(int a1, int b1)
{
a=a1;
b=b1;
}
-------
}
-----------
public namespace myns1
{
------
}
}

//code to include calculator class (In Java)


using myns.Calculator;
//this will not work in C#

//code to include all the classes in myns (In C#)


using myns;

//code to include classes in myns1


using myns.myns1;
Garbage Collection:

Is a process of collecting the memories allocated using new


keyword. Developer don’t require to de-allocate the memory
allocated using new operator. C# provides Garbage Collector
utility for the same and it works in the background. It is
automatically invoked by CLR, whenever required by the system.

Exception Handling in C#:

Exception is an object which is thrown (or raised) whenever any


unwanted situation occurs in the program. This is normally an
error but can also be any other values which perform unwanted
actions. (E.g. age value 200 or check constraint for some specific
value set).

Exception Handling: Is a mechanism of handling exceptions


whenever thrown by the program segment. For exception
handling we need to create a handler section which will provide
the necessary code to handle the exception. In C#, we have
following try block for exception handling. (Keywords used are
try, catch, throw, throws, finally)
try
{
-----
}
catch(Exception ex)
{
-------
}
Ex.
void myf(){
int a,b;
a=1;b=0;
try
{
if(b==0)
{
IOException t = new IOException(“Divide by
zero”);
throw t;
}
Response.Write(“Output is :” + a/b);
}
catch(IOException ex){
Response.Write(“Error :”+ex.toString());
}
finally
{
Response.write("PACE BUREAU");
}
//Response.write("PACE BUREAU");
}

Вам также может понравиться