Dot Net Coding Standard: UI Control Naming Conventions

UI controls would use the following prefixes.  The primary purpose was to make code more readable.

Control Type Prefix
Button btn
CheckBox chk
CheckedListBox lst
ComboBox cmb
ContextMenu mnu
DataGrid dg
DateTimePicker dtp
Form suffix: XXXForm
GroupBox grp
ImageList iml
Label lb
ListBox lst
ListView lvw
Menu mnu
MenuItem mnu
NotificationIcon nfy
Panel pnl
PictureBox pct
ProgressBar prg
RadioButton rad
Splitter spl
StatusBar sts
TabControl tab
TabPage tab
TextBox tb
Timer tmr
TreeView tvw
Share this:
Share

OOP – What is abstract ?

The abstract modifier can be used with classes, methods, properties, indexers, and events.

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

Abstract classes have the following features:

  1. An abstract class cannot be instantiated.
  2. An abstract class may contain abstract methods and accessors.
  3. It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  4. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
  5. Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.

Abstract methods have the following features:

  • An abstract method is implicitly a virtual method.
  • Abstract method declarations are only permitted in abstract classes.
  • Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature. For example:public abstract void MyMethod();
  • The implementation is provided by an overriding method, which is a member of a non-abstract class.
  • It is an error to use the static or virtual modifiers in an abstract method declaration.
  • Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the abstract modifier on a static property.
  • An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
  • An abstract class must provide implementation for all interface members.

An abstract class that implements an interface might map the interface methods onto abstract methods.

Example:

interface I
{
void M();
}

abstract class C: I
{
public abstract void M();
}

In this example, the class MyDerivedC is derived from an abstract class MyBaseC. The abstract class contains an abstract method, MyMethod(), and two abstract properties, GetX() and GetY().

Example:
// abstract_keyword.cs
// Abstract Classes

using System;
abstract class MyBaseC // Abstract class
{
protected int x = 100;
protected int y = 150;
public abstract void MyMethod(); // Abstract method

public abstract int GetX // Abstract property
{
get;
}

public abstract int GetY // Abstract property
{
get;
}
}

class MyDerivedC: MyBaseC
{
public override void MyMethod()
{
x++;
y++;
}

public override int GetX // overriding property
{
get
{
return x+10;
}
}

public override int GetY // overriding property
{
get
{
return y+10;
}
}

public static void Main()
{
MyDerivedC mC = new MyDerivedC();
mC.MyMethod();
Console.WriteLine(“x = {0}, y = {1}”, mC.GetX, mC.GetY);
}
}

Output
x = 111, y = 161

In the preceding example, if you attempt to instantiate the abstract class by using a statement like this:

MyBaseC mC1 = new MyBaseC(); // Error

you will get the following error message:
Cannot create an instance of the abstract class ‘MyBaseC’.

Share this:
Share

C# – What is readonly variable and how to use it

The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

You can assign a value to a readonly field only in the following contexts:

When the variable is initialized in the declaration, for example:

public readonly int y = 5;

For an instance field, in the instance constructors of the class that contains the field declaration, or for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a readonly field as an out or ref parameter.

Example

// Readonly fields

using System;
public class ReadOnlyTest
{
class MyClass
{
public int x;
public readonly int y = 25; // Initialize a readonly field
public readonly int z;
public MyClass()
{
z = 24; // Initialize a readonly instance field
}

public MyClass(int p1, int p2, int p3)
{
x = p1;
y = p2;
z = p3;
}
}

public static void Main()
{
MyClass p1= new MyClass(11, 21, 32); // OK
Console.WriteLine(“p1: x={0}, y={1}, z={2}” , p1.x, p1.y, p1.z);
MyClass p2 = new MyClass();
p2.x = 55; // OK
Console.WriteLine(“p2: x={0}, y={1}, z={2}” , p2.x, p2.y, p2.z);
}
}

Output:
p1: x=11, y=21, z=32
p2: x=55, y=25, z=24

In the preceding example, if you use a statement like this:
p2.y = 66; // Error

you will get the compiler error message:
The left-hand side of an assignment must be an l-value
which is the same error you get when you attempt to assign a value to a constant.

Note:

  • The readonly keyword is different from the const keyword.
  • A const field can only be initialized at the declaration of the field.
  • A readonly field can be initialized either at the declaration or in a constructor.

Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks;

Share this:
Share

C# – What is boxing and unboxing in .net it

Boxing_Unboxing-1When we move a value type to reference type the data is moved from the stack to the heap. When we move reference type to a value type the data is moved from the heap to the stack.

This movement of data from the heap to stack and vice-versa creates a performance hit.

When the data moves from value types to reference types its termed as ‘Boxing’ and the vice versa is termed as ‘UnBoxing’.

If you compile the above code and see the same in ILDASM you can see in the IL code how ‘boxing’ and ‘unboxing’ looks, below figure demonstrates the same.
Boxing_Unboxing-2

Performance implication of Boxing and unboxing

In order to see how the performance is impacted we ran the below two functions 10,000 times. One function has boxing and the other function is simple. We used a stop watch object to monitor the time taken.

The boxing function was executed in 3542 MS while without boxing the code was executed in 2477 MS. In other words try to avoid boxing and unboxing. In project you always need boxing and unboxing , use it when it’s absolutely necessary.
Boxing_Unboxing-3

Share this:
Share

OOP – What is virtual method?

  • Virtual method is a method whose behavior can be overridden in derived class.
  • Virtual method allows declare a method in base class that can be redefined in each derived class.
  • When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
  • The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
  • By default, methods are non-virtual. You cannot override a non-virtual method.
  • You cannot use the virtual modifier with the static, abstract, private or override modifiers.
  • Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Share this:
Share