C# interview questions and answers


31) What is the difference between string and StringBuilder in c#?
String

The string is immutable. Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create a new instance in memory to hold the new value.
String belongs to System namespace


=>Example
string str = "hi";
//It creates a new string instance instead of changing the old one
str += "help";
str += "test";


StringBuilder
String builder is mutable. Mutable means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.
StringBuilder belongs to System.Text namespace


=> Example
StringBuilder sb = new StringBuilder("");
//It does not creats new instance for every time.
sb.Append("hi");
sb.Append("help ");
string str = sb.ToString();

32) List out some of the exceptions in C#?
Below are some of the exceptions in C#-

1. ArgumentNullException
2. NullReferenceException
3. IndexOutOfRangeException
4. DivideByZeroException
5. StackOverflowException
6. InvalidOperationException etc.

33) What do you mean by the delegate in C#?

Delegates are type safe pointers, unlike function pointers as in C++. A delegate is used to represent the reference to the methods of some return type and parameters.
Delegates are required because they can be used to write much more generic type safe functions.

34) What are the types of delegates in C#?

Following are the types of delegates in C# -
1. Single Delegate
2. Multicast Delegate
3. Generic Delegate

35) What is a multicast delegate?

A delegate having multiple handlers assigned to it is called multicast delegate. Each handler is assigned to a method.

36) What is Nullable Type in C#?

Variable types do not hold null values so to hold the null values we have to use nullable types. nullable types can have values either null or other values as well.
Eg: int? nullvariable = null;

     
37) What do you mean by boxing and unboxing in C#?

Boxing: This is the process of converting from value type to reference type.

Example
int i = 67; // i is a value type
object o = i; // i is boxed
System.Console.WriteLine(i.ToString()); // i is boxed


UnBoxing: It’s the process of converting reference type to value type.

System.Collections.ArrayList list = new System.Collections.ArrayList(); // list is a reference type
int n = 67; // n is a value type
list.Add(n); // n is boxed
n = (int)list[0]; // list[0] is unboxed

38) Can "this" be used within a static method?

We can't use "this" in a static method because we can only use static variables/methods in a static method.

39) Explain is an interface class?

Interface class is an abstract class which has only public abstract methods & the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.

40) Explain Abstract class?

We create an abstract class when we define a template that needs to be followed by all the derived classes. Abstract class can have implementation which should be implemented in the child class.

41) What is the difference between Interface and Abstract class?
# Abstract class Interface Class
1. Abstract class can have implementation for some of its members or methods. Interface class can not have implementation for any its members.
2. Abstract class can have fields. Interface class can not have fields.
3. Abstract class can inherit from another Abstract class or another interface. Interface class can inherit from another interface class only. Interface cannot inherit from an abstract class.
4. Abstract class members can have access modifiers. Interface class members can not have access modifiers.
5. Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
e.g. Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}.
42) What is the difference between static & dynamic polymorphism?
# Static polymorphism Dynamic polymorphism
1. Method overloading would be an example of static polymorphism. It uses the concept of early binding(Compile time binding). Method Overriding would be an example of dynamic polymorphism. It uses the concept of late binding(Runtime Binding).
2. Static polymorphism is faster than dynamic polymorphism because of early binding. Dynamic polymorphism is faster than static polymorphism because of late binding.
3. Static polymorphism is less flexible as all things execute at compile time. Dynamic polymorphism is more flexible as all things execute at run time.
4. It is achieved by function overloading and operator overloading. It is achieved by abstract classes and virtual functions.
43) What are the ways a method can be overloaded?

Methods can be overloaded using the different order of parameters, data types for the parameter, and using a different number of parameters.

44) Explain 'Virtual' Keyword?

The virtual keyword is used while defining a class to specify that the method and properties of that class can be overridden in derived class.

45) What is generics in C#.NET?

Generics are used to make reusable code classes to decrease the code redundancy, increase type safety and performance. Using generics, we can create a collection of classes. To create a generic collection, System.Collections.Generic namespace should be used.


« 1 2 3 4 5 »
 

Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.