SunQuest
 
       C#
  Home arrow C# arrow C# Classes Explained
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Classes Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 70
    2005-04-05

    Table of Contents:
  • C# Classes Explained
  • Access Modifiers
  • The this keyword
  • Fields
  • Constants
  • Static members

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    C# Classes Explained


    (Page 1 of 6 )

    Continuing our series about C# programming, in this article we discuss classes, which lie at the heart of any modern Object-Oriented Programming language. Among other things, you will learn some of the ways in which C# is different from C and C++ in this vital area.

    In this article we will discuss C# classes and how to create a class in C#. We will also discuss Class Members like fields, constants and static members; I will also discuss the Access Modifiers available in C#.

    A class is the core of any modern Object Oriented Programming language such as C#; most of the time, you will be writing classes. If you have a background in C or C++, many class aspects are similar, but some are different. For example, C# doesn't permit functions to exist outside the class declaration like C++; also, C# use the .NET Framework Class Library, so you have access to thousands more functions than the class libraries available to C++.

    But the concept of data encapsulation is the same in all the modern OOP languages, so a class in C# contains data (fields for example) and operations that manipulate the data (methods). For example, a class that describes an employee would include fields like firstName, lastName, dateOfBirth, basicSalary and department, and methods that operate on the data, such as CalculateSalary() which calculates the employee's basic salary plus the overtime, for example, and another method like RaiseBasicSalary() that raises the basic salary of the employee.

    To create a class, you simply use the keyword "class" followed by the class name:

    class Employee
    {

    }

    You can precede the class keyword with an access modifier keyword (which we will discuss later in the article):

    public class Employee
    {

    }

    All of the class members must exist between the curly braces, so you don't have global functions like C++. You can create more than one class definition in one physical file, but it's not a good programming practice; it's better to create each class in one file with the extension .cs

    Note that all classes in C# directly or indirectly inherit from one base class, System.Object, and you don't have to explicitly define the inheritance, because the C# compiler enforces it. So the following class:

    public class Employee
    {

    }

    is the same as:

    public class Employee : System.Object
    {

    }

    You can add a class to your project using Visual Studio.NET; simply right click the project icon in Solution Explorer window (you can view the Solution Explorer windows using CTRL+ALT+L), then click on Add --> Add Class, and the Add New Item windows will be shown:

    Note that you can add a class, Windows Form, Control, Component and many other variations; this is one feature of the Visual Studio.NET tool. For example, when you add Windows Form, VS.NET will create the Form (it's a class too) and generate code that the Form requires.

    Modify the name of the class to Employee.cs and click Open. VS.NET will create the file, and generate basic class declaration code:

    using System;
    namespace Company
    {
    /// <summary>
    /// Summary description for Employee.
    /// </summary>
    public class Employee
    {
    public Employee()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    }
    }

    VS.NET declares the class as public, and creates an empty default constructor along with some comments. These are special kinds of comments that are used for generating documentation -- another feature of VS.NET. For now, delete the documentation and the default constructor, so you will show the Employee class as:

    using System;
    namespace Company
    {
    public class Employee
    {
    }
    }

    Before we discuss class members you should know that every member must have an access modifier, and if you didn't supply one, C# will consider this member a private member. It's better, however, that you explicitly define it private with the keyword "private." As you can see, there's no semicolon needed after the class declaration, although you can put it there if you're used to doing so. There are no more #include files, because the C# compiler automatically resolves project dependencies. In C# the operators -> and :: (unlike C++) don't exist, so if you want to access class members, use the dot operator ( . )

    C# classes are reference types, so they are never allocated on the Stack; they are allocated on the Managed Heap. The Managed Heap is an area of memory that is managed by the Common Language Runtime, which has the ability to free unused memory blocks (objects) in a process known as Garbage Collection. When you have a variable that's not attached to an object:

    Employee Michael;

    You will not be able to access the object's members, and the variable Michael will reference to a block of memory which contains nothing, so we say of this variable that "it has a null reference" which means we can't use it. You must instantiate an object in order to use it:

    Employee Michael = new Employee();

    As we have said before, the new operator creates the object on the Managed Heap and returns the reference, which will be stored in the variable Michael.

    More C# Articles
    More By Michael Youssef


       · i m very new to c# pls tell me how to get a MSIL window i m using microsoft visual...
     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway