C# - Classes
You saw a class structure in the “Hello, C# World!” sample. In the C#, you define a class by using the class keyword, just as you do in C++. Following the class keyword the class name and curly brackets ({. . .}), as shown here:
class Hello
{
static void Main()
{
Console.WriteLine("Hello, C# World!");
}
}
Note: C# classes don’t end semicolon (;) as C++.
Once a class is defined, you can add class members to it. Class members can include constants, fields, methods, properties, indexers, events, operators, instance constructors, static constructors, destructors, and nested type declarations. Each class member has an associated accessibility, which controls the scope of the member and defines whether these members are accessible outside the class.
Class Members
CLASS MEMBER INHERITANCE
Methods- Similar to C++ functions. Methods implement some action that can be performed by an object.
Properties- Provide access to a class attribute (a field). Useful for exposing fields in components.
Events- Used to provide notification.
Constants- Represents a constant value.
Fields- Represents a variable of the class
Operators- Used to define an expression (+, *,->, ++,[], and so on ).
Instance
Constructors- Methods called during initialization of an object.
Static Constructors- Called automatically.
Destructors- Called when an object is being destroyed.
Indexers- A new concept in C#. An indexer provider indexing on an object. It allows you to treat a class as an array.
Types- All local types used in a class.
Class member accessibility types and scopes
ACCESSIBLITY TYPE SCOPE
Public Member is accessible from other programs.
Protected Member is accessible by the containing and its derived classes and types.
Internal Member is accessible only the current program.
Protected internal Member is accessible by the current program and the class derived from the containing class.
Fields
A field member Represent a variable of a class. In this example, strClassName is a string type public variable that can be accessed by the class instance:
class myClass
{
public static string strClassName;
public void SetClassName(string strName)
{
strClassName = strName;
}
}
As noted earlier; you can define field members as read-only. This means the field can only be assigned in the declaration or in the constructor of the class.
class myClass
{
public static readonly string strClassName = "myClass";
public void SetClassName(string strName)
{
strClassName = strName; // illegal assignment
}
}
Note that the complier will throw an error because of an illegal assignment. If the field is not static, you have to access fields from the class instance. It’s the same idea as accessing a public variable in the C++ or structure in C. for example:
myClass cls = new MyClass();
string clsName = cls.strClassName;
Constants
A constant Member represents a constant value throughout the program. For example the clsNodes is constant that has integer value 12. See the following code:
class myClass
{
public const int clsNodes = 12;
}
The value of clsNodes will be 12 throughout the program and can’t be reassigned.
Instance and Static Constructors
Constructors in C# are defined in the same way as in C++. C# supports two types of constructors: instance constructors and static constructors. Instance constructors are called every time a class is initialized. Static constructors are executed only once. Static constructors are for initialing the values of static variable. Listing 25 is an example of a class with a static constructor.
Constructors can be overloaded, as shown below:
class myClass
{
public int iCounter, iTotal;
public myClass()
{
iCounter = 0;
iTotal = 0;
}
public myClass (int iCount, int iTot)
{
iCounter = iCount;
iTotal = iTot;
}
}
Destructors
A destructor is called when it’s time to destroy the object. Destructors can’t take parameters. See following code:
class myClass
{
~myClass()
{
// free resources
}
}
TIP: It’s not mandatory; in fact it’s unadvisable to call destructors. They’re called automatically by the CLR.
Methods
A method is a member that implements some functionality. It’s similar in appearance to the methods found in C++ and java. A method can return a value have, a list of parameters, and can be accessed through the class, whereas non - static. Static methods are accessed through the class, whereas non-static methods are accessed through the instance of the class. For example, listing 28 adds a method sum to the class myClass
and called this method from the Main method.
Class method example
using System;
class myClass
{
public int Sum(int a, int b)
{
int res = a + b;
return res;
}
}
class TestmyClass
{
static void Main()
{
myClass cls = new myClass();
int total = cls.Sum(5, 8);
Console.WriteLine(total.ToString());
}
}
Methods in C# support function overloading in a similar way as C++. If you have programmed in C++, you’ll notice that C# methods are similar to C++ functions (and almost mirror those methods found in java). So it’s not a bad idea to call function overloading in C# method overloading. In listing 29, I over- overload the Sum method by passing in different types of values and call each of the overloaded Sum methods from the Main method.
Properties
Other than methods, another important set of members of a class is variables. A variable is a type that stores some value. The property member of a class provides access to variables. Some examples of Properties are font type, color, and visible properties. Basically, Properties are fields. A field member can be accessed directly, but a property member is always accessed through accessor and modifier methods called get and set, respectively.
Class property member example
using System;
class myClass
{
private bool bGender;
private int intAge;
// Gender property.
public bool MaleGender
{
get
{
return bGender;
}
set
{
bGender = value;
}
}
// Age property
public int Age
{
get
{
return intAge;
}
set
{
intAge = value;
}
}
}
class TestmyClass
{
static void Main()
{
myClass cls = new myClass();
// set properties values
cls.MaleGender = true;
cls.Age = 25;
if (cls.MaleGender)
{
Console.WriteLine("The Gender is Male");
Console.WriteLine("Age is" + cls. Age.ToString() );
}
}
}
Why use properties if you already have the field available? First of all, properties expose fields in classes being used in components. They also provide a means for doing necessary computation before or after accessing or modifying the private fields they’re representing. For example, if you’re changing the color of a control in the set method, you may also want to execute an invalidate method inside the set to repaint the screen