Sunday, October 31, 2010

C# concepts

1.     Explain about Protected and protected internal, “internal” access-specifier?
protected – Access is limited to the containing class or types derived from the containing class.
internal – Access is limited to the current assembly.
protected internal – Access is limited to the current assembly or types derived from the containing class.
 
2.     Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?
(Class constructor method is also known as type constructor or type initializer)
Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. (It will get executed only 1st time, when we call any static methods/fields in the same class.) Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention.
A static constructor is used to initialize a class. It is called automatically to initialize the class before the first instance is created or any static members are referenced.
3.     What is Private Constructor? and it’s use? Can you create instance of a class which has Private Constructor?
A: When a class declares only private instance constructors, it is not possible for classes outside the program to derive from the class or to directly create instances of it. (Except Nested classes)
Make a constructor private if:
- You want it to be available only to the class itself. For example, you might have a special constructor used only in the implementation of your class’ Clone method.
- You do not want instances of your component to be created. For example, you may have a class containing nothing but Shared utility functions, and no instance data. Creating instances of the class would waste memory.
4.     I have 3 overloaded constructors in my class. In order to avoid making instance of the class do I need to make all constructors to private?
(yes)
5.     Overloaded constructor will call default constructor internally?
(no)


No comments:

Post a Comment