Learn Object Oriented Programming Using C#: Part 3

Before reading this article, please go through the following articles:

  1. Learn Object Oriented Programming Using C#: Part 1

  2. Learn Object Oriented Programming Using C#: Part 2

More about C# Class Properties

In Part 2 we just described the basic understanding of properties of a class. In this lesson we will discuss various types of properties in detail.

Types of Properties

The  following are the types of properties:

  • Read only property.
  • Write only property.
  • Auto implemented property.

Read only Properties

Using a simple technique we can apply the read only property by just defining the get accessor in the property implementation.

Example

using System;

 

namespace Readonlyproperties

{

    class Program

    {

        static void Main(string[] args)

        {

            Car SportCar = new Car();

 

            Console.WriteLine(

                "Model: {0}",SportCar.Model_ID);

 

            Console.WriteLine(

               "Maker: {0}",SportCar.Maker_Name);

 

            Console.ReadKey();

        }

        public class Car

        {

            private int model = 2012;

            private string maker = "mercedes";     

           

            public int Model_ID

            {

                get

                {

                    return model;

                }                

            }

 

            public string Maker_Name

            {

                get

                {

                    return maker;

                }

            }

        }

 

    }

}

In the example above we create the Car class with two read-only properties, Model_ID and Maker_Name. You can say that each property is read-only because they only have get accessors. We assign the values for the model and maker at the time of defining the variables. In this case, the values are 2012 and "mercedes".

The Main method of the Readonlyproperties class instantiates a new Car object named SportCar. The instantiation of SportCar uses the default constructor of the Car class.

Since the model and maker properties of the Car class are read-only, if you insert SportCar.Model_ID = 2013 into the example, the program will generate an error and not compile, because Model_ID is read-only; the same goes for Maker_Name. When the Model_ID and Maker_Name properties are used in Console.WriteLine, they work fine. This is because these are read operations that only invoke the get accessor of the Model_ID and Maker_Name properties.

Write only Property

We have discussed read only properties. Now we will discuss the write-only property; there is very little difference between the read-only and write-only properties. A write-only property only has a set accessor.

Example

using System;

 

namespace WriteOnlyProperty

{

    class Program

    {

        static void Main(string[] args)

        {

            Car SportsCar = new Car();

            SportsCar._model = 2013;

            SportsCar._maker = "lamborghini";

            SportsCar.DisplayCustomerData();

            Console.ReadKey();

 

        }

        public class Car

{

    private int model = -1;

 

    public int _model

    {

        set

        {

            model = value;

        }

    }

  

 

    private string maker = string.Empty;

 

    public string _maker

    {

        set

        {

            maker = value;

        }

    }

 

    public void DisplayCustomerData()

    {

        Console.WriteLine(

                "Model: {0}", model );

 

        Console.WriteLine(

           "Maker: {0}", maker );

 

    }

}

 

    }

 

}

In the example above we create the Car class with two write-only properties, Model_ID and Maker_Name. You can say that each property is write-only because they only have set accessors. Using the set property we have assigned values to model and maker. In this case, the values are 2013 and "lamborghini".

The Main method of the WriteOnlyProperty class instantiates a new Car object named SportCar. The instantiation of SportCar uses the default constructor of the Car class.

Since the model and maker properties of the Car class are write-only, if you inserted Console.WriteLine (SportCar. Model_ID) into the example, the program will generate an error and not compile, because Model_ID is write-only; the same goes for Maker_Name. When the Model_ID and Maker_Name properties are used in SportCar.Model_ID =2012, they work fine. This is because these are write operations that only invoke the set accessor of the Model_ID and Maker_Name properties.

Auto implemented Property

C# 3.0 introduced a new class property, called auto implemented property, that creates properties without get and set accessor implementations.

Example
 

using System;

 

namespace AutoImplementedproperty

{

    class Program

    {

        static void Main(string[] args)

        {

            car SportCar = new car();

 

            SportCar.model = 2014;

            SportCar.maker = "ferrari";

 

            Console.WriteLine(

                "Model: {0}", SportCar.model);

 

            Console.WriteLine(

               "Maker: {0}", SportCar.maker);

 

 

            Console.ReadKey();

 

        }

        public class car

        {

            public int model { get; set; }

            public string maker { get; set; }

        }

 

    }

}

Please note that the get and set accessors in this example do not have implementations. In an an auto-implemented property, the C# compiler performs the traditional properties behind the scenes. Main methods use the same traditional properties in auto-implemented property that we discussed earlier.