This chapter continues from the earlier chapter thus if you have not read it. please do so before proceeding further.
Constructor can have arguments too as shown in the above program, the second constructor accepts an argument to initialize the studentage property with the given value.
Constructor
These are special methods which get called at the time of object creation automatically. They cannot be called later by the user.. Constructors allow the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.using System; /** * author Sudeep Mukherjee * description Basic program to show the usage of a constructor * * */ namespace MySamples { class Student { public string name; private int age; public float hindi, english, maths; public int studentage { get { return age; } set { if (value < 0 || value >= 100) { age = 1; } else { age = value; } } } public Student() { studentage = 30; } public Student(int stAge) { studentage = stAge; } } class RunSample { static void Main(string[] args) { Student obj1 = new Student(); Console.WriteLine("Student age is {0}", obj1.studentage); Console.ReadKey(false); } } }the above program declares a constructor which initializes the studentage property with the value of 30 , thus if we print the property then instead of 0 which is the default value, it will show 30
Sample Output
Constructor can have arguments too as shown in the above program, the second constructor accepts an argument to initialize the studentage property with the given value.
Destructor
Destructor run just before the instance of the class is destroyed. They are used for cleanup routines and are called automatically. Some of the features of a destructor are- A class can only have one destructor.
- Destructors cannot be inherited or overloaded.
- Destructors cannot be called. They are invoked automatically.
- A destructor does not take modifiers or have parameters.
class Sample { ~Sample() { //cleanup code } }
this keyword
The this keyword refers to the current instance of the class. It is mainly used to distinguish variables hidden by similar names or passing the current instance as an argument to a method.Following program shows the three uses of this keyword. First it uses it as object to refer to the class member studentage. Next it uses to call the constructor with arguments to initialize the studentage property. Lastly it passes the current instance of the class to a function called calcGrade.using System; /** * author Sudeep Mukherjee * description Basic program to show the usage of a this keyword * * */ namespace MySamples { class Student { public string name; private int age; public int hindi, english, maths; public int studentage { get { return age; } set { if (value < 0 || value >= 100) { age = 1; } else { age = value; } } } public Student() :this(30) { } public Student(int studentage) { this.studentage = studentage; } public char getGrade() { char answer = ExternalClass.calcGrade(this); return answer; } } class ExternalClass { public static char calcGrade(Student obj) { char answer='a'; int tot1=obj.hindi+obj.english+obj.maths; if (tot1 >= 90) answer = 'a'; else if (tot1 >= 65 && tot1 < 90) answer = 'b'; else if (tot1 < 65) answer = 'c'; return answer; } } class RunSample { static void Main(string[] args) { Student obj1 = new Student(); Console.WriteLine("Student age is {0}", obj1.studentage); Console.ReadKey(false); } } }
Events
We will discuss this a bit in detail after few more section, please treat this material as introduction and nothing more. Events are a way for a class or object to notify other classes or objects when something has happened. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers. Events have the following properties:- The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event.
- An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers.
- Events that have no subscribers are never called.
- Events are commonly used to signal user actions such as button clicks or menu selections in graphical user interfaces.
- When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. To invoke events asynchronously.
- Events can be used to synchronize threads.
- In the .NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class.
No comments:
Post a Comment