C#
  Home arrow C# arrow Page 4 - C# Classes Explained
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 
Mobile Linux 
App Generation ROI 
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 / 74
    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
     
     
    ADVERTISEMENT


    C# Classes Explained - Fields


    (Page 4 of 6 )

    A field is a class level variable, or you can call it a member variable. If you declare a field of a value type, it stores the data itself, but if you declare a field of a reference type, it stores a reference to where the actual data is allocated. You must be careful when you define member variables. Let's take an example:

    using System;
    namespace MyCompany
    {
    public class Employee
    {
    public decimal Salary;
    }

    public class EmployeeTest
    {
    static void Main(string[] args)
    {
    // this is client code that can modify the value of the Employee's salary
    Employee Michael = new Employee();
    Michael.Salary = 10000;
    Console.WriteLine("Michael's salary = {0:C}",Michael.Salary);
    Michael.Salary = 0;
    Console.WriteLine("Michael's salary = {0:C}",Michael.Salary);
    Console.ReadLine();
    }
    }

    The class Employee has only one public field (Salary) which represents the employee's salary. The EmployeeTest class' Main method creates the instance Michael of type Employee, and the next statement sets the salary to $10,000. As you can see, the client code assigns the value to the public field Salary, and you don't have any kind of security over the process. Again, the Main method assigns 0 to the salary and writes it to the Console Window. You will get the following result:

    Good Object oriented practice states that you must declare your fields as private, and provide provide public properties to access the fields (we will discuss properties later in the series, too, so don't worry). Let's look at the MSIL code for Employee class. Open the ILDASM tool and load the application, then navigate to the Employee class and double click on the Salary field:

    The MSIL is just telling us that it's a public field of type System.Decimal, and it's called Salary. This means that when you instantiate the Employee class, the created object will have a block of memory for the Salary field; that is worth remembering.

    With other programming languages we had a very common problem, which C# has vanquished. Suppose that you need a field to be initialized at runtime and never change after that. If you know the value at design time, you can use constants (as we will discuss shortly). You can declare your field with the keyword "readonly" to have this behavior, and once it has been initialized at runtime it will never change. You have only one place to initialize the "readonly" field, which is the class constructor. Turning back to our Employee class, we need a "readonly" field to hold the SSN that will dynamically return from a method; this method maybe will connect to a database server to get the SSN of the employee, so the SSN is not available at design time.

    using System;
    namespace MyCompany
    {
    public class Employee
    {
    public decimal Salary;
    public readonly double SSN;

    // class constructor
    public Employee()
    {
    SSN = this.GetSSN();
    }

    private double GetSSN()
    {
    //we will assume that we have connected to a database
    return 987654321;
    }
    }

    public class EmployeeTest
    {
    static void Main(string[] args)
    {
    // this is client code that can modify the value of the Employee's salary
    Employee Michael = new Employee();
    Console.WriteLine(Michael.SSN);
    Console.ReadLine();
    }
    }
    }

    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

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT