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.
class Startup{// Main Methodstatic void Main(string[] args) {Console.WriteLine($"Count of command line arguments: {args.Length}"); }}# Alternatively, return type can be an int or a Task
- “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.
int age; # 1. Declarative statement
int age = 3 # 2. Assignment statement
....
doSomething() # invocation statement
if( a > 4 ) { doSomething() } # conditional statements
.......
- 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.
...
public string Fullname = "Myname" + "Surname"; ....
int discount = calculateDiscount(); # function call calculates and returns discount value
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.
...
var item = "Asp is fun!"; # type inferred as string by compiler
var player = new { team = "FCB", name = "Lionel Messi" };# You can also create arrays of anonymous objects as follows
var myArr = new[] { new { phone = "apple", size = 4 }, new { phone = "huawei", size= 1 }};
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.
....
int void double( ref int x) { return x * 2 };# Inside main
...
int x = 1 ; # local variable x
var result = double(ref x);
...
# local variable x now equals 2
- 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.
...# This delegate references any function which takes in two arguments # and returns an intpublic delegate int MyDelType(int x, int y);# You can use a delegate as follows
int summer(int x, int y){return x+y};..
MyDelType add = summer;var sum = add(1,2);
Console.WriteLine($"The sum of {1} and {2} is {sum}");
- 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.