Basic C# Interview Questions on classes and structs



Difference between classes and structs - Video

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.

5 comments:

  1. If a Class derives from another class will the derived class automatically contain all public protected internal members of the base class?

    If the derived class is in a different assembly, internal members will not be accessible.(Protected Internal Members will be accessible across the assembly)

    ReplyDelete
  2. yes a derived class can have both public and protected members of base class.....but
    in the case the base class is in another assembly protected members can not be inherited

    ReplyDelete
  3. I want to send email using smtp server in asp.net c#. could you please help me.

    ReplyDelete
  4. The default access modifier for a class is internal, because when we declare private to a class member it will throw an error saying a class can not be private, protected or protected internal.

    ReplyDelete
  5. A class can contain following 10 members
    ------------------------------------------------
    1.DataFields/classfields
    2.Properties
    3.Indexers
    4.Constants
    5.Nested Types
    6.Constructors
    7.Destructors
    8.Events
    9.Functions/Methods
    10.Operators.

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us