CSharp Basic Questions and Answers

What is the difference between a class and an object?
A class defines a new type; an object is a single instance of that type.

Where are reference types created?
Instances of classes are reference types and are created on the heap.

Where are value types created?
Intrinsic types (such as integers) and structs are value types and are created on the stack.

What does the keyword private do?
Access is limited to methods of the defining class.


What does the keyword public do?
Access is available to methods in any class.

What method is called when you create an object?
The class's constructor is called.

What is a default constructor?
A default constructor is a constructor that takes no parameters. If you do not create any constructor at all for your class, a default constructor is implicitly created.

What types can a constructor return?
None. A constructor is not defined to return a type, and is not marked void.

How do you initialize the value of a member variable in a class?
Either in the constructor, using assignment, or when the member variable is created:
private int myVariable = 88;
Technically, only the latter is truly initialization; assigning it in the constructor is not as efficient.

What does the keyword this refer to?
this refers to the object itselfthe current instance of the class.

What is the difference between a static method and an instance method?
A static method has no this reference. It does not belong to an instance; it belongs to the class and can only call other static methods.

You access a static method through the name of the class:

Dog myDog = new Dog( );
myDog.InstanceMethod( );
Dog.StaticMethod( );


Of course, from within any method (including static methods), you can instantiate a class, and then call methods on that instance.

You can even instantiate an instance of your own class, and then call any non-static method of that object, as we did with [static] Main( ) calling [non-static] Test( ).

What does the using statement do?
The using statement automatically calls the dispose method on the object once the statement completes.

1 comment:

Super Hero said...

vow. Superb Thalai.