Home arrow C# arrow Page 6 - Introduction to Objects and Classes in C#

Introduction to Objects and Classes in C#

In this article Michael introduces us to C#, as well as attempts to demystify the theory behind "Object and Classes" in OO Programming.

Author Info:
By: Michael Youssef
Rating: 5 stars5 stars5 stars5 stars5 stars / 496
September 16, 2003
  1. · Introduction to Objects and Classes in C#
  2. · Introduction
  3. · World's Classes and Objects
  4. · Programmerís Classes and Objects
  5. · Properties and Variables
  6. · Properties
  7. · Reworked
  8. · Creating Objects and Classes
  9. · Conclusion

print this article

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
        return age;
            if(value <= 65 && value >= 18)
                age = value;
                age = 18;
    public string HairColor
            return hairColor;
            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:

    return hairColor;

returns the value stored in hairColor.


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


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

blog comments powered by Disqus

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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials