Govt. Exams
Entrance Exams
C# auto-properties with 'init' accessor allow creating read-only properties that can only be set during initialization, improving encapsulation.
Constructor chaining uses 'this()' to call another constructor in the same class. Option B shows proper syntax for calling a parameterless constructor.
Classes are reference types stored on heap, structs are value types stored on stack. This affects memory management and behavior.
The 'sealed' keyword prevents a class from being inherited or a method from being overridden in derived classes.
Properties in C# allow controlled access to fields with validation logic while maintaining clean, intuitive syntax like field access.
IEnumerable interface in C# is used to provide iteration capability through the GetEnumerator() method.
'new' keyword hides the base class method (method hiding), while 'override' provides true polymorphic behavior by replacing the method.
class A { public virtual void Show() { Console.WriteLine("A"); } }
class B : A { public override void Show() { Console.WriteLine("B"); } }
A obj = new B();
obj.Show();
Due to polymorphism, the overridden method in class B is called, not the virtual method in class A. Output is 'B'.
The 'is' operator checks reference equality by default and is used for type checking, while '==' can be overloaded by classes to provide custom equality logic.
string str = null;
string result = str?.ToUpper() ?? "DEFAULT";
Console.WriteLine(result);
The null-coalescing operator (?.) with null-coalescing assignment (??) ensures that when str is null, the expression evaluates to null, and then ?? provides the default value 'DEFAULT'.