Home arrow C# arrow Page 5 - Introduction to Objects and Classes in C#
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 / 491
September 16, 2003
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
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#

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 
Support 

Developer Shed Affiliates

 




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