C Sharp/Classes and Objects I

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Home

Basics
Home
introduction
Overview
Statements
Data Types
Variables
Operators
Flow Control
Variables II
Functions
C# - Classes and Objects I

Dates and Times
Conversions

Advanced
Classes II
Inheritance
GUI
Namespaces
Exceptions
Threads
ADO.NET
Generics

[edit] Introduction C# Classes and Objects

C# objects as the whole and classes as descriptions of the object. For Example, lets take a computer and make an object. Well the computer is definitly going to have a price, model, manufactuer, type of CD-ROM, case, etc.. All these describe what kind of computer it is. One good benefit of using object oriented programming is if a part of the computer class had to be replaced we can replace NOT the whole computer unlike in traditional programming.

[edit] Declaring a C# Class

using System;

class Program
{
    public class MyClass
    {
        //this is a class
    }
    static void Main(string[] args)
    {
    }
}


declaring a class is simple.

<access-modifier> class <class name> { }

  • access-modifier: The level of access the class has in the program: public, protected and private
  • class name: The name of the class


public class MyComputer    //Declare class
    {
        public string VideoCard;   //Declare field
        public string HardDrive;   //Declare field
 
        public void Print()         //Declare method
        {
            VideoCard = "ati";
            HardDrive = "Seagate";
            Console.WriteLine(VideoCard);
            Console.WriteLine(HardDrive);
        }
    }

Classes often use the the public access identifier. This enables the class to be accessible in the Main() block.

  • Classes must be declared outside Main()
  • Variables that are declared directly in the class block are called fields
  • Function that are decalred in a class are called methods

[edit] Create C# Objects

In this section we will focus on creating C# objects. Remember in the MyComputer object that we explained above? We will create a real C# MyComputer object. We have been creating objects in every previous section but you just did'nt realize it. Now you will create your own C# objects.

Using the MyComputer class above we will create a simple MyComputer object.

MyComputer MyComputerClass = new MyComputer();

We can now access MyComputer members via MyComputerClass. We made the field variables public so they could be accessed via the object declared in Main() block. Under normal circumstances we would usually set the field access level to private because we want to hide the field members from the programmer and the compiler. If you had a team of 30 programmers and each one worked on parts of the application you only want them to see just the public methods.


Example 1: Here is a C# example:

using System;
class Program
{
    //Class
    public class MyComputer
    {
        //Field members
        public string VideoCard = "ati";   
        public string HardDrive = "seagate";
    }
    static void Main()
    {
        MyComputer MyComputerClass = new MyComputer();
        string MyVidCard = MyComputerClass.VideoCard;
        string MyHardDrive = MyComputerClass.HardDrive;
        Console.WriteLine(MyVidCard);
        Console.WriteLine(MyHardDrive);
        Console.Read();
    }
}

Output:

ati
Seagate

As you can see MyComputerClass is the object and it is declared using the name of the class. new MyComputer(); calls the constructor and creates the memory required to hold the class in the object MyComputerClass.

  • string MyVidCard = MyComputerClass.VideoCard;

Remember to access a field member use the ( . ) operator. The ( . ) operator can access methods as well.


Example 2: This program will illustrate that private field members cannot be accessed.

using System;
class Program
{
    public class MyComputer
    {
        //Field members
        private string VideoCard = "ati";   
        private string HardDrive = "seagate";
    }
    static void Main()
    {
 
        MyComputer MyComputerClass = new MyComputer();
        string MyVidCard = MyComputerClass.VideoCard;
        string MyHardDrive = MyComputerClass.HardDrive;
        Console.WriteLine(MyVidCard);
        Console.WriteLine(MyHardDrive);
        Console.Read();
    }
}
  • Notice: This code will not compile correctly.


MyComputerClass.VideoCard; is an error because VideoCard class member is a private field.