nterfaces
An interface is a reference type containing only abstract members. These can be events, indexers, methods or properties, but only the member declarations. A class implementing an interface must provide the implementation of the interface members. An interface cannot contain constants, constructors, data fields, destructors, static members or other interfaces. Interface member declarations are implicitly public.
[edit]
Declaration
[attributes] [modifiers]
interface identifier [:base-type[,]]
{
body [;]
}
The attributes is optional and is used to hold additional declarative information.
The modifier is optional. The allowed modifiers are public and internal, unless nested inside a class, then the allowed modifiers are public, protected, private and internal. If no modifier is supplied then a default of internal is used.
The keyword interface must be followed by an identifier that names the interface.
The base-type of an interface can be zero or more interfaces. When more than one base-type is used, then this is a comma-separated list of base-types.
The body contains the member declarations.
[edit]
Implementation
An interface is defined using the keyword interface. It is common practice to start all interface names with a capital I. For example:
public interface IVehicle
{
void Start();
void Drive();
void Park();
void ChangeGear(int gear);
void SwitchOff();
}
In order to implement the interface, every method must be implemented in the class, else a compiler error will ensue.
public class Vehicle : IVehicle
{
public void Start()
{
Console.WriteLine("The vehicle has been started");
}
public void Drive()
{
Console.WriteLine("The vehicle is being driven");
}
public void Park()
{
Console.WriteLine("The vehicle is being parked");
}
public void ChangeGear(int gear)
{
Console.WriteLine("Gear changed to " + gear.ToString());
}
public void SwitchOff()
{
Console.WriteLine("The vehicle has been switched off");
}
}
If a class implements more that one interface and the two interfaces have methods named the same, then each method must be implemented unless they have the same definition in which case only one implementation is needed to implement the methods
Abstract classes
Like an interface, you cannot implement an instance of an abstract class, however you can implement methods, fields, and properties in the abstract class that can be used by the child class.
For example, we could create an abstract class for all vehicle to inherit from:
public abstract class Vehicle
{
public void Start()
{
Console.WriteLine("The vehicle has been started");
}
public abstract void Drive();
public abstract void Park();
public abstract void ChangeGear(int gear);
public void SwitchOff()
{
Console.WriteLine("The vehicle has been switched off");
}
public virtual void Operation1()
{
}
}
So each class that inherits from Vehicle will already be able to use the methods Start and SwitchOff, but they must implement Drive, Park and ChangeGear.
So if we were to implement a Car class, it may look something like this.
public class Car : Vehicle
{
public Car()
{
}
public override void Drive()
{
Console.WriteLine("The car is being driven");
}
public override void Park()
{
Console.WriteLine("The car is being parked");
}
public override void ChangeGear(int gear)
{
Console.WriteLine("The car changed gear changed to " + gear.ToString());
}
public new void Operation1() { }
}
The override keyword tells the compiler that this method was defined in the base class.
Summary
* An Interface cannot implement methods.
* An abstract class can implement methods.
* An Interface can only inherit from another Interface.
* An abstract class can inherit from a class and one or more interfaces.
* An Interface cannot contain fields.
* An abstract class can contain fields.
* An Interface can contain property definitions.
* An abstract class can implement a property.
* An Interface cannot contain constructors or destructors.
* An abstract class can contain constructors or destructors.
* An Interface can be inherited from by structures.
* An abstract class cannot be inherited from by structures.
* An Interface can support multiple inheritance.
* An abstract class cannot support multiple inheritance.
Thursday, December 17, 2009
C# – Static Members
A C# class can contain both static and non-static members. When we declare a member with the help of the keyword static, it becomes a static member. A static member belongs to the class rather than to the objects of the class. Hence static members are also known as class members and non-static members are known as instance members.
In C#, data fields, member functions, properties and events can be declared either as static or non-static. Remember that indexers in C# can't declared as static.
Static Fields
Static fields can be declared as follows by using the keyword static.
class MyClass
{
public static int x;
public static int y = 20;
}
When we declare a static field inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded first time.
For example
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyClass
{
public static int x = 20;
public static int y;
public static int z = 25;
public MyClass(int i)
{
x = i;
y = i;
z = i;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1},{2}",MyClass.x,MyClass.y,MyClass.z);
MyClass mc = new MyClass(25);
Console.WriteLine("{0},{1},{2}",MyClass.x,MyClass.y,MyClass.z);
}
}
The C# provides a special type of constructor known as static constructor to initialize the static data members when the class is loaded at first. Remember that, just like any other static member functions, static constructors can't access non-static data members directly.
The name of a static constructor must be the name of the class and even they don't have any return type. The keyword static is used to differentiate the static constructor from the normal constructors. The static constructor can't take any arguments. That means there is only one form of static constructor, without any arguments. In other way it is not possible to overload a static constructor.
We can't use any access modifiers along with a static constructor.
// C# static constructor
// Author: rajeshvs@msn.com
using System;
class MyClass
{
public static int x;
public static int y;
static MyClass ()
{
x = 100;
Y = 200;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1},{2}",MyClass.x,MyClass.y);
}
}
Note that static constructor is called when the class is loaded at the first time. However we can't predict the exact time and order of static constructor execution. They are called before an instance of the class is created, before a static member is called and before the static constructor of the derived class is called.
Static Member Functions
Inside a C# class, member functions can also be declared as static. But a static member function can access only other static members. They can access non-static members only through an instance of the class.
We can invoke a static member only through the name of the class. In C#, static members can't invoked through an object of the class as like in C++ or JAVA.
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyClass
{
private static int x = 20;
private static int y = 40;
public static void Method()
{
Console.WriteLine("{0},{1}",x,y);
}
}
class MyClient
{
public static void Main()
{
MyClass.Method();
}
}
Static Properties
The properties also in C# can be declared as static. The static properties are accessing using the class name. A concrete example is shown below.
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyClass
{
public static int X
{
get
{
Console.Write("GET");
return 10;
}
set
{
Console.Write("SET");
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 20; // calls setter displays SET
int val = MyClass.X;// calls getter displays GET
}
}
Static Indexers
In C# there is no concept of static indexers, even though static properties are there.
Static Members & Inheritance
A derived class can inherit a static member. The example is shown below.
// C#:static
// Author: rajeshvs@msn.com
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("Base static method");
}
}
class MyClass : MyBase
{
}
class MyClient
{
public static void Main()
{
MyClass.Method(); // Displays 'Base static method'
Console.WriteLine(MyClass.x);// Displays 25
}
}
But a static member in C# can't be marked as override, virtual or abstract. However it is possible to hide a base class static method in a derived class by using the keyword new.
An example is shown below.
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("Base static method");
}
}
class MyClass : MyBase
{
public new static int x = 50;
public new static void Method()
{
Console.WriteLine("Derived static method");
}
}
class MyClient
{
public static void Main()
{
MyClass.Method(); // Displays 'Derived static method'
Console.WriteLine(MyClass.x);// Displays 50
}
}
Finally remember that it is not possible to use this to reference static methods.
In C#, data fields, member functions, properties and events can be declared either as static or non-static. Remember that indexers in C# can't declared as static.
Static Fields
Static fields can be declared as follows by using the keyword static.
class MyClass
{
public static int x;
public static int y = 20;
}
When we declare a static field inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded first time.
For example
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyClass
{
public static int x = 20;
public static int y;
public static int z = 25;
public MyClass(int i)
{
x = i;
y = i;
z = i;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1},{2}",MyClass.x,MyClass.y,MyClass.z);
MyClass mc = new MyClass(25);
Console.WriteLine("{0},{1},{2}",MyClass.x,MyClass.y,MyClass.z);
}
}
The C# provides a special type of constructor known as static constructor to initialize the static data members when the class is loaded at first. Remember that, just like any other static member functions, static constructors can't access non-static data members directly.
The name of a static constructor must be the name of the class and even they don't have any return type. The keyword static is used to differentiate the static constructor from the normal constructors. The static constructor can't take any arguments. That means there is only one form of static constructor, without any arguments. In other way it is not possible to overload a static constructor.
We can't use any access modifiers along with a static constructor.
// C# static constructor
// Author: rajeshvs@msn.com
using System;
class MyClass
{
public static int x;
public static int y;
static MyClass ()
{
x = 100;
Y = 200;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1},{2}",MyClass.x,MyClass.y);
}
}
Note that static constructor is called when the class is loaded at the first time. However we can't predict the exact time and order of static constructor execution. They are called before an instance of the class is created, before a static member is called and before the static constructor of the derived class is called.
Static Member Functions
Inside a C# class, member functions can also be declared as static. But a static member function can access only other static members. They can access non-static members only through an instance of the class.
We can invoke a static member only through the name of the class. In C#, static members can't invoked through an object of the class as like in C++ or JAVA.
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyClass
{
private static int x = 20;
private static int y = 40;
public static void Method()
{
Console.WriteLine("{0},{1}",x,y);
}
}
class MyClient
{
public static void Main()
{
MyClass.Method();
}
}
Static Properties
The properties also in C# can be declared as static. The static properties are accessing using the class name. A concrete example is shown below.
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyClass
{
public static int X
{
get
{
Console.Write("GET");
return 10;
}
set
{
Console.Write("SET");
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 20; // calls setter displays SET
int val = MyClass.X;// calls getter displays GET
}
}
Static Indexers
In C# there is no concept of static indexers, even though static properties are there.
Static Members & Inheritance
A derived class can inherit a static member. The example is shown below.
// C#:static
// Author: rajeshvs@msn.com
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("Base static method");
}
}
class MyClass : MyBase
{
}
class MyClient
{
public static void Main()
{
MyClass.Method(); // Displays 'Base static method'
Console.WriteLine(MyClass.x);// Displays 25
}
}
But a static member in C# can't be marked as override, virtual or abstract. However it is possible to hide a base class static method in a derived class by using the keyword new.
An example is shown below.
// C#:static & non-static
// Author: rajeshvs@msn.com
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("Base static method");
}
}
class MyClass : MyBase
{
public new static int x = 50;
public new static void Method()
{
Console.WriteLine("Derived static method");
}
}
class MyClient
{
public static void Main()
{
MyClass.Method(); // Displays 'Derived static method'
Console.WriteLine(MyClass.x);// Displays 50
}
}
Finally remember that it is not possible to use this to reference static methods.
Subscribe to:
Posts (Atom)