1 Question: – What is C#?
C# Stands for C Sharp. It is a powerful Object-Oriented Programming language which is created by the Microsoft. It is the part of the .NET framework and it is widely used for building various types of applications like, desktop software, web applications, games, mobile apps and more.
2 Question: – Describe the features of C# Language.
C# (C Sharp) is a modern programming language which developed by Microsoft that Offers a rich set of features for building the various types of applications. Here are the key features of C# Language:
- Object-Oriented Programming
- Type Safety
- Automatic Memory Management
- Platform Independence
- Modern Language Features
- Safety and Security
- Rich Standard Library
- Tooling and Ecosystem
3 Question: – Differentiate between ‘value types’ and ‘reference types’ in C#.
Value Types | Reference Type |
---|---|
value Types are stored their data directly in memory where the variable is declared. | Reference is used for store a reference (memory address) to the location where the data is stored. |
When you are declaring the value type variable, it holds the actual value of the data values. | When you declare the reference type variable, it holds the reference (pointer) to the actual data in the heap. |
Operations on value types directly manipulate their values. | Operations on Reference type variable, it holds a reference (pointer) to the actual data in a heap. |
Example of the value types include primitive types like int, float, char and bool. | Example of reference types include classes, interfaces, delegates and arrays. |
4 Question: – Explain the difference between ‘const
‘ and ‘readonly
‘ fields.
The ‘const’ and ‘readonly’ keywords in C# are used to declare fields with immutability, but they differ in the behavior and when they can be assigned a value.
- ‘const’: – These fields are compile-time constant and it must be initialized with a value at the time of declaration. The value of ‘const’ field is constant and cannot be changed after initialization.
- ‘readonly’: – These fields are runtime constants and can be assigned a value either at the time of declaration or within a constructor. The value of ‘readonly’ field can be set only once (either at declaration or in a constructor) and cannot be modified there after.
5 Question: – What is the role of the Main method in a C# application?
The “Main” Method is the fundamental entry point for executions in a C# application. It serves as the starting point of any C# program and is the method that the runtime calls when the program begins. The “Main” method has a specific signature:
static void Main(string[] args)
6 Question: – Describe the difference between == and Equals() method in C#.
In C#, the “==” operator and the “Equals()” method serve different purpose when comparing objects:
- “==” Operator: – The “==” operator is used to compare the references (memory address) of two objects. By default, the “==” operator compares the reference equality for reference types. In other words we say it checks if two references point to the exact same instances.
- “Equals() Method”: – The “Equals()” method is used to compare the contents or values of two objects for equality. By default, “Equals()” compares the reference equality for the reference types, but this behavior can be overridden in classes to provide value-based equality comparison.
7 Question: – Explain boxing and unboxing in C#.
Boxing and Unboxing in C# are operations used to convert value types to reference types (boxing) and vice versa (unboxing).
Boxing | Unboxing |
---|---|
Boxing is the process of converting a value type (e.g. int, float, bool) to an object reference type (e.g. ‘object’ or any interfaces implemented by the value types). | Unboxing is the process of converting an object of a reference type back to its original value type. |
When you box a value type, the runtime allocates memory on the heap to store a copy of the value type’s value along with its type information. | Unboxing requires an explicit type cast and can only be performed if the object is actually a boxed value of the specified value type. |
8 Question: – How does C# handle multiple inheritance?
C# does not support the multiple inheritance of classes like some other programming languages (e.g.. C++). Instead, C# supports multiple inheritance through interfaces, which is a different approach known as “interfaces-based inheritance”.
9 Question: – What is the purpose of the using statement in C#?
In C#, “using” statement serves two primary purposes related to resources management,
- Managing Disposable Object
- Simplifying Exception Handling
10 Question: – How are comments written in C#?
In C#, we used two types of Comments:
- Single Line Comment: – Single Line comments are written like,
// This is a single-line comment
int x = 10; // This comment follows a statement
- Double Line Comment: – Double Line Comments are written like,
/*
This is a multi-line comment
It can span multiple lines
*/
11 Question: – What is a nullable type in C#?
A nullable type in C# is a special type which allows us to a variable to either hold a normal value which is of its underlying type or a null value. In C#, nullable types are represented using a generic “struct” called “Nullable”, where T is the underlying value type (such as int, float, bool, etc.).
12 Question: – Describe the scope of variables.
The Scope of the variables determines where the variable can be accessed and used in the codes. In other words we also says that the scope of a variable is defined by the block of code in which it is declared. By understanding the variable scope in C# you can easily ensuring the proper memory management.
13 Question: – What are implicitly typed variables (var) in C#?
In C#, implicitly types variables, denoted by the “var” keyword, which allow us to declare variables without explicitly specifying the type of the variable. Instead, the type of the variable is inferred by the compiler based on the expression used to initialize it.
14 Question: – Explain the use of “enum”.
An “enum” (enumeration) is a value type that allows you to define a set of named integral constants. “Enums” are useful because it is used for representing a fixed set of related constants, making the code more readable, and maintainable.
15 Question: – Define a Method.
A method is a block of code that performs a specific task or operations. Methods are declared within the classes and it is used for encapsulate behavior, which improve the modularity. Methods have also the parameters, return values, and access modifiers which is used for control the visibility and accessibility.
16 Question: – Explain Method Overloading.
Method Overloading allows us to define the multiple methods in the same class with the same name but the list of the parameters is different. Here the compiler determines the which method to invoke based on the number, type, and order of the parameters passing during method invocation.
17 Question: – Explain Optional Parameters Methods.
Optional Parameters allows us to specify default values for method parameters. When calling a method with optional parameters, we can choose to omit some arguments, and the default values specified in the method declaration which will be used for those parameters.
18 Question: – How do you use “params” keyword in Method.
The “params” keywords allows you to specify a variable number of parameters for a method. This feature enables to pass the variable number of arguments of the same type to a method without explicitly specifying an array. The “param” keyword is important for applying at the last parameter of the method, beacuase it allows you to pass zero or more arguments of the specified type.
19 Question: – Explain Encapsulation in C#.
Encapsulation is the fundamental principle of object-oriented programming which involves bundling the data (fields) and methods (behaviors) that operate on the data into a single unit called class. Encapsulation helps us to hide the internal state (data) of an object from the outside world and allows us for controlled the access to that state through public methods (getter and setter).
20 Question: – Explain interfaces.
In C#, Interfaces are defines a contract of methods, properties, events, indexers that a class can implement. An interfaces specifies what a class must do (its behavior) without providing the implementation details. Classes that implement an interfaces must provide definitions (implementations) for all the members defined in the interfaces.