Govt. Exams
Entrance Exams
'this' keyword represents the current instance of the class. It is used to access instance members, differentiate between local and instance variables, and pass the current object as a parameter.
public class Employee
{
private string name = "John";
public Employee(string n) { name = n; }
}
Employee emp = new Employee("Alice");
Console.WriteLine(emp.name);
The 'name' field is declared as 'private', so it cannot be accessed outside the class. Attempting to access emp.name from outside the class results in a compilation error.
The 'base' keyword allows a derived class to access members (methods, properties, constructors) of the parent class. It's commonly used to call the parent constructor or override methods while keeping parent functionality.
Encapsulation is the bundling of data (variables) and methods (functions) into a single unit (class) while hiding the internal details. Access modifiers like private, protected, and public control visibility.
The 'base' keyword is used to access members (properties, methods) of the parent class from within derived class methods, particularly in constructors and method overriding.
Interfaces define a contract that classes must implement, ensuring consistent method signatures across different implementations. They enable polymorphism and loose coupling.
The 'private' access modifier restricts access to members within the same class only. It is the most restrictive access level in C#.
Default ToString() (inherited from object) returns the fully qualified namespace and class name. Custom implementations are recommended for meaningful string representation.
Proper encapsulation uses private fields with public properties, providing controlled access and allowing for future validation logic.
'base' keyword is used to access base class members, particularly useful in constructors to call base class constructors or override base methods.