Basic c# interview question and answers

What do you mean by saying a "class is a reference type"?
A class is a reference type means when an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

What do you mean by saying a "struct is a value type"?
A struct is a value type mean when a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

When do you generally use a class over a struct?
A class is used to model more complex behavior, or data that is intended to be modified after a class object is created. A struct is best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

List the 5 different access modifiers in C#?
1.
public
2. protected
3. internal
4. protected internal
5. private

If you donot specify an access modifier for a method, what is the default access modifier?
private

Classes and structs support inheritance. Is this statement true or false?
False, Only classes support inheritance. structs donot support inheritance.

If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class?
Yes, the derived class will automatically contain all the public, protected, and internal members of the base class except its constructors and destructors.

Can you create an instance for an abstract class?
No, you cannot create an instance for an abstract class.

How do you prevent a class from being inherited by another class?
Use the sealed keyword to prevent a class from being inherited by another class.

Classes and structs can be declared as static, Is this statement true or false?
False, only classes can be declared as static and not structs.

Can you create an instance of a static class?
No, you cannot create an instance of a static class.

Can a static class contain non static members?
No, a static class can contain only static members.

No comments:

 
Top Blogs