0

I am new to C#, and writing a piece of code to do some exercises. What surprises me is that I can use undefined member variables in a C# class as if they had been defined. Below is my code. In class Person, I only defined "myName" and "myAge," but I can use the member variables "Name" and "Age" without any issue. The code can be compiled and the executable can be run. Can someone tell me why I can use "Name" and "Age" without defining them? Many thanks,

C# code

====================================== using System;

namespace prj01
{
class Person
{
    private string myName = "N/A";
    private int myAge = 0;

    public string Name
    {
        get
        {
            return myName;
        }
        set
        {
            myName = value;
        }
    }

    public int Age
    {
        get
        {
            return myAge;
        }
        set
        {
            myAge = value;
        }
    }

    public override string ToString()
    {
        return "Name = " + Name + ", Age = " + Age;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // property
        Console.WriteLine("Simple Properties");

        Person person01 = new Person();
        Console.WriteLine("Person details - {0}", person01);
        person01.Name = "Joe";  // Why can I use "Name"?
        person01.Age = 99;      // Why is "Age" accessible and usable?
        Console.WriteLine("Person details - {0}", person01);

        Console.ReadLine();
    }
}
}

======================================

cy77
  • 55
  • 5

3 Answers3

2

You did define them. Right here:

public string Name
{
    get
    {
        return myName;
    }
    set
    {
        myName = value;
    }
}

public int Age
{
    get
    {
        return myAge;
    }
    set
    {
        myAge = value;
    }
}

These are called "properties" in .NET classes. In your current code, they're essentially "pass-through" properties which do nothing but delegate access to the member variables. They compile into getter and setter methods wrapping those member variables.

David
  • 208,112
  • 36
  • 198
  • 279
  • Sorry, my mistakes. I really appreciated you for your detailed explanation on the code. – cy77 Sep 17 '13 at 23:46
0

Name and Age are public properties, these are used by code external to the class to send in data, that is, to modify the private variables. If you change the public beside Name to private, you won't be able to use that property because of the protection level: What is the difference between Public, Private, Protected, and Nothing?

Community
  • 1
  • 1
Terry Kernan
  • 746
  • 5
  • 12
0

.Net classes expose 2 types of data members

1>Fields :

in your example they are myName and myAge, since they are private you can only use them within your class members.

2> Properties In you class they are Name and Age. Since they are public they can be accessed within and outside your class. With properties you can getters and/or setters. getters enable you to read Value from a property eg :

Person person01 = new Person();

int xyz = person01.Age; // it is internally calling person01.Age.get();

and setters enable setting value for the property ie

Person person01 = new Person();

person01.Age = 2;// this is internally calling person01.Age.set(2);

Hope this clarifies.

Manish Kumar
  • 113
  • 3