Govt. Exams
Entrance Exams
C# 11 allows static abstract members in interfaces. Static methods in interfaces provide default implementations and can be called via the interface type.
class Base { public virtual void Method() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Method() { base.Method(); Console.WriteLine("Derived"); } }
Derived d = new Derived();
d.Method();
The 'base' keyword calls the parent class method first, printing 'Base', then the derived method prints 'Derived'. Output: 'BaseDerived'.
To override a method, it must be marked as 'virtual' in the base class. Attempting to override a non-virtual method causes a compilation error.
interface I1 { void Show(); }
interface I2 { void Show(); }
class C : I1, I2 { public void Show() { Console.WriteLine("C"); } }
C obj = new C();
obj.Show();
A single method implementation satisfies both interface contracts. The method is called and prints 'C'.
class A { public void Test() { Console.WriteLine("A"); } }
class B : A { public new void Test() { Console.WriteLine("B"); } }
A a = new B();
a.Test();
Using 'new' keyword hides the method. Since reference is of type A, A's Test() is called. Output is 'A'.
C# 12 (2024) introduced advanced list patterns that allow filtering and deconstructing collections in pattern matching expressions, improving code readability and efficiency.
GetHashCode() on the same string returns the same hash code value, so comparison is true. Strings are immutable in C#.
C# has progressively introduced nullable reference types, required keyword (C# 11), and init-only properties (C# 9) for better null-safety and immutability.
The GetType().Name returns the type name as "Decimal" (capitalized). The 'm' suffix specifies a decimal literal. The output shows the CLR type name, which is "Decimal".
C# implements multiple interface inheritance by listing interfaces separated by commas after the class name: class MyClass : Interface1, Interface2. There is no 'extends' or 'implements' keyword in C#.