InterviewSolution
Saved Bookmarks
| 1. |
Role of “this” in C# |
|
Answer» The current instance of the class is REFERRED using the “this” keyword. Also “this” keyword can be used to access data members from constructors, instance methods etc. Another CONSTRUCTOR can also be called from a constructor in the same class using the “this” keyword. A program that demonstrates the usage of the “this” keyword in C# is given as follows: using SYSTEM; namespace Demo { class Student { private int rollNo; private string name; PUBLIC Student(int rollNo, string name) { this.rollNo = rollNo; this.name = name; } public int returnRollNo() { RETURN rollNo; } public string returnName() { return name; } } class program { public static void Main() { Student s = new Student(1202, "Harry Smith"); Console.WriteLine("Roll Number: " + s.returnRollNo()); Console.WriteLine("Name: " + s.returnName()); } } }The output of the above program is as follows: Roll Number: 1202 Name: Harry Smith |
|