C#
  Home arrow C# arrow Page 7 - 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  
Moblin 
JMSL Numerical Library 
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#

Introduction to Objects and Classes in C#
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 306
    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# - Reworked


    (Page 7 of 9 )

    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();
    }

    Here I created the same objects from the last example, except that I used only properties to access the variable instead of accessing it directly. Look at the following line of code

    Michael.Age = 20;

    When you assign a value to the property like that C# will use the set accessor. The great thing with the set accessor is that we can control the assigned value and test it; and maybe change to in some cases. When you assign a value to a property C# changes the value in a variable and you can access the variable's value using the reserved keyword value exactly as I did in the example. Let's see it again here.

    set
       {
           if(value <= 65 && value >= 18)
           {
               age = value;
           }
           else
               age = 18;
       }

    Here in the code I used if statement to test the assigned value because for some reason I want any object of type Person to be aged between 18 and 65. Here I test the value and if it is in the range then I will simply store it in the variable age. If it's not in the range I will put 18 as a value to age.

    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 ...
     

    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-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT