Wednesday, December 24, 2008

Modifiers - Abstract, Sealed, Static

C# provides many modifiers for use with types and type members. Of these, three can be used with classes: abstract, sealed and static.

Abstract
Indicates that a class is to be used only as a base class for other classes. This means that you cannot create an instance of the class directly. Any class derived from it must implement all of its abstract methods and accessors. Despite its name, an abstract class can possess non-abstract methods and properties.

Sealed
Specifies that a class cannot be inherited (used as a base class). Note that .NET does not permit a class to be both abstract and sealed.

Static
Specifies that a class contains only static members (.NET 2.0).

Structs vs classes in C#

Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.
• When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.
• Structs can declare constructors, but they must take parameters. It is an error to declare a default (parameterless) constructor for a struct. Struct members cannot have initializers. A default constructor is always provided to initialize the struct members to their default values.

• When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator. If you do not use New, the fields will remain unassigned and the object cannot be used until all the fields are initialized.
• There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class object. A struct can implement interfaces, and it does that exactly as classes do,
• Structs are simple to use and can prove to be useful at times. Just keep in mind that they're created on the stack and that you're not dealing with references to them but dealing directly with them. Whenever you have a need for a type that will be used often and is mostly just a piece of data, structs might be a good option.

OOPS - Access Specifiers

“The ability to define a class and create instances of classes is one of the most important capabilities of any Object Oriented Language”

Q: How will you define a CLASS ?
A: All classes in Visual Basic. NET are defined in a .VB file (as oppose to .CLS file in vb6), Also .VB file may contain one or more classes. The basic syntax of a class is as follows:

Class ClassName

End Class

PublicProtectedFriendProtected FriendPrivate Class Vehicle

End Class

Q: List All Class Access Specifiers?
A: There are five access specifiers in Visual Basic .NET defined as follows:

Public – Applied at the class level and is the most common specifier. Public Classes are classes that are intended to be used by any consumer.

Public Class PubClass
End Class

Protected - can only be applied to Nested Classes. Are only accessible within the class and within the child classes.

Public Class HasProtected
Protected Class ProtectedClass

End Class
End Class

Friend – Are accessible only within the program in which they are defined. If you add the Friend access specifier to a class definition, instance of that class can only be created from within th same program.

Friend Class FriendClass
Public Shared Sub PublicMethod()
MsgBox("FriendClass.PublicMethod")
End Sub
End Class

Protected Friend - Represents a union of the Protected and Friend Specifiers. Protected class must be nested class, thus Protected Friend class must be nested too.

Public Class HasProtectedFriend
Protected Friend Class ProtectedFriend
Public Shared Sub Test()
MsgBox("test")
End Sub
End Class
End Class

Private – Can only be applied to a nested class. A Private nested class represents implementation details of the class. When you have complex problem that requires more problem solving horsepower then simple methods can provided, defining a nested private class that implements the abstraction.
Public Class HasPrivate
Private Class PrivateClass
End Class
End Class

Q: What is a Field?
A: Field is a Data Member of a class. Fields can be ValueType members, like Integer or Dates, or can be aggregate types, like structures or classes.

Q: What is a Property?
A: A Property is a special member constructor that is used like a field, but acts like a method. Properties are special kind of methods that generally are used to provide constrained access to Field.

Q: What are the Indexed Properties?
A: Indexed Properties are simply property methods that have a mandatory parameter. The mendatory parameter is semantically treated like an index Index properties has an argument between the parentheses. This argument doesn't represent the field value; rather, it represents an index to an underlying field value. The fundamental idea behind indexed properties is that you can wrap arrays and other kind of collections of data safely behind property methods.

Public Class Indexed
Private FStrings() As String = {"One", "Two", "Three"}

Public Property Strings(ByVal index As Integer) As String
Get
Return FStrings(index)
End Get
Set(ByVal value As String)
FStrings(index) = value
End Set
End Property
End Class

'//useage
'Msgbox(objIndex.Strings(1))

Q: Explain Default Properties?
A: Default Properties must be indexed properties, you can have only one default property per class and you can invoke property setter and getter methods on a default property using the verbos or shorthand form.

Public Class Indexed
Private FStrings() As String = {"One", "Two", "Three"}

Default Public Property Strings(ByVal index As Integer) As String
Get
Return FStrings(index)
End Get
Set(ByVal value As String)
FStrings(index) = value
End Set
End Property
End Class

'//useage
'Msgbox(objIndex.Strings(1))
'Msgbox(objIndex(1)

Q: What are ReadOnly, WriteOnly and Shared Properties?
A: Read Only property is a property that can be used as an r- value only. That's why a property statement that includes a read only modifier will generate a getter block only and users can evaluate this property but can not modify it.

Public ReadOnly Property Strings(ByVal index As Integer) As String
Get
Return FStrings(index)
End Get
End Property

Write Only – is a property that a consumer can modify but can't view. This implements a property setter only.

Public WriteOnly Property Strings(ByVal index As Integer) As String
Set(ByVal value As String)
FStrings(index) = value
End Set
End Property

Shared Property – is a property that a shared members can also be invoked using instances.

Public Property Strings(ByVal index As Integer) As String
Get
Return FStrings(index)
End Get
End Property

Q: What is Constructor and Destructor ?
A: A constructor is called to Initialize a class. A destructor is called to Finalize the class. Visual Basic.NET implements the constructor as Sub New and Destructor as protected method Sub Finalize().

Q: what is MyBase and MyClass?
A: MyBase allows you to invoke methods in your class's Base Class that may be overloaded in your class, resolving any name ambiguity.

MyClass is roughly equivalent to the Me reference to self.

Q: what is Method Overloading?
A: Method Overloading means to have two or more methods in the same class with different signature. The benefit of method overloading is that it allows you to implement methods that supports the same semantic operation but differ by argument number or type.
Public Overloads Sub GetCustomer(ByVal strCustomerName As String)
End Sub

Public Overloads Sub GetCustomer(ByVal iCustomerID As Integer)
End Sub

Q: what is Method OverRiding?
A: Method Overriding changing the behavior of a method in a base class. Use keyword Overrides.

Q: what are Overridable, MustOverride and NotOverridable modifiers?
A: Overridable – modifier indicates that a method can be overriden.
NotOverridable- modifier indicates that you can not override a method.
MustOverride- modifier indicates that a method is abstract, and child class must implement the MustOverride method in a parent class.

Q: what Shadows modifier?
A: Shadows – if you want a child class to use a name previously used in a parent class, use the Shadows keyword to do so. Shadows keyword simply allows you to reintroduce a previously used name in the child class without a compiler error.


Partial Classes Concept in Asp.Net 2.0One of the language enhancements in .NET 2.0 - available to both VB2005 and C# 2.0 programmersis support for partial classes. In a nutshell,

Partial classes mean that your class definition can be split into multiple physical files. Logically, partial classes do not make any difference to the compiler. During compile time, it simply groups all the various partial classes and treats them as a single entity.

Benefits of Partial Classes One of the greatest benefits of partial classes is that they allow a clean separation of business logic and the user interface (in particular, the code that is generated by the Visual Studio Designer). Using partial classes, the UI code can be hidden from the developer, who usually has no need to access it anyway. Partial classes also make debugging easier, as the code is partitioned into separate files. This feature also helps members of large development teams work on their pieces of a project in separate physical files.