Interface vs Abstract Class

August 4, 2010 Leave a comment

Technical Differences

S.No. Interface Abstract Class
1. Consists of only four type of members

(1) Method (2) Property (3) Event (4) Indexer

As a normal class it can have all supported members
2. Declaration only allowed. There will not be any implementation or definition (i.e., can not have virtual and concrete members and only abstract members are allowed)

It supports

1) Abstract members (only declaration)

2) Virtual members (with definition)

3) Concrete members (with definition)

3. No access modifiers can be used. By default members are abstract and public Modifiers allowed and to define abstract member ‘abstract’ keyword is used
4. A class that inherits an interface should implement all the members defined in the interface. No ‘override’ keyword is used but ‘public’ keyword should be used. A class that inherits an abstract class should implement all abstract members defined in the abstract class. ‘override’ keyword is used.

For virtual members, the class can either override or not.

5. A class can inherit more than one interfaces A class can not inherits more than one abstract class
6. A struct can inherit interfaces A struct can not an abstract class

Usage and Functional Differences

S.No. Interface Abstract Class
1. As the name implies, it generally used as a contract for classes.  It only can define signatures not the implementations that the derived classes should implement the same. Abstract class offers variety in its implementation and it is generally used to encapsulate common functionalities for both all derived classes and particular derived classes.
2. It is well suitable for sending an object via interface to ensure ‘data hiding’ because only the members defined in interface can be accessed from the interface all other members will be hidden. It is well suitable for the situation to extend common functionalities (versioning).  Adding a common method in the base abstract class is enough and it will automatically make all the derived classes have the new method.
3. In structure inheritance, when casting the structure to interface object, a temporary object will be created by boxing (value type to reference type) and making any changes in interface object will not affect the original struct object. Since no boxing required (because both class and interface are reference types), making any changes in interface object will affect the original struct object. This is because; both are same object with two references.

Technical Difference

Non CLS-Compliant Codes in C#

July 28, 2010 Leave a comment

 

Common Language Specification (CLS) is set of basic rules and it is expected that a dotnet language must satisfy them. Though C# is probably the biggest language in dotnet framework, it supports many non-CLS-compliant codes mainly to support and maintain the legacy language nativity. Writing C# programs by using only CLS-compliant codes give great portablity among other dot-net programs written in other dotnet langauges. This article tries to list non CLS-compliant codes in C#.

 

1. Two or more public / protected / protected internal members defined with only case difference

public int intA = 0;
public int INTA = 2;

public int x = 0;

public void X()
{
}

 

2. Declaring a member name preceding underscore (_) with outside visible access specifiers (public, protected, protected internal)

public void _test()
{
:::::::
}

 

3. Declaring unsigned type member with access public / protected / protected internal

public uint a = 10;

 

4. Declaring unsafe (pointer) type with access public / protected / protected internal

protected internal unsafe int *c;

 

5. A (public / protected / protected internal) method returning a non-CLS compliant type or with parameter of non-CLS compliant type

public uint testmethod(int x, int y)
{
::::::::
}

public void anothermethod(ushort x, int y)
{
::::::::
}

 

6. An abstract member makred as non-CLS-Compliant in a CLS-Compliant class.

[CLSCompliant(true)]
public abstract class B
{
[CLSCompliant(false)]
protected internal abstract void test();
}

The above codes are not CLS-compliant and one should avoid these type of coding to ensure CLS-Compliant code. Many of CLS are followed by C# and the above list mentioned some of them are not pursued by C#.

 

There is an attribute, called CLSCompliant, to check whether our code is written in CLS-compliant or not. By giveing CLSCompliant attribute with true parameter, we can get warning message for all non CLS-Compliant codes.

using System;
[assembly: CLSCompliant(true)]
public class Test
{
public static int a = 10;

public int intA = 0;
public int INTA = 2;

public int x = 0;
public void X()
{
}
protected uint testmethod(int x, int y)
{
return 0;
}
}

 

[assembly: CLSCompliant(true)] means the CLS-Compliant is enabled to the assembly. After setting this, compiler will check all codes in the assembly for CLS-Compliant and if we want any class or method to not check for CLS-Compliant we can give false for that member alone.

using System;
[assembly: CLSCompliant(true)]
[CLSCompliant(false)]
public class clsA
{
public unsafe int* x;
}

public class clsB
{
public unsafe int* x;
}

[CLSCompliant(true)]
public class clsC
{
public unsafe int* x;
}

The clsA class will not be checked by compiler for CLS-Compliat but compiler will do for clsB and clsC.

Some Useful C# attributes

July 27, 2010 Leave a comment
S.No. Attribute Description & Usage
1. Obsolete Marks a program entity (method, property, field, etc) that should not be used.  This is useful to make an old/ unwanted/ outdated method as not usable one without removing or commenting it.

[Obsolete]
Public  method OldCompute(int x, int y)
{

}

-  above code give a warning message during compilation


[Obsolete("Do not use this method", true)]

-   above code will give error message with given message

2. Category It groups a property under a category in property grid.
3. Description It is used to set description to a property

[Category("Appearance")]

[Description("The caption")]
Public  string Caption
{
get  { return _caption; }
set  { _caption = value; }
}

4. Conditional It defines a method whether it will be executed or not when a call.

#define “Debug”
..
..
[Conditional("Debug")]
public  void  LogMsg(string  msg)
{

}
..
..
LogMsg(“reached here”)
..
#undef “Debug”
..
..
[Conditional("Debug")]
public  void  LogMsg(string  msg)
{

}
..
..
LogMsg(“reached here”)
..
Here the LogMsg method will be executed Here the LogMsg method will not be executed
5. DllImport It is used to import an external dll or API

using System.Runtime.InteropServices;
public class test
{
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency,int duration);
static void Main()
{
Beep(1000,111);
}
}

6. CLSComplaint It is used to tell the compiler to check whether the code is CLSComplaint or not. If it is true the compiler will give warning message for non-CLSComplaint code.

[assembly:CLSCompliant(true)]

the above attribute’s target is assembly.

[CLSCompliant(true)]

the target can be a class or method and before this it is must to set assembly level CLSCompliant attribute.

Follow

Get every new post delivered to your Inbox.