By 22/07/2015 5 Comments

C# Classes

A class just is a template, the blue print, the definition, the logical abstraction, an idea that only defines the form of an object. It is also a set of plans which specifies how an object will be created. Example of classes (Mark sheet, User, Bank account, Button, Date, Media player etc.). It is like a wrapper which defines the attributes and behavior inside it. A class is a user defined type and it is a reference type.

Attributes and behavior:

An attribute is a characteristic of each object in real world examples. Suppose we have a Mark sheet class, a mark sheet class defines each subject names, subject marks, student name, student address etc. as its attributes and percentage, total marks, obtain marks, grade etc. as its behavior. In C# the attributes are called properties and behavior is called method. In classes Attributes are data, a fields or a variables of different types and behaviors are methods or functions, these methods operates or modifies the properties. Some classes defines only properties in this case these properties are called data members of a class. Some classes defines only methods which is called method member of a class. In real world object oriented programming a class contains both methods and properties which are collectively called class members. A class also contains other members such as delegates, auto properties etc. as its members.

Syntax:

class ClassName
{
    Members (any class member)
}

STATEMENT DESCRIPTION
class ClassName A class declaration with class keyword and its name, where name can be any user defined valid name.
{ A class definition starting scope.
Members (any class member) This statement is called class members. The class members can be properties, methods, events and variables etc.
} Ending scope of a class definition.

Example of a Mark sheet class:

using System;
namespace csharpBasic
{
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            Console.ReadKey();
        } 
    }

    // Declare a Mark Sheet class.
    class MarkSheet
    {				
        /* Following is the initialization of string and double type class attributes/properties, 
           here these attributes are called fields. 
         */
        public string StudentName = "abc";
        public string StudentAddress = "xyz";
        public string Asp = "Asp.net";
        public string Javascript = "JavaScript";

        public double AspMarks = 80;
        public double JavascriptMarks = 60;

    }    // End class definition scope.
}


REMEMBER:

  • At this point, above you have created a class, which describes only definition of a Mark sheet not a Mark sheet itself. Remember this class does not have physical existence and cannot do anything, this is just a blue print, template or a logical abstraction until its object will be created.
  • It is just an idea about how an actual Mark sheet will be created. At this point this Mark sheet has no any specific information until its object will be created.
  • Many objects can be created from this Mark sheet template.





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .