1. Can we call a base class method without creating instance?

Yep. But ..

► Its possible If its a static method.

► Its possible by inheriting from that class also.

► Its possible from derived classes using base keyword.

2. In which cases you use override and new base?

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

3. What is a private constructor? Where will you use it?

When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn't allow to create object for its derived class, i.e you loose inherent facility for that class.

Example:

Class A

{

// some code

Private Void A()

{

//Private Constructor

}

}



Class B:A

{

//code

}



B obj = new B();// will give Compilation Error



Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error.

4. Can we declare private class in a Namespace?

No. If you try to create a private class in a Namespace, Compiler will throw a compile time error “Namespace elements cannot be explicitly declared as private, protected, or protected internal”.

Reason: The message says it all. Classes can only be declared as private, protected or protected internal when declared as nested classes, other than that, it doesn't make sense to declare a class with a visibility that makes it unusable, even in the same module. Top level classes cannot be private, they are "internal" by default, and you can just make them public to make them visible from outside your DLL.

5. What is Polymorphism?

In OPP'S, polymorphism(Greek meaning “having multiple forms”) is the ablity of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a a function, or an object to have more than one forms.

In C# :
Parent classes may define and implement “virtual” methods(Which is done using the “virtual” keyword), and derived classes can override them(using the “override” keyword), which means they provide their own definition and implementation.At run-time, when user's code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code when a method of the base class is called it executes the overriden method.

6. What Are Attributes in DotNet?

An Attribute is a declarative tag which can be used to provide information to the compiler about the behaviour of the C# elements such as classes and assemblies.
C# provides convenient technique that will handle tasks such as performing compile time operations , changing the behaviour of a method at runtime or maybe even handle unmanaged code.
C# Provides many Built-in Attributes

Some Popular ones are

- Obsolete
- DllImport
- Conditional
- WebMethod

and Many more.
Members please keep on posting more responses providing more In-Built attributes.

Regards Hefin Dsouza

7. What can you do to make class available for inheritance but you need to prevent its method to come in inheritance chain?

Well, Declare a class with public access specifier and mark all it's method to sealed . As anything which is declared with sealed keyword cannot be inherited.

8. Whats the Difference between Interface and Abstract Class?

Abstract Class:
Have constructors.
Not necessarily for the class inheriting it to Implement all the Methods.
Doesn't Support Multiple Inheritance.

Where everything is Opposite in the Interfaces.

9. What are the various types of Constructor

Public : Accessible to All
Private: Those classes in which only static members are there and you don't want there objects to be created in any class.
Static: Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
Intern: implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly (Namespace).
and External

10. What are Constructors?

Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization.

If no constructor defined then the CLR will provide an implicit constructor which is called as Default Constructor.

A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.

Constructors do not return a value
Constructors can be overloaded

Download Interview PDF