A C# class is defined in the following format:
[ attributes ] [ modifiers ] class identifier [: base-list ] { class-body }[;]
where attributes may be one of the following:
public The class is available to all other pieces of code.
Internal The class is available only to other pieces of code in the same assembly.
Note | Protected and Private are not permissible at the class definition level, except for nested classes. |
Modifiers may be
abstract The class is an abstract class, and can only be used as a base class for new classes.
sealed The class is a sealed class, and cannot be used as the base class of a new class.
The new keyword is not permissible at the class definition level, but is used to make members of a base class invisible to a derived class ( new appears in the derived class).
The base-list is a list of base classes. Unlike C++ where a derived class defines the accessibility of its base class, the base class defines accessibility. For example, if a base class declared itself as internal, then a derived class would inherit the base classes members as internal.
A class can contain declarations of the following member types: constructors, destructors, constants, fields, methods , properties, indexers, and operators. Fields are common data members.
Properties are methods that define accessor functions for some value or property of the class. There is no property keyword; instead the declaration of the property appears in formatting the code:
public class Picture
{
private int m_RotateAngle;
public int RotateAngle
{
get{ return(m_RotateAngle); }
set{ if(value>=0 && value<=359) m_RotateAngle = value; }
}
Indexers are methods that provide an index operator on the class. The special format of declaration is similar to a property, only it uses the this keyword in the declaration as the property name . For example:
public class Picture
{
private int[] m_TagID;
public int this [int Index] // Index can be of any data type.
{
set { if(Index >=0 && Index <= m_TagID.Length)
m_TagID[Index]=value; }
get { if(Index >=0 && Index <= m_TagID.Length)
return(m_TagID[Index]); else return(-1);
}
}
Operator methods are operator overloads similar to those in C++. The main difference is that the methods must be declared public static , and all parameters are passed to them (unlike the implicit left-hand operator this in C++). An example of an operator would be the following:
Class Date {
public static int operator+(Date D, int Days) {};
Like Java, C# implements automatic garbage collection. Though a destructor can be declared in C#, it is important to note that they are called when the Garbage Collector determines the object is no longer reachable by code, and when memory space is needed.
A destructor has the same format in C# as in C++: ~ClassName(); . The garbage collector will invoke this method automatically, when it sees fit.
In the event you are handling nonmanaged (non-CLR) resources, you may want to force the garbage collection for the object. In order to do this, you must declare your class to implement the IDispose interface, and also provide a Dispose method. A typical example is
using System;
class Testing : IDisposable
{
bool is_disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!is_disposed) // only dispose once!
{
if (disposing)
{
// Not in destructor, OK to reference other objects
}
// perform cleanup for this object
}
this.is_disposed = true;
}
public void Dispose()
{
Dispose(true);
// tell the GC not to finalize
GC.SuppressFinalize(this);
}
~Testing()
{
Dispose(false);
}
}