TOC

This article is currently in the process of being translated into Romanian (~51% done).

Clase:

Introducere in clase C#

In lots of programming tutorials, information about classes will be saved for much later. However, since C# is all about Object Oriented programming and thereby classes, we will look at a basic introduction to the most important features now.

In primul rand, o clasa este un grup de metode si variabile. O clasa descrie aceste metode si variabile si in cele mai multe cazuri, poti crea o instanta a clasei adica un obiect. In acest obiect folosesti metodele si variabilele definite in clasa. Bineînțeles ca poti crea oricat de multe instante ale acestei clase. Clasele si Programarea Orientata pe Obiect este in general un topic mare. O sa acoperim o parte din subiecte in acest capitol si in capitolele ce urmeaza.

In capitolul intitulat "Hello World" am folosit pentru prima data o clasa din moment ce orice construim in C# se bazeaza pe clase. Sa extindem exemplul Hello world cu o clasa pe care o vom construii impreuna:

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; }
        }
    }
}

Okey, o multime de lucruri noi aici dar aproape toate se bazeaza pe lucruri pe care noi le-am folosit anterior in acest tutorial. Asa cum poti vedea , am definit o clasa noua numita Car. Este declarata in acelasi fisier ca si functia main pentru o mai buna vizualizare dar de obicei fiecare clasa se defineste in propriul fisier. Aceasta clasa defineste o singura variabila, numita culoare, care ne zice culoarea masinii. Am declarat-o privata , fiind o buna practica-deoarece pentru a accesa variabile in afara clasei ar trebuii sa utilizam proprietati. Proprietatea Color este definita la finalul clasei dand acces la culoarea variabilei.

Besides that, our Car class defines a constructor. It takes a parameter which allows us to initialize Car objects with a color. Since there is only one constructor, Car objects can only be instantiated with a color. The Describe() method allows us to get a nice message with the single piece of information that we record about our car. It simply returns a string with the information we provide.

Now, in our main application, we declare a variable of the type Car. After that, we create a new instance of it, with "Red" as a parameter. According to the code of our class, this means that the color red will be assigned as the color of the car. To verify this, we call the Describe() method, and to show how easily we can create several instances of the same class, we do it again, but with another color. We have just created our first functional class and used it.

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


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!