# OOPs in C# Interview Questions | Page 2

##### Question 11:What is method signature in C#? Explain.

C# allows us to define multiple methods in a class with the same name having different method signature. Method signature includes the following

• Name of the method
• Number of arguments in a method
• Data type of the arguments

Note: return type of method in not part of method signature. param keyword is also not part of method signature.

Consider the below examples

### Example

class Student
{
void TestMethod() { };                          // TestMethod()
void TestMethod(int x) { };                    // TestMethod(int)
void TestMethod(ref int x) { };               // TestMethod(ref int)
void TestMethod(int x, int y) { };            // TestMethod(int, int)
int TestMethod(string s) { };                 // TestMethod(string)
int TestMethod(int x) { };                     // TestMethod(int)  error as return type int is not considered as part of  method signature
void TestMethod(string[] a) { };              // TestMethod(string[])
void TestMethod(params string[] a) { };      //TestMethod(string[])      error as peram is not part of method signature
}


Method overloading is to define the multiple methods with the same name but having different method signature. Consider the below example.

class TestOverloading
{
public void Print(int number)
{
Console.WriteLine(number);
}

public void Print(float number)
{
Console.WriteLine(number);
}

public void Print(int number1, int number2)
{
Console.WriteLine(number1+ number2);
}

public void Print(double number)
{
Console.WriteLine(number);
}
}


In the above example, Test method have different overloads. Name of the parameter is not the part of the method signature. If we define the different name of the parameter with the same type as mentioned in the below example, then it will be the compiler error. "'TestOverloading' already defines a member called 'Print' with the same parameter types"

class TestOverloading
{
public void Print(int number)
{
Console.WriteLine(number);
}

public void Print(int num)
{
Console.WriteLine(num);
}
}

##### Question 13:What is the sealed class in C#?

Sealed class is a class that can not be inherited by any class. C# provided sealed keyword to mark a class as sealed. If any class try to inherit sealed class then there will be a compile error "cannot derive from sealed class".

### Example

 sealed class Window
{

}
##### Question 14:Why use sealed class in C#? Explain a business case.

We use sealed class when we want to prevent inheritance of a class, we do not want to allow some class to extend the functionality of a class.

Generally, we create sealed classes for those classes which implement security features which can be overridden by other classes if it is not in sealed class.

##### Question 15:Can we use sealed class as a base class?

Base class is a class which can be overridden by other classes. Sealed class cannot be used as a base class because sealed class can not be overridden by any class.

Similarly, sealed class also cannot be used as an abstract class because abstract classes have one or more abstract methods which are implemented by derived classes.

One of the benefits of sealed classes is also because sealed classes can not be used as base classes, so CLR makes some runtime optimization on sealed classes. Creating objects and calling members of sealed class is faster. Learn more about why to use sealed class here.

##### Question 16:What is difference between constant and read-only in C#?

Constants are the compile time constant i.e. value of a constant field can only be assigned one time and it can not be changed later.

### Example: Defining a constant variable

   class Circle
{
public const int number = 10;
}


Read-only members are the runtime constant i.e. value of read-only members can be assigned at runtime inside the constructor of the class.

### Example:

   class Circle
{
public readonly double PI = 3.14;

public Circle(bool value)
{
if (value)
{
PI = 3.142;
}
}
}


In the above example, we are changing the value of read-only field PI inside the constructor of class Circle based on certain condition.

Constants are implicitly static,i.e. we can use a ClassName.ConstantName notation to access constants.

### Example:

class Program
{
static void Main(string[] args)
{
Console.WriteLine(Circle.number);
}
}


Output

10
Share interview questions