Link Search Menu Expand Document

C# Interview for Beginners

Here are a few C# interview questions with answers which will help freshers to plan and prepare for technical interviews during their placements.

1. Mention the different types of classes in C#.

Ans:

  • Partial class: Partial class is a class that is represented using the ‘partial’ keyword. ‘Partial’ keyword shows that it is easier to specify other aspects of a class or interface in the namespace.
  • Sealed class: Sealed class is a class that is represented using the ‘sealed’ keyword. The keyword can be used to escape inheritance.
  • Abstract class: Abstract class is a class that is represented using the ‘abstract keyword’. It is not possible to initialize an abstract class.
  • Static class: Static class is a class that is represented using the ‘static’ keyword. It is not possible to initialize a static class.

2. What is Boxing and Unboxing in C#?

Ans: Boxing and Unboxing are both used to convert one data type to another data type. The method of converting from a data type to an object type is known as Boxing.

Example:
int number = 456;
Object obj = number;
Console.WriteLine(number);
Console.WriteLine(obj);

The method of converting from an object type to a data type is known as Unboxing.

Example:
Object ob = 456;
int number1 = (int)obj;
Console.WriteLine(number1);
Console.WriteLine(obj);

3. Illustrate the difference between a struct and a class in C#.

Ans: Struct

  • A struct is represented using the ‘struct’ keyword.
  • A struct is a value type that is built from System. Value type.
  • A struct is commonly used to represent smaller volumes of data.

Class

  • A class is an outline from which objects are created.
  • A class is a reference type that is built from System. Object type.
  • A class is commonly used to represent vast volumes of data.

4. Define Internal Modifier.

Ans:

  • Internal Modifier is one of the access modifiers in C#.
  • Internal members are only available inside the files in the same assembly.
  • A class, its members, or its fields can be declared as internal
  • An internal modifier is represented using the ‘internal’ keyword before declaring a class

5. What is enum in C#?

Ans: An enum is a preset data type which is represented using the ‘enum’ keyword. It is a particular “class” containing a set of constants.

6. Mention the differences between Interface and Abstract class in C#

Ans:

  1. Interfaces may only include abstract methods, whereas Abstract class may include both abstract and non-abstract methods.
  2. Abstract class doesn't support multiple inheritances while an interface supports multiple inheritances
  3. The Abstract class may have variables that are final, non-final, static, and non-static. An interface can have variables that are static and final.

7. What are extension methods in C#?

Ans: Extension methods are static methods for a static class. It helps you to apply new methods to the same class or framework without changing the original code without changing the actual data type.

8. Mention the difference between continue and break statements.

Ans: The break statement comes out of the particular loop and moves execution with the immediate statement after the loop. Continue statement breaks the loop and proceeds for the next iteration in the loop.

9. Can we execute multiple catch blocks?

Ans: Yes, we can execute multiple catch blocks. When multiple catch blocks occur, only the one that meets the exception form is first implemented.

10. Distinguish between constant and read-only in C#?

Ans: Const is a variable whose value is constant at compile time. Readonly is the keyword whose value can be modified or allocated at runtime, but only through a non-static builder.

Other useful articles:


Back to top

© , CsharpOnline.com — All Rights Reserved - Terms of Use - Privacy Policy