• No results found

Features new in C# 2.0 and 3

In document C# (Page 166-176)

1) In 2.0 version we were given with an option to set the scope of each Property accessors independently. So that both the blocks can be having different scopes.

Eg:- state property we have defined above.

2) In C# 3.0 version we were given with a feature automatic property features, which allows us to define a property that doesn't require any variable and also the 'get' and 'set' blocks doesn't require

any codes also, but in case of an automatic property it is must the property should define with both 'get' and 'set' blocks.

Eg: - Country property in our above code.

Consuming the Properties we have define

To Consume the properties add a new class "TestCustomer.cs" and write the following.

class TestCustomer

{

staticvoid Main() {

Customer obj = new Customer(101);

Console.WriteLine(obj.Custid);

//Assignment cannot be performed as property in ReadOnly //obj.Custid=102;

Console.WriteLine(obj.Cname); obj.Cname = "Mr.Praveen";

Console.WriteLine(obj.Cname);

//Balance will be zero as status is inactive Console.WriteLine(obj.Balance);

Console.WriteLine(obj.Status); obj.Status = true;

//Now we can access balance as status is set as Active Console.WriteLine(obj.Balance);

//Assignment fails as balance can't be less than 500

obj.Balance = 400; Console.WriteLine(obj.Balance); obj.Balance = 1000; //Assignment is Succeeds Console.WriteLine(obj.Balance); Console.WriteLine(obj.City);

obj.City = Cities.Mumbai;

Console.WriteLine(obj.City);

Console.WriteLine(obj.State);

//Assigning is not possible because current class is not a child class of Constructor //obj.State="UP"; //Invalid Console.WriteLine(obj.Country); Console.ReadLine(); } } Indexers

1) These are also very much similar to a 'property' that are used for providing access to values of a class, where properties will be used for accessing of Scalar values like int,float,string etc…, where as indexers will used for providing access to Arrays of a class.

2) We define a Indexer very much similar like a property but an Indexer will not have any name. We use 'this' keyword as a name to Indexer.

3) After defining an Indexer the object of class starts behaving like an array providing access to values of the Array present inside it.

*Add a class "IndexerDemo.cs" and write the following.

class IndexerDemo

{

//Declaring a private Array string[] arr;

public IndexerDemo(int size) {

//Initializing the Array under Constructor

arr = newstring[size];

//Assigning default values to Array for (int i = 0; i < size; i++) arr[i] = "Empty";

}

//Declaring Indexers to provide access for array values outside the class public stringthis[int index]

{

get { return arr[index]; }

set { arr[index] = value; } }

}

*Add a class "TestIndexer.cs" and write the following

class TestIndexer

{

staticvoid Main() {

IndexerDemo obj = new IndexerDemo(6);

for (int i = 0; i < 6; i++)

Console.WriteLine(obj[i] + "");

Console.WriteLine(); obj[0] = "India"; obj[2] = "USA";

obj[4] = "Hyderabad";

for (int i = 0; i < 6; i++)

Console.WriteLine(obj[i] + "");

Console.ReadLine(); }

Delegates

1) A 'delegate' is a pointer to a method, which can be used for invoking a method.

2) These are similar to the concept of Function pointer; we use in our traditional 'C' and 'C++' languages that are used for invoking functions.

3) A Method that is defined in a class can be invoked in two different ways.

1) Using object of a class we can invoke a method. 2) Using a Delegate also we can invoke a Method.

Note:-Invoking a method using delegate is faster in execution, when compared with invoking a

method using object of the class and we used this process, when we want to invoke a method for more number of times.

Using Delegates

If we want to use a 'delegate' for invoking a method follow the below process. Step1:- Delegate Declaration

As a delegate is type, it should be declared or defined first as following. [<modifiers>] delegate <void|type> <Name> {[<param def's>]}

A delegate declaration also contains IO parameters in it just like a method declaration, where the IO Parameters of delegate should exactly be same as the IO Parameters of Method it has to call.

Eg:-

public void Add(int x,int y) {

Console.WriteLine(x+y); }

public delegatevoid AddDel(int x,int y);

public stirng SayHello(string name) {

return"Hello "+name; }

public delegatestring SayDel(string name);

Step2:- To consume the delegate we have defined, we have to create object of delegate. Because it is a type, while creating the object we need to pass the method name, which it has to call as a parameter to the delegate constructor.

Eg:-

AddDel ad=new AddDel(Add); SayDel sd=new SayDel(SayHello);

Step3:- Now call the delegate, so that the Method which is bound with the delegate gets executed.

Eg:-

ad(332,32);ad(343,23);ad(43,43); sd("xxx");sd("yyy");sd("zzz");

Note: - A delegate type can be defined either with in a 'class' or within a 'namespace' also.

*Add a class "DelDemo.cs" and write the following

class DelDemo

{

public string SayHello(string name) {

return"Hello " + name; }

public delegatestring SayDel(string name);

staticvoid Main() {

DelDemo obj=newDelDemo();

SayDel sd = new SayDel(obj.SayHello);

Console.WriteLine(sd("Praveen\n"));

Console.WriteLine(sd("Prem\n"));

Console.WriteLine(sd("Mogli\n"));

Console.ReadLine(); }

}

Delegates are of two types

1) SingleCast (or) UniCast Delegates 2) MultiCast Delegates

If a delegate is used for invoking of a single method it is referred as a UniCast Delegate, where as if a delegate is used for invoking of multiple methods, we called them as MultiCast Delegates.

Note: - If we want to invoke Multiple Methods using a single delegate all those methods should have the same IO parameters.

*Add a class "MultiDelDemo.cs" and write the following.

public delegatevoid Math(int x,int y);

class MultiDelDemo

{

public void Add(int x, int y) {

Console.WriteLine("Add:" + (x + y)); }

public void Sub(int x, int y) {

Console.WriteLine("Sub:" + (x - y)); }

public void Mul(int x, int y) {

Console.WriteLine("Mul:" + (x * y)); }

public void Div(int x, int y) {

Console.WriteLine("Div:" + (x / y)); }

staticvoid Main() {

MultiDelDemo obj = new MultiDelDemo();

Math m = new Math(obj.Add); m += obj.Sub; m += obj.Mul; m += obj.Div; m(100, 100); //Invokes 4 methods Console.WriteLine(); m(10, 10); Console.WriteLine(); m -= obj.Add; m -= obj.Div; m(33, 32); Console.WriteLine(); Console.ReadLine(); }

}

Note: - In case of Multicast delegate using a Single delegate call, it will be possible for invoking multiple methods with the same parameter values.

Anonymous Delegates (or) Methods

It is a new feature that has been added in C# 2.0, which allows us to define a delegate without any method, that is whatever we want to write under the method, can be directly written under delegate only while creating its object.

*Add a class "AnonymousMethods.cs" and write the following.

class AnonymousMethods

{

public delegatestring SayDel(string name);

staticvoid Main() {

SayDel sd = delegate(string name) {return"Hello " + name;};

Console.WriteLine(sd("Praveen"));

Console.WriteLine(sd("Prem"));

Console.WriteLine(sd("Raj"));

Console.WriteLine(sd("Mogli"));

Console.ReadLine(); }

Exceptions and Exception Handling

With an application we may be coming across two different types of errors 1) Compile time error

2) Runtime error

An error which come into picture at the time of compilation of a program is referred as compile time error. These errors may come into picture due to Syntactical mistakes and they are not considered to be dangerous.An error which comes into picture while execution of a program are Runtime errors. This can be called as 'Exception' also. A Runtime error will come into picture due to various reasons like wrong implementation of the logic, wrong input supply to a program, missing of required resources etc...

Runtime errors are going to be dangerous, because whenever a runtime error occurs in a program the program terminates abnormally on the same line, where Exception occurred without executing the next lines of code, even if the code is no way related with the exception that has occurred.

Note: - Whenever an exception occurs in a program and the program is getting terminated abnormally it displays the reasons for abnormal termination of the program.

*Who is responsible for abnormal termination of a Program whenever an exception occurs in the program?

-> Whenever Runtime errors occur in a program, our program will be abnormally terminated by displaying an related error message. These two responsibilities are taken by some predefined classes known as 'Exception Classes'Relating with each different type of runtime error we have been provided with different exception classes defined under 'System namespace. Every exception class is responsible for two things.

1) Abnormal Termination of the Program

2) Displaying of error message that is related with the exception that has occurred.

Exception classes are implemented as following.

In document C# (Page 166-176)

Related documents