Search This Blog

Friday, 20 July 2012

Introduction to C# classes


A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable.

Example 1: Method 1
    class Program
    {
        public static void Main()
        {
            Car objcar;
            objcar = new Car("Green"); //objCar is instance of a class which is related to Object
            Console.WriteLine(objcar.describe());
            objcar = new Car("Red");
            Console.WriteLine(objcar.describe());
            Console.ReadLine();
        }
    }
    class Car
    {
        private string color;
        public Car(string color)
        {
            this.color = color;
        }
        public string describe()
        {
            return "The colour of the Car is: " + color;
        }
    }

Example 2: Method 2
class Program
    {
        public static void Main()
        {
            Car objcar = new Car();
            objcar.color = "Red";
            Console.WriteLine(objcar.describe());
            objcar.color = "Green";
            Console.WriteLine(objcar.describe());
            Console.ReadLine();
        }
    }
    class Car
    {
        private string colour;
       
        public string describe()
        {
            return "The colour of the Car is: " + color;
        }
        public string color
        {
            get { return colour; }
            set { colour = value; }
        }
    }
Example 3: Method 3

Example 1

This sample shows a Person class that has two properties: Name (string) and Age (int). Both properties are read/write.
// person.cs
using System;
class Person
{
    private string myName ="N/A";
    private int myAge = 0;
 
    // Declare a Name property of type string:
    public string Name
    {
        get 
        {
           return myName; 
        }
        set 
        {
           myName = value; 
        }
    }
 
    // Declare an Age property of type int:
    public int Age
    {
        get 
        { 
           return myAge; 
        }
        set 
        { 
           myAge = value; 
        }
    }
 
    public override string ToString()
    {
        return "Name = " + Name + ", Age = " + Age;
    }
 
    public static void Main()
    {
        Console.WriteLine("Simple Properties");
 
        // Create a new Person object:
        Person person = new Person();
 
        // Print out the name and the age associated with the person:
        Console.WriteLine("Person details - {0}", person);
 
        // Set some values on the person object:
        person.Name = "Joe";
        person.Age = 99;
        Console.WriteLine("Person details - {0}", person);
 
        // Increment the Age property:
        person.Age += 1;
        Console.WriteLine("Person details - {0}", person);
    }
}

Output

Simple Properties
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100

No comments:

Post a Comment