C# Interview Question 1
What is the difference between an `abstract` class and an `interface` in C#?
Hint 1: An `abstract` class can provide implementation, whereas an interface cannot.
Hint 2: `abstract` classes allow method implementation, while interfaces require all methods to be abstract.
Correct Answer: B) An abstract class can contain method implementations, but an interface cannot.
Explanation: An `abstract` class can provide partial implementation of methods, while an interface only defines method signatures without any implementation. Also, an abstract class can have fields and constructors, whereas an interface cannot.
Code Sample:
// Abstract class example
// An abstract class can have both abstract methods (without implementation) and concrete methods (with implementation).
public abstract class Animal
{
// Abstract method: must be implemented by derived class
public abstract void MakeSound();
// Concrete method: can be used as-is by derived class
public void Sleep()
{
Console.WriteLine("This animal is sleeping.");
}
}
// Interface example
// An interface defines method signatures, but does not provide any implementation.
public interface IWalkable
{
// All methods in an interface must be abstract (no implementation)
void Walk();
}
// Derived class implementing both abstract class and interface
public class Dog : Animal, IWalkable
{
// Implement the abstract method from Animal
public override void MakeSound()
{
Console.WriteLine("Woof! Woof!");
}
// Implement the method from IWalkable
public void Walk()
{
Console.WriteLine("The dog is walking.");
}
}
Explanation of Code:
- The `Animal` abstract class has both an abstract method (`MakeSound`) and a concrete method (`Sleep`). The derived class (`Dog`) must implement the abstract method.
- The `IWalkable` interface only defines the method signature for `Walk` without any implementation, and the `Dog` class provides the implementation for it.
- Key difference: Abstract classes can provide method implementations, whereas interfaces only define method signatures that must be implemented by the class.
Subscribe to get access
Read more of this content when you subscribe today.
C# Interview Question 2
What is the difference between a `struct` and a `class` in C#?
Hint 1: A `struct` is a value type, while a `class` is a reference type.
Hint 2: `struct` types are typically used for small, simple data structures, while `class` types are used for more complex objects.
Correct Answer: C) A `struct` is a value type, while a `class` is a reference type.
Explanation: In C#, a `struct` is a value type, meaning it holds the data directly. A `class` is a reference type, meaning it holds a reference to the data. This is the primary difference between the two. Additionally, `structs` cannot inherit from other types, whereas `classes` can.
Code Sample:
// Struct example
// A struct is a value type, meaning it holds its data directly.
// Structs are typically used for small data structures where performance matters.
public struct Point
{
public int X;
public int Y;
// Constructor that initializes the X and Y coordinates
public Point(int x, int y)
{
X = x;
Y = y;
}
}
// Class example
// A class is a reference type, meaning it stores a reference to the data rather than the actual data.
// Classes are typically used for more complex objects.
public class Person
{
public string Name;
public int Age;
// Constructor that initializes the Name and Age properties
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Explanation of Code:
- Point struct: The `Point` struct holds `X` and `Y` values directly. When you create or pass a struct, the data is copied. This is an example of how value types work in C#.
- Person class: The `Person` class holds a reference to the object. When you pass a class object around, you are passing the reference to the object, not the actual object itself. This is an example of how reference types work in C#.
- Key Difference: The primary difference between structs and classes is that structs are **value types** (data is stored directly) and classes are **reference types** (data is accessed via a reference).