Static Class
- Static classes can not be instantiated.
- Static classes are sealed so they can not be inherited.
- Only static members are allowed.
- Static classes can only have static constructor to initialize static members. They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created.
- Static Class members are Called Like “ClassName.MemberName”
- In normal class, we can create object, So the compiler cannot know the class member address directly at compile time; runtime only the get a address based on objects and isolate the method/member by using “this” keyword ( you won’t give like this.FunctionName(), it will take automatically at the creation of Object ) at runtime. But the Static Class defined at compile time, so it will be faster.
Example
//App_Code Page
static class PrinterCall
{
/// <summary>
/// Copy the items from one dropdown list to another dropdown list
/// </summary>
/// <param name="ddlSource">Source DropDown list</param>
/// <param name="ddlDestination">Destination DropDown list</param>
public static void CopyDropDownList(DropDownList ddlSource, DropDownList ddlDestination)
{
ListItem[] myListItemArray = new ListItem[ddlSource.Items.Count];
ddlSource.Items.CopyTo(myListItemArray, 0);
ddlDestination.Items.AddRange(myListItemArray);
}
}
//Aspx or any other Codebehind
protected void Page_Load(object sender, EventArgs e)
{
CommonBehavior.CopyDropDownList(ddlSource, ddlDestination);
}
Can Struct be inherited?
No, Structure can't be inherited as this is implicitly sealed.What is Virtual keyword?
This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.What is new Modifier?
The new modifiers hides a member of the base class. C# supports only hide by signature.
Default Modifiers :
An enum has default modifier as public
A class has default modifiers as Internal . It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal
An interface has default modifier as public
A struct has default modifier as Internal and it can declare its members (methods etc) with following access modifiers:
public
internal
private
A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.
Abstraction -> Hiding the unwanted informations and giving the relevant information.
Early binding means that our code directly interacts with the object, by directly calling its methods. Since the compiler knows the object's data type ahead of time, it can directly compile code to invoke the methods on the object.
Ex : string s;
Ex : string s;
Late binding means that our code interacts with an object dynamically at run-time. This provides a great deal of flexibility since our code literally doesn't care what type of object it is interacting with as long as the object supports the methods we want to call.
Ex : a = new Object();
Ex : a = new Object();
07:42 |
Category:
C# Concepts
|
0
comments
Comments (0)