Home arrow C# arrow Page 5 - 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 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,

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.

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