Wednesday, July 6, 2011

Classes , Structs and Interfaces

Classes
What can be the members of class, struct and interfaces?
Class: (http://msdn.microsoft.com/en-us/library/0b0thckt.aspx)
·         Constructors
·         Destructors
·         Constants
·         Fields
·         Methods
·         Properties
·         Indexers
·         Operators
·         Events
·         Delegates
·         Classes
·         Interfaces
·         Structs

Struct: (http://msdn.microsoft.com/en-us/library/ah19swz4.aspx)

Interfaces :  methods, properties, events, indexers  (http://msdn.microsoft.com/en-us/library/ms173156.aspx)


Class
Struct
Interface
Constructors
Y
Y
N
Destructors
Y
N
N
Constants
Y
Y
N
Fields
Y
Y
N
Methods
Y
Y
Y
Properties
Y
Y
Y
Indexers
Y
Y
Y
Operators
Y
Y
N
Events
Y
Y
Y
Can a static class contain non-static members?
NO. (http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx).
The main features of a static class are:
·         They only contain static members.
·         They cannot be instantiated.
·         They are sealed.
·         They cannot contain Instance Constructors.
Constructors:
- Constructors cannot be “virtual”.
- They cannot be inherited.
- Constructors are called in the order of inheritance.
- Constructors can be overloaded.
- special function that is called when class is instantiated.
- same name as the class.
- does not have a return type.
- used to initialize values.
- can have parameters.
- compiler will supply one if not declared.
Can you define constructors for struct?
We cannot define default constructor for structs, because one is compulsorily provided by compiler.
However, we can define constructors for structs that take parameters.

Remember that the default constructor is called when we use new with struct. However, objects based on structs can be initialized or assigned and then used.
e.g. int I = new int();  //valid ; can be used
int i ; I = 33; //valid ; can be used
int I; // cannot be directly used without assigning a value, because new is not given, hence value is not initialized.
Can you define a default constructor for a strut ?
NO. It is compulsorily provided by compiler.
In how many type value types can be created? 
By calling new : int I = new int();
By assigning a value: int I = 44; //using new is not a requirement.
A base class has no default constructor but has some parametered constructors. Will the derived class have to do something?
If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor by using base. (MSDN http://msdn.microsoft.com/en-us/library/ms173115.aspx#Y1200)
Can constructors be inherited? NO.
What is the order of calling constructors and destructors in an inheritance chain ?
Constructors : Least derived (base) to Most Derived.
Destructors : Most Derived to Least Derived. (http://msdn.microsoft.com/en-us/library/66x5fx1b(v=vs.80).aspx)
Consider the following :
Class A {
               Public A() {}
              ~A(){}
}
Class B  : A {
               Public B() {}
              ~B(){}
}


What will be the order of ctor/dtor in following cases ?
B b = new A(); //INVALID
A a = new B();  // ctor A, ctor B , dtor B , dtor A
In a nested class, can you access members of container class ?
NOT DIRECTLY, you have to pass an instance of container to nested class either in nested class’s ctor or some other method.
Destructors :
·         Destructors cannot be defined in structs. They are only used with classes.
·         A class can only have one destructor.
·         Destructors cannot be inherited or overloaded.
·         Destructors cannot be called. They are invoked automatically.
·         A destructor does not take modifiers or have parameters.
What are the differences between classes and structs ?
Class
Struct
Classes are reference type
Struct is a  value type
Classes can inherit from another class  or interface but not from a class (struct cannot be a base of a class)
Structs cannot inherit from other structs or classes
A struct cannot inherit from another struct or class, and it cannot be the base of a class
http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx
Classes can have destructors
Structs cannot have destructors.
Encapsulation
What is the default access level of members in a class?
Private. This is applicable to constructor also, if a constructor is provided and no access specifiers are provided for it.
However, if you do not provide a default constructor to a class, the default parameterless constructor provided by the compiler is public.
Can you inherit from a class which has only  private constructors ?
No. It is not possible to inherit a class that has only a private constructor.
Can you inherit from a static class ?
NO. Static classes are sealed and cannot be inherited. http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
Can a static class contain not static methods?
Inheritance
What is class inheritance and interface inheritance?

Can you inherit from a class which has only  private constructors ?
No. It is not possible to inherit a class that has only a private constructor.
What is the difference in virtual and abstract methods?
When talking about the base class, a virtual method may or may not contain implementation. But, an abstract method cannot contain implementation.
When talking about the (non-abstract) derived class, a virtual method may or may not be overridden, but an abstract method must be overridden ( or actually implemented).



Polymorphism
How many types of overloading are supported?
Method overloads and operator overloading.
What is operator overloading?

No comments:

Post a Comment

 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...