classes Introduction in C#

In many programming tutorials, information about classes will be much later. However, since C # object is about Oriented programming and class, we will already introduce a basic introduction to the most important things.

First of all, a class is a group of related methods and variables. A class describes these things, and in most cases, you make an example of this class, which is now referred to as an object on this object, you use defined methods and variables. Of course, you want you to create many examples of your class. Normally, classes, and object-oriented programming, is a big topic. We will include something in this lesson and later chapters, but not all this.

In the Hello world chapter, we used the first class, because everything in C # has been built on the classes. Hello, extend our examples of the world, we have made it with a class of our own.


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Car car;

            car = new Car("Red");
            Console.WriteLine(car.Describe());

            car = new Car("Green");
            Console.WriteLine(car.Describe());

            Console.ReadLine();

        }
    }

    class Car
    {
        private string color;

        public Car(string color)
        {
            this.color = color;
        }

        public string Describe()
        {
            return "This car is " + Color;
        }

        public string Color
        {
            get { return color; }
            set { color = value; }
        }
    }
}

Okay, here are many new things, but almost everything is based on the stuff used in this tutorial. As you can see, we have defined a new class, which is called a car, has been declared as our main application in the same file for the same overview, however, new organs usually have their files , It defines a single variable, which is called color, which is used to describe the color of our car, we have declared it as private, which is good. Believe that - should use variables without using property. The color property is defined at the end of the class so that the color variables can be used.

In addition, our car class defines the creator because it takes a parameter that allows us to start car objects with colors. Since there is only one constructor, car objects can only be done with color as well as with the institute. The description () method allows us to get a good message with such information that we record about ourselves, it only gives a string with the information given by us

Now, in our main application, we announce a type of car. After that, we make it a new example as a parameter with "red". According to the code of our class, it means that to verify that the color will be specified as the color of the red car, we call the details () method, and to demonstrate that we have a lot of Classes are just simple examples, we do it again, but with any other color we have made our first functional class and used it.

In the following chapters, concepts like properties, constructors and visibility will be explained in more depth.