Base keyword behavior in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how using the base keyword affects the time it takes for a program to run.
We want to see how calling a base class method impacts performance as the program grows.
Analyze the time complexity of the following code snippet.
class BaseClass
{
public virtual void Display()
{
Console.WriteLine("Base Display");
}
}
class DerivedClass : BaseClass
{
public override void Display()
{
base.Display();
Console.WriteLine("Derived Display");
}
}
// Usage
var obj = new DerivedClass();
obj.Display();
This code calls a method in the base class from the derived class using base.Display().
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
base.Display()once inside the overridden method. - How many times: Exactly once per call to
DerivedClass.Display().
Each time Display() is called on the derived object, it calls the base method once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to base method |
| 100 | 100 calls to base method |
| 1000 | 1000 calls to base method |
Pattern observation: The number of base method calls grows directly with the number of calls to the derived method.
Time Complexity: O(n)
This means the time to run grows in a straight line with how many times you call the method.
[X] Wrong: "Using base makes the method run slower exponentially."
[OK] Correct: Calling a base method just adds one extra step per call, so it grows evenly, not exponentially.
Understanding how base method calls affect performance helps you explain inheritance behavior clearly and confidently.
What if the base method itself called another method repeatedly? How would that change the time complexity?
Practice
base keyword do in C#?Solution
Step 1: Understand the role of
Thebasebasekeyword is used in a child class to refer to its parent class members.Step 2: Identify what
It allows access to parent class methods, properties, or constructors from the child class.baseallowsFinal Answer:
It allows a child class to access members of its parent class. -> Option BQuick Check:
basekeyword = access parent members [OK]
- Confusing base with new instance creation
- Thinking base deletes objects
- Assuming base defines a class
base in C#?Solution
Step 1: Recall syntax for calling parent constructor
In C#, to call a parent constructor, use: base()after the child constructor signature.Step 2: Match correct syntax
public Child() : base() { } usespublic Child() : base() { }, which is the correct syntax.Final Answer:
public Child() : base() { } -> Option AQuick Check:
Parent constructor call =: base()[OK]
- Omitting colon before base()
- Using base() inside constructor body incorrectly
- Using wrong keyword like parent()
class Parent {
public virtual void Show() {
Console.WriteLine("Parent Show");
}
}
class Child : Parent {
public override void Show() {
base.Show();
Console.WriteLine("Child Show");
}
}
var obj = new Child();
obj.Show();Solution
Step 1: Understand method overriding and base call
TheChildclass overridesShow()and callsbase.Show()which runs theParentversion first.Step 2: Trace the output
First, "Parent Show" is printed frombase.Show(), then "Child Show" is printed from the child method.Final Answer:
Parent Show Child Show -> Option DQuick Check:
base.Method() runs parent method first [OK]
- Ignoring base.Show() call
- Expecting only child output
- Thinking code causes error
class Parent {
public void Display() {
Console.WriteLine("Parent Display");
}
}
class Child : Parent {
public override void Display() {
base.Display();
Console.WriteLine("Child Display");
}
}Solution
Step 1: Check method overriding rules
In C#, only methods markedvirtualorabstractin the parent can be overridden.Step 2: Analyze the Parent class method
TheDisplay()method inParentis not virtual, sooverrideinChildcauses a compile error.Final Answer:
Cannot override non-virtual method Display() in Parent. -> Option AQuick Check:
Override requires virtual method [OK]
- Trying to override non-virtual method
- Confusing base call syntax
- Assuming override works without virtual
class A {
public virtual string GetName() => "A";
}
class B : A {
public override string GetName() => "B";
}
class C : B {
public override string GetName() => base.GetName() + "C";
}
var obj = new C();
Console.WriteLine(obj.GetName());Solution
Step 1: Trace method calls through inheritance
ClassCoverridesGetName()and callsbase.GetName(), which refers toB's override returning "B".Step 2: Combine returned strings
Cappends "C" to the result fromB, so the final string is "B" + "C" = "BC".Final Answer:
BC -> Option CQuick Check:
base.GetName() calls immediate parent method [OK]
- Assuming base calls grandparent method
- Ignoring string concatenation
- Expecting only "C" output
