#242 – Declaring and Using a Property in a Class

You can create instance data in a class by defining public fields.  You can read and write fields using a reference to an instance of the class.

A class more often exposes its instance data using properties, rather than fields.  A property looks like a field from outside the class–it allows you to read and write a value using a reference to the object.  But internally in the class, a property just wraps a private field, which is not visible from outside the class.

    public class Dog
    {
        // Private field, stores the dog's name
        private string name;

        // Public property, provides a way to access
        // the dog's name from outside the class
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }

Using the property:

            Dog kirby = new Dog();

            kirby.Name = "Kirby";
Advertisements

#232 – Declaring and Using Instance Methods in a Class

In a class, an instance method is a kind of class member that is a block of statements that will execute when a user of the class calls the method.  It typically acts upon the data stored in that particular instance of the class.

A method has a return value that allows the method to return a result, or a return type of void, indicating that it doesn’t return any result.

Here’s a simple example of a method:

    public class Dog
    {
        public string Name;
        public int Age;

        public void BarkYourAge()
        {
            for (int i = 1; i <= Age; i++)
                Console.WriteLine("Woof");
        }
    }

Calling our method:

    Dog rover = new Dog();
    rover.Name = "Rover";
    rover.Age = 3;

    rover.BarkYourAge();

The result:

#231 – Declaring and Using Instance Fields in a Class

In a class, an instance field is a kind of class member that is a variable declared in the class and having a particular type.  There is one copy of the field for each instance of the class.  You can read and write the value of the field using a reference to the instance of the class.

Fields are declared in a class as follows:

    public class Dog
    {
        public string Name;
        public int Age;
    }

These fields can then be used as follows:

            Dog buster = new Dog();  // Create new instance of Dog
            buster.Name = "Buster";  // Write to Name field
            buster.Age = 3;          // Write to Age field

            Dog kirby = new Dog();   // Another instance of a Dog
            kirby.Name = "Kirby";
            kirby.Age = 13;

            // Reading properties
            int agesAdded = buster.Age + kirby.Age;

#230 – User-Defined Types Are First Class Citizens

When you create a user-defined reference type using the class keyword, that new type can be treated in the same way that you treat any built-in type in the .NET Framework.  You can create instances of the type and interact with it in the same way that you interact with built-in types.

            // Interacting with TimeSpan class
            TimeSpan ts = new TimeSpan(5, 3, 2);
            double ms = ts.TotalMilliseconds;
            TimeSpan dblIt = ts.Add(ts);

            // Interacting with my Dog class
            Dog buster = new Dog("Buster", 5);
            int age = buster.Age;
            buster.Bark();

#229 – The Core Principles of Object-Oriented Programming

As an object-oriented language, C# supports the three core principles of object-oriented programming:

  • Encapsulation – Hide implementation details in a class from users of the class, exposing only a public interface
  • Inheritance – Derive a subclass from a parent class, inheriting data and behavior from the parent, in an “is-a” relationship.  Inheritance defines a hierarchy of classes.  All classes ultimately inherit from System.Object.
  • Polymorphism – Any subtype may be used where a parent type (or type higher up in the class hierarchy) is expected.  Conversely, a variable of a particular class will be treated as the appropriate subclass.

#228 – Object-Oriented Programming in C# Using Classes

Classes are the construct in C# that enable the object-oriented paradigm and make C# an object-oriented language.

Classes are the user-defined types that define a set of related data and the methods that act upon that data.  Each instance of the class (an object) has a set of values for the class’ data, know as the object’s state.

Object-orientation is a powerful paradigm to use when writing software because using classes and objects helps you think about things, rather than algorithms.

For example, we might have a Dog class with data fields like: Name, Age, Breed, LikesBalls, and PersonalityType.  We might have methods in the Dog class like Bark, Sit, and Fetch.  Each instance of a Dog would have different values for these fields and we can call any of the Dog methods on an instance of a dog and it would respond based on the value of these fields.

#226 – Classes and Objects

A class is a data structure containing data and associated behavior.  You use the class keyword in C# to define a new class.  You use your user-defined class in the same way that you use built-in classes in the .NET Framework.

You can create an instance of a class, also known as an object, using the new keyword.  Each instance of a class has its own copy of the data defined by the class.

Here’s an example, a declaration of a new Person class.

    public class Person
    {
        public string FirstName;
        public string LastName;
        public int Age;

        public string DescribeMe()
        {
            return string.Format("{0} {1} is {2} yrs old.",
                                 FirstName, LastName, Age);
        }
    }

Once the class is defined, we can create new instances of the class, read/write its data and call its methods.

            Person p = new Person();
            p.FirstName = "James";
            p.LastName = "Joyce";
            p.Age = 40;

            string desc = p.DescribeMe();