C# Interview Questions for beginners — Part 1

In this article we cover some of the Language Basics questions in C#.

1. Explain the role of the Main method in C# scripts

  • The main method is the entry point for any executable C# application.
  • Since it is called when the program starts executing, it must be a static class member. i.e. It can be called without instantiating the startup class.
  • “If the return type is an int, it enables the program to communicate status information to other programs or scripts that invoke the executable file”. e.g. returning 0 indicates successful execution.
  • More info.

2. How is a C# statement different from an expression statement

  • A statement represent a line or lines of code terminated by a semi-colon or a sequence of statements enclosed in curly braces {}.
  • Basically a program is made up of a sequence of statements that perform a specific task. e.g.
  • An expression is an assignment statement where the value of a variable is obtained by evaluating a result of an operation or calling a function.

3. Can you give an example of a situation where you’d use a struct over a class?

  • Struct(s) are typically used in cases where the represented data is not expected to change after initialization.
  • In addition, if the required behavior is that of a value type, a struct is used.
  • More info.

4. Which keyword do we use to declare anonymous types?

  • Anonymous types are declared using the keyword var.

5. Discuss the difference between sealed & abstract classes

  • A sealed class cannot be used as a base class. This means that no class can be derived from this class.
  • This feature can be useful in implementing security features to avoid spoofing. Read more about this here.
  • An abstract class cannot be instantiated and hence it can only be used as a base class.
  • This is useful for encapsulating common behavior among different classes and it also facilitates code re-usability.
  • More info.

6. What is the difference between members marked as virtual and those marked as abstract inside a base class?

  • A class that derives from an abstract class must implement all methods marked as abstract in the base class.
  • Virtual methods can be optionally overridden by a derived class if needed or the original method from the base class can be used.
  • This is because abstract methods are not implemented in the base class, i.e. They are only declared. On the contrary, virtual methods are always implemented in the base class.
  • More info.

7. What are access modifiers does C# support? Describe each of these briefly

  • Public — Class members accessible to all classes in the assembly and all assemblies that reference this assembly.
  • Private-Class members only accessible inside the class that contains these members.
  • Protected-Class members ‘visible’ in the defining class as well as in derived classes.
  • Private protected-Accessible from the same or derived class within the same assembly.
  • Internal-Accessible from anywhere within the parent assembly.
  • Protected internal- Class members ‘visible’ in the defining class as well as in derived classes in the same or other assemblies.
  • More info.

8. What is the purpose of ref and out keywords?

  • Ref is typically used to mark arguments passed to functions as reference types instead of value types.
  • The out keyword does the same thing as ref, except that, the value does not need to be initialized before it is passed to the function call.
  • More info.

9. What is the fundamental difference between reference reference and value types?

  • A reference value type holds the address to the memory location to where the actual value is stored.
  • A value type holds the actual value that is assigned to it.

10. Explain what a delegate is using a code example

  • A delegate represents a C# type that represents a method with a specified signature.
  • Thank you for reading, if you have any comments or suggestions please feel free to ping me on the comments section below.
  • Watch out for part 2 of this series.

Written by

Former Glorified Electrician(aka Electrical Engineer). Now a Software Developer working on complex Enterprise Software. Lets connect on twitter @NdamuleloNemakh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store