C#
  Home arrow C# arrow Page 6 - 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# - Properties


    (Page 6 of 9 )

    Properties are a way to access the variables of the class in a secure manner. Let's see the same example using properties.

    class Person
    {
        private int age;
        private string hairColor;
        public int Age
        {
            get
            {
            return age;
            }
            set
            {
                if(value <= 65 && value >= 18)
                {
                    age = value;
                }
                else
                    age = 18;
            }
        }
        public string HairColor
        {
            get
            {
                return hairColor;
            }
            set
            {
                hairColor = value;
            }
        }
    }

    I made some modifications, but focus on the new 2 properties that I created. So the property consists of 2 accessors. The get accessor, responsible of retrieving the variable value, and the set accessor, responsible of modifying the variable's value. So the get accessor code is very simple. We just use the keyword return with the variable name to return its value. So the following code:

     get
       {
        return hairColor;
       }

    returns the value stored in hairColor.

    [Note]

    The keyword value is a reserved keyword by C# (that is, reserved keywords means that these keywords are owned only by C# and you can't create it for any other purposes. For example, you can't create a variable called value .If you did, the C# compiler would generate an error. To make things easier, Visual Studio.NET will color the reserved keywords in blue.)

    [/Note]

    Let's put this code to work and then discuss the set accessor..

    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 6 hosted by Hostway
    Stay green...Green IT