C#
  Home arrow C# arrow Page 5 - Introduction to Objects and Classes in C#
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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#

Introduction to Objects and Classes in C#
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 351
    2003-09-16

    Table of Contents:
  • Introduction to Objects and Classes in C#
  • Introduction
  • World's Classes and Objects
  • Programmer’s Classes and Objects
  • Properties and Variables
  • Properties
  • Reworked
  • Creating Objects and Classes
  • Conclusion

  • 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


    Introduction to Objects and Classes in C# - Properties and Variables


    (Page 5 of 9 )

    Variables declared in a class store the data for each instance.  What does this mean? It means that when you instantiate this class (that is, when you create an object of this class) the object will allocate memory locations to store the data of its variables. Let's take an example to understand it well.

    class Person
    {
        public int Age;
        public string HairColor;
    }

    This is our simple class which contains 2 variables. Don't worry about public keyword now because we will talk about it later. Now we will instantiate this class (that is, when you create an object of this class).

    static void Main(string[] args)
    {
        Person Michael = new Person();
        Person Mary = new Person();

        // Specify some values for the instance variables
        Michael.Age = 20;
        Michael.HairColor = "Brown";
        Mary.Age = 25;
        Mary.HairColor = "Black";
        // print the console's screen some of the variable's values
        Console.WriteLine("Michael's age = {0}, and Mary's age = {1}",Michael.Age,
            Mary.Age);
        Console.ReadLine();
    }

    So we begin our Main method by creating 2 objects of type Person. After creating the 2 objects we initialize the instance variables for object Michael and then for object Mary. Finally we print some values to the console.  Here, when you create the Michael object, the C# compiler allocates a memory location for the 2 instance variables to put the values there. Also, the same thing with the Mary object; the compiler will create 2 variables in memory for Mary object. So each object now contains different data. Note that we directly accessed the variables and we put any values we wanted, right?  But wait there is a solution to this problem. We will use properties.

    More C# Articles
    More By Michael Youssef


       · sirthis artical was very good...even a lay man can understand this. iam from a...
       · Great for beginners !! It helped me a lot. Thanks!
       · very good ......but blank output...
       · very god article for beginners.. i benefitted alot, highly recommended to...
       · This article is very good for beginners like me.In page 8 of this article you...
       · Thanks for your post, could you please specify what is it that you need to...
       · Thank you for your reply.At the end of the page you talked about constructors and...
       · this article is simply superb,i learned a lot of basics from this article,and ...
       · Before reading this article,I knew alot about objects and classes but the way this...
       · Excellent!
       · this is what kind of tutorial I can understand, hEHe..:)
       · Have come back to OOP via C# after break of 14yrs. This tutorial has enabled me to...
       · it is a good stuf :)
       · Michael Youssef,I loved the articles. I've been taking a rapid course in C# and...
       · it is really a gr8 article.
       · i saw the same article written by some other author in some other website...so who...
       · I think this is the best article on learning, i tried so many ways by browsing, but...
       · I think this is the best article for beginners who don’t know about the real life...
       · This article is simple to understand, must-read material for a OOP...
       · Sir i m very happy to see ur artical and from that artical i ll get the proper...
       · Awsome simplification dude keep it upJa$$i...
       · thank you soo much..this is really great!!
       · Dev thank you God bless you !!!!!1111looking forward to seeing your book
     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek