C# Questions Part 1
1. What is a class?
A class is a template to create an object. It contains properties as well as methods.
Whereas an Object is a real-time entity, consisting of state (look & feel) and behavior (what it does)
2. What are the main concepts of OOPs?
Encapsulation, abstraction, polymorphism, and inheritance are the main concepts of object-oriented programming.
3. Explain Encapsulation
Encapsulation is a process of wrapping functions and data members together in a class. It is like a capsule, a single unit.
Encapsulation is to prevent the unauthorized or unwanted change of data from outside of the function.
4. What is abstraction?
Abstraction is the method of exposing only the required features of the class and hiding unnecessary information.
- A rider knows the color, name, and model of the bike. Still, he does not know the internal engine and exhaust functionality.
So abstraction focuses on providing access to a specific functionality without exposing how that functionality works internally.
5. What is polymorphism?
Polymorphism means the same method but a different implementation.
2 types of polymorphism.
Compile-time polymorphism – achieved by method overloading
Run time polymorphism – achieved by method overriding
6. What is Inheritance in C#?
When we create a class that can inherit the data members and the methods of another class (Parent class), it is called inheritance.
The class that inherits the properties and method is called a child class, derived class, or subclass.
A class from which the child class inherits its properties and methods is a parent class or base class.
Note: C# doesn't support multiple inheritance using classes, like Java
Output will be 1337, even though Class B doesn’t directly have any value.
7. What is an object?
An object is an instance of a class through which we access the functions of that class. We can use the “new” keyword to create an object. A class that creates an object in memory holds information about the functions, data members, and behavior of that class.
8. What is a constructor, and what are its different types?
A constructor is like a method with the same name as the class. Even if it is not created, the compiler creates a default constructor in memory at the time of creating an object of the class.
- The constructor is used to initialize the object with some default values.
- Default constructor, parameterized constructor, copy constructor, static constructor, and private constructor are different constructor types.
Static constructor: Used to initialize any static members of a class.
Static members are shared among all instances of the class.
9. What is a destructor in C#?
The Destructor clears out the memory to free up resources. It is managed automatically by the garbage collector. System.GC.collect() is called internally for this purpose. However, if required, it can be done explicitly using a destructor.
10. Is C# code managed or unmanaged code?
C# is managed code.
Managed code is code that is written in languages such as C#, Visual Basic.NET, or F# and is executed by the Common Language Runtime (CLR) in the .NET Framework or .NET Core.
The CLR provides various services such as automatic memory management (garbage collection, allocation, and deallocation), exception handling, and security enforcement.
On the other hand, unmanaged code typically refers to code that is written in languages like C or C++, where the programmer has direct control over memory and other system resources.
11. What are value types and reference types?
Based on memory allocation, data types can be value types and reference types.
Value types:
- Store the value in a stack
- Classified in
- Structs: bool, byte, int, long, short, float, double, char, decimal, struct,
- Enums
- Store the value in the heap and its reference in the stack
- Classified in
- String, array, dynamic, object, class, interface, and delegates
Note: Arrays can store elements of either value types or reference types, depending on the type of elements they hold.
12. What are namespaces, and are they compulsory?
A namespace is a way of organizing classes of the same group or functionality under the same name. We can call it a module. Although it is not compulsory to put a class in a namespace.
13. Explain the types of comments in C # with examples.
There are three types of comments in C #.
- Single line comments - //
- Multiline comments - /* ... */
- XML comments - ///
14. What is an interface? Give an example.
An interface is another form of an abstract class that has only abstract public methods, and these methods only have the declaration and not the definition. A class implementing the interface must have the implementation of all the methods of the interface.
15. How to implement multiple interfaces with the same method name in the same class?
If we want to implement multiple interfaces with the same method name, then we cannot directly implement the body of the function. We have to explicitly provide the name of the interface to implement the body of the method. In this way, the compiler decides which interface methods we are referring to, and this resolves the issue.
16. What is the virtual method, and how is it different from the abstract method?
A virtual method must have a default implementation, and we can override this virtual method using the override keyword in the derived class.
The abstract method is without implementation, and it is created inside the abstract class only. In the case of an abstract class, the class derived from the abstract class must have an implementation of that abstract method.
Example of a virtual method.
Example of an abstract method.
17. What is method overloading and method overriding?
Method overloading is when we have a function with the same name but a different signature.
Method overriding is when we override the virtual method of a base class in the child class using the override keyword.
Both method overloading and overriding are types of polymorphism.
18. What is the static keyword?
We use the “static” keyword to create:
- static class,
- static method,
- static properties
When we create a static class, there can be only static data members and static methods in that class.
Static means that we cannot create an instance of that class. That class can be used directly like ClassName.methodName.
For example, there is a requirement to load some default application-level values. We create a static class with static functions. That class is then accessible to all other classes without creating any instance. It also shares the same data with all the classes.
19. Can we use “this” with the static class/method?
No, we cannot use “this” with the static class or methods because this
represents the current object instance of a class, and static classes/members do not have instances.
20. What is the difference between constants and read-only?
Constant:
- Must be initialized at the time of declaration and cannot be changed afterward
- They are implicitly static and belong to the class, making them accessible using the class name without creating an instance
Read-only:
- Can be initialized either at the time of declaration or in the constructor of the class
- Once initialized, their values cannot be changed.
- They are instance-specific and belong to a particular object of the class. They can only be accessed using an instance of the class.
Here is an example of constants.
Here is an example of read-only.
Use constants when we have values known at compile-time that should not change, and use read-only fields when we need to set the value at runtime but want to prevent further modifications after initialization.
21. What is the difference between string and string builder in C#?
A string is an immutable object. When we have to do some actions to change a string or append a new string, it clears out the old value of the string object, and it creates a new instance in memory to hold the new value in a string object. It uses a System.String class, for example.
StringBuilder is a mutable object. It means that it creates a new instance every time for operations like adding a string (append) or replacing a string (replace). It uses the old object only for any of the operations done to the string and thus increases the performance. It uses a System.Text.StringBuilder class.
22. Explain the “continue” and “break” statements.
We can use continue and break statements in a loop in C #. Using a break statement, we can break the loop execution, while using the continue statement, we can break one iteration of the loop.
An example of the break statement.
Here is the same example with a continue statement.
23. What are boxing and unboxing?
Conversion of a value type to a reference type (object) is called boxing.
In the Boxing process, a value type is allocated on the heap rather than the stack
Unboxing is the conversion of a reference type to a value type.
In the unboxing process, an unboxed value is allocated to a variable in the stack rather than the heap.
24. What is a sealed class?
Classes are created as sealed classes when there is no need to inherit from that class further.
25. What is a partial class?
There is a feature in the C # language to divide a single class file into multiple physical files. To achieve this, we have to use the “partial” keyword.
At compile time, it is logically one file only. So we cannot have a method with the same name or a variable with the same name in two different partial class files.
26. What is enum?
- An enum is a set of Constants
- To create an enum, the “enum” keyword is used, and the enum items are listed with a comma, directly inside a namespace, class, or structure
- An enum is a value type
- Used to assign constant names to a group of integer values
- The default underlying type of an enum is int
- The default value for 1st element is zero and gets incremented by 1
- Enum is converted into an abstract class behind the scenes
- If values are not assigned to enum members, then the compiler will assign an integer value to each member starting with ZERO by default
27. What is dependency injection, and how can it be achieved?
- Dependency injection is a design pattern
- Achieved using interfaces (classes can communicate through interfaces rather than concrete classes)
- Instead of creating an object of a class in another class (dependent class) directly, we are passing the object as an argument in the constructor of the dependent class.
- It helps to write loosely coupled code, and helps us easily test
There are 3 ways to achieve dependency injection.
- Constructor Injection:
- In constructor injection, we can pass the dependency class object through the constructor.
- We have to make sure that we do not have a default constructor here, and the only one should be a parameterized constructor.
- Setter or Property Injection: There are cases when we need the default constructor of a class, so in that case, we can use property injection
- Method Injection: In method injection, we need to pass the dependency in the method only. When the entire class does not require that dependency, there is no need to implement constructor injection.
28. The “using” statement.
There are 2 purposes for using keywords in C #:
- Using directive
- Using statement: Ensures that the DISPOSE() method of the class object is called even if an exception occurs.
29. What are the access modifiers? Explain each type.
- Access modifiers are keywords used to provide accessibility to a class, member, or function.
- Below are its types
- Public: Can be accessed anywhere without any restrictions
- Protected: Access is limited to the class, which inherits this class.
- Internal: Can be accessed only within the current assembly.
- Private: Cannot be accessed outside.
30. What are delegates?
- Delegates are like function pointers
- It is a reference data type that holds the reference of a method
- We use delegates to write generic type-safe functions
- Derive from System.Delegate class
- Declared using the delegate keyword followed by a function signature
- Delegates have a signature as well as a return type. A function assigned to delegates must fit with its signature.
- Delegates can point to instance methods or static.
- Delegate objects, once created, can dynamically invoke the methods it points to at runtime.
- Delegates can call methods synchronously and asynchronously.
- Used for implementing events and call-back methods.
31. What are the different types of delegates?
- Single Delegate: A single Delegate can invoke a single method.
- Multicast Delegate:
- Delegate who holds the reference of multiple methods and can invoke it.
- We can add a method in the delegate instance using the + operator, and we can remove a method using the – operator.
- All the methods are invoked in sequence as they are assigned.
- Generic Delegate: .Net Framework 3.5 introduces Generic delegates. There is no need to create an instance in a generic delegate.