Beginner level questions
What are Nullable types?
Value types that can accept a normal value or a null value are referred to as Nullable types
What is (??) operator in C#?
The ?? operator is called the null coalescing operator. It is used to define a default value for a nullable value type.
How to check that a nullable variable is having value?
The HasValue property is used to check whether a variable having value or not.
What are the differences between Object and Var?
Object | Var |
---|---|
The object was introduced with C# 1.0 | Var was introduced with C# 3.0 |
Useful when you want to store multiple types of values to a single variable. | Useful when you don’t know the type of assigned value. |
It can be passed as a method argument. | It cannot be passed as a method argument. |
What is the ref keyword in C#?
The ref keyword causes an argument to be passed by reference, not by value.
What is the params keyword in C#?
The params keyword enables a method parameter to receive n number of arguments. These n number of arguments are changed by the compiler into elements in a temporary array.
What do you mean by operators in C#?
An operator is a symbol that tells the compiler what operations can be performed o an operand.
What is ternary operator in C#?
A ternary operator works on a conditional expression that returns a Boolean value. It is a shorthand form of if-else.
What is the static keyword mean in C#?
The static keyword is used to specify a static number, which means that static members are common to all the objects and they do not tie to a specific object.
What do you mean by Typecasting in C#?
Typecasting is a mechanism to covert one type of value to another value. It is possible only when both the data types are compatible with each other.
What are the different types of casting in c#?
- Explicit conversion: Conversion of larger data type to smaller data type. It might result in loss of data so it is also an unsafe type of casting.
- Implicit conversion: Conversion of a smaller data type to a larger data type and conversion of derived classes to base class.
What is an Out keyword in C#?
The out is a keyword in C# which is used for passing the arguments to methods as a reference type. It is generally used when a method returns multiple values
Can you use out and ref for overloading as the different signature of method?
No, we cannot do that. Even if both ref and out are treated differently at runtime they treated the same at compile time. Hence it cannot be overloaded with the same type of arguments.
What are the named arguments?
Any argument can be passed by parameter name instead of the parameter position.
What you mean by value type and reference type?
Value type: A value type variable stores actual values.
What is a safe and unsafe code in C#?
- Safe code: The code which runs under the management of CLR is called safe code
- Unsafe code: The code which does not run under the management of CLR is called unsafe code.
What is boxing and unboxing in C#?
- Boxing: Implicit conversion of a value type(int, char) to a reference type is known as boxing. Eg:
1 2
int variable = 15 object boxingvar = variable
- Unboxing: Explicit conversion of the same reference type(which is created back in boxing process) back to value type is known as unboxing.
1 2 3
int variable = 15 object boxingvar = variable int unboxed = (int)boxingvar
What is upcasting and Downcasting?
Implicit conversion of derived classes to a base class is called Upcasting and explicit conversion of the base class to a derived class is called Downcasting.
Intermediate level questions
What are the different types of decision-making statements in C#?
Decision-making statements help you to make a decision based on certain conditions. Different types of decision-making statements are: if statements, if-else statements, if-else-if statements, and switch statements.
Which one is better/fast, switch or if-else-if statements and why?
Switch statements are father than if-else-if statements because in if-else if statements each and every condition has to check but in case of the switch statement compiler does not need to check earlier cases.
What is the goto statement?
The goto statement transfers program control to a labeled statement. The statement must exist in the scope of the goto statement.
What is the return statement in C#?
The return statement terminates the execution of the method in which it appears and returns control to the calling method.
What is the jump statement in C#?
The jump statements transfer the program control from one point in the program to another point of the program.
What is the throw statement mean?
This statement throws an exception which indicates that an error has occurred during the program execution.
What do you mean by an array and what are the different types of an array in C#?
An array is a collection of the same type of elements that are accessible by a numerical index. Different types of array are- one dimensional, two dimensional, and jagged arrays.
What do you mean by multi-dimensional array?
A multi–dimensional array is an array with more than one level or dimension. For example, a 2D array,3D array, etc. Eg:
1 2 int[,] arr2D = new int[6,8];// declaration of 2D array arr2D[0,0] = 1;
What is a jagged array?
A jagged array is an array whose elements are array itself of different dimensions and sizes.
What do you mean by an object in C#?
An object is a representative of the class and is responsible for memory allocation of its member functions. An object is a real-world entity having attributes and behaviors.
What is a constructor?
A constructor is a special type of function which has the same name as its class. The constructor is invoked whenever an object of a class is created.
Why static constructor has no parameter?
Because it is going to call by CLR and not by an object.
Why you can have only one static constructor?
To define multiple constructors for a class, you need to overload the constructors. It means you need to define parameterized constructors that accept parameters from outside but a static constructor is called by the CLR and CLR cannot pass parameters to the parameterized constructor.
What do you mean by Destructor?
The destructor is a special type of member function which has the same name as its class name preceded by tilde[~] sign. It is used to release unmanaged resources allocated by the object. It cannot be called explicitly.
What is the purpose of “using” statement in C#?
The using statement obtains the specified resources, uses it and then automatically calls the dispose method to clean up the specified resources when the execution of the statement is completed.
What is an access modifier?
An access modifier is used to specify the accessibility of the class member.
Can destructors have access modifiers?
No, destructors cannot have access modifiers as they are directly called by compiler.
What do you mean by an enum and when to use it?
An enum is a value type that stores a list of named constants which are known as enumerators.
Uses of enum are:
- To define static constants.
- To define constant flags.
What is the difference between class and structure?
Class | Structure |
---|---|
The class can have default and parameterized constructor. | The structure can have only default. |
The class can have a destructor. | The structure cannot have destructor. |
The class is a reference type. | The structure is a value type. |
What do you mean by static members?
Static class members are declared using the static keyword. These can be only called with the class name.
What is the base class in .NET framework from which all the classes are derived?
System.Object
What is a static class?
A static class is a special class that is loaded into memory automatically by the CLR at the time of code execution.
When to use a static class?
A static class is useful when you want to provide common utilities like: configuration settings, driver functions, etc
What are sealed classes in C#?
Sealed classes are special types of class that is being restricted to be inherited. It is used to prevent inheritance
Can multiple catch blocks be executed?
No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the final block, and then the code that follows the final block gets executed.
What are value types and reference types?
- A value type holds a data value within its own memory space. Example
1
int a = 30;
- Reference type stores the address of the object where the value is being stored. It is a pointer to another memory location.
1
string b = "Hello";
What is an object pool in .NET?
An object pool is a container having objects ready to be used. It tracks the object that is currently in use, the total number of objects in the pool.
What do you mean by partial method?
A partial method is a special method within a partial class or struct. One part of a struct has the only partial method declaration means signature and another part of the same partial class may have an implementation.
What are the different ways a method can be overloaded?
Methods can be overloaded using different data types for a parameter, different order of parameters, and the different number of parameters.
Expert level questions
What is serialization and its main purpose in C#?
Serialization is the process of converting an object into a stream of bytes. That helps in storing that object into memory or a fill int he form of XML, JSON, etc. Its main purpose is to save the state of an object so that it can be recreated when it requires.
What is reflection in c#?
Reflection is used to examine objects and their types. The system. Reflection namespace contains classes that allow you to obtain information and about assemblies, modules, and types.
When you should use reflection?
It can be used in the following cases:
- To create an instance of a type or bind the type to an existing object dynamically.
- To get the type from an existing object and invoke its methods or access its fields and properties.
What do you mean by exceptions in C#?
Exceptions are unexpected or unseen errors that occur during the execution of a program. It can be caused due to the improper use of user inputs, system errors, etc
What is the role of System.Exception class?
System.Exception class is provided by .NET framework to handle any type of exception that occurs. The exception class is the base class for all other exception classes.
What is an exception Handling?
Exception handling is a method to capture run-time errors and handle them correctly. It is done by using Try-catch blocks and throw keyword.
What are the different types of serialization?
- XML serialization
- Binary serialization
- SOAP serialization
What are delegates?
A delegate is a reference type that holds the reference to a method class method. Any method which has the same signature as a delegate can be assigned to delegate.
What is Polymorphism in C#?
The ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. There are two types of polymorphism
- Compile-time polymorphism. The best example is Overloading
- Runtime polymorphism. The best example is Overriding
What is Method Hiding in C#?
If the derived class doesn’t want to use methods in the base class, the derived class can implement its own version of the same method with the same signature. For example, in the classes given below, DriveType() is implemented in the derived class with the same signature. This is called Method Hiding.
What are the uses of delegates in C#?
Below is the list of uses of delegates in C#
- Callback Mechanism
- Asynchronous Processing
- Abstract and Encapsulate method
- Multicasting
What is the property in C#?
A property acts as a wrapper around a field. It is used to assign and read the value from that field by using set and get accessors. The property can be created for a public, private, protected and internal field.
When you should use the property?
Its uses are
- To validate data before assigning it to a field.
- To log all access for a child.
What is the difference between “as” and “is” operators in C#?
- “as” operator is used for casting the object to type or class.
- “is” operator is used for checking the object with type and this will return a Boolean value.
What is an indexer in C#?
An indexer enables a class or struct instances to be indexed in the same way as an array. It is defined using this keyword.
How encapsulation is implemented in C#?
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member.
What are collections in C#?
A collection is a set of related objects. It is a class, so you must declare a new collection before you can add elements to that collection.
What do you mean by Generics in C#?
Generic is a class that allows the user to define classes and methods with the placeholder. Generics were added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, etc and user-defined types) to be a parameter to methods, classes, and interfaces.
Explain Anonymous type in C#?
Anonymous types allow us to create a new type without defining them. This is a way of defining read-only properties into a single object without having to define type explicitly.
Give a brief explanation on Thread Pooling in C#.
A collection of threads, termed as a Thread Pool in C#. Such threads are for performing tasks without disturbing the execution of the primary thread. After a thread belonging to a thread pool completes execution, it returns to the thread pool.
What is Multithreading in C#?
Multithreading allows you to perform more than one operation concurrently.The .NET framework System.Threading namespace is used to perform threading in C#
Explain different states of a Thread in C#?
A thread in C# can have any of the following states:
- Aborted – The thread is dead but not stopped
- Running – The thread is executing
- Stopped – The thread has stopped the execution
- Suspended – The thread has been suspended