Text Size

Classes With C#

PDF 

Tutorials

(4 votes, average: 4.00 out of 5)

INTRODUCTION

Classes are a major element of every object-oriented language. If you are a promising new programmer, chances are you will create and use classes in each and every one of your programs. Classes provide templates that describe various objects, from a customer of a hotel to a new car model. In this tutorial we will talk about some basic concepts regarding their declaration methods and usage.

DEFINING CLASSES

After creating a project, one should define the classes that represent the objects used in the project. Each class contains data and functions which are known as the class’s members, data members and function members. They can be declared either as public or private. Public members are visible and accessible from outside the class while private are only visible to code within the particular class. Another widely used option is protected, which makes the member visible to the particular class and any other derived classes.

Finally, classes are reference types and stored on the managed heap. This means that when you pass a class as a parameter in a function the actual object is being passed.

DATA MEMBERS

Data members contain the data for each class and can be either static or instance. Static data members are associated with the class as a whole while instance members (the default option)) are associated with every instance of the class. They are further distinguished as fields or constants. Fields are variables associated with the class while constants are constant variables and their value cannot be changed. The following snippet of code declares a class and changes one of its fields.

class Customer

{

public const string Company = "LG";

public int age = 22;

public string FirstName;

public string LastName;

}

static void Main(string[] args)

{

Customer Elizabeth = new Customer();

Elizabeth.age = 27;

Elizabeth.FirstName = "Elizabeth";

Console.WriteLine(Elizabeth.FirstName + " is " + Elizabeth.age + " years old");

Console.ReadLine();

 

}

Function members

These members include methods, properties, constructors, operators and finalizers. Methods are simple functions providing functionality to the class. As with data members, they can be either static or instance. Properties are functions with specific purpose and usage for implementing properties in your classes. Constructors and finalizers are called when an object is instantiated or destroyed respectively. Finally, operator overloading is possible for your custom-made classes. In C# you must define the method together with its declaration. It is not possible to declare a method and define it at another point of your code. The following snippet of code defines a class with static and instance member types and illustrates the invocation of them.

class GreekHotels

{

public string Town;

public static string GetCountry()

{

return "Greece";

}

}

static void Main()

{

Console.WriteLine("Greek hotels are in " + GreekHotels.GetCountry());

GreekHotels ABC = new GreekHotels();

ABC.Town = "Thessaloniki";

Console.WriteLine("ABC hotel is in " + GreekHotels.GetCountry() + ", in the town of " + ABC.Town);

Console.ReadLine();

}

The class contains a public static function which returns the country of the hotels. Since all Greek hotels are in Greece, no need to define instance types for this information. It also contains an instance type field called town. Notice the difference in their invocation in the last line of code.

Trackback(0)
Comments (3)add comment

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1