TOC

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

Classes:

Introduction to C# classes

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.

Oncelikle, bir sinif birbirleriyle alakali method ve parametreler grubu olarak tanimlanabilir. Bir sinif butun bunlari tanimlar, ve cogu durumda, bu sinifin bir ornegini olusturabilirsin, ki bu sinifin nesnesi olarak tanimlanir. Bu nesne uzerinden, sinifin tanimlanmis methodlarini ve parametrelerini kullanabilirsin. Istersen bir sinifin birden cok nesnesini de olusturabilirsin tabii ki. Siniflar, ve genel anlamda nesne bazli programlama, oldukca genis bir konudur. Bu kisimda bunun yalnizca bir kismini anlatacagiz, gelecek bolumlerde ise biraz daha cogunu, ama tamamini degil.

In the Hello world chapter, we saw a class used for the first time, since everything in C# is built upon classes. Let's expand our Hello world example with a class we build on 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; }
        }
    }
}

Tamam, burada cokca anlatilacak sey var, ama neredeyse cogu onceki bolumlerde anlatildi bile. Ornektede goruldugu gibi, Car (araba) diye bir sinif tanimlandi. Bu sinif temel uygulamayla ayni dosya icinde bildirilir., daha kolay bir yaklasim icin, buna ragmen, yeni siniflar genellikle kendi siniflari icinde tanimlanirlar. Burada tek bir degisken, color olarak adlandirilan, tanimlanmistir. Bu degisken arabanin rengini anlatmak icin tanimlanmistir. Bunu ozel olarak tanimlarsak ki bu iyi bir pratik olur, disaridan bu degere ulasmak isteyenlerin ozellik kullanmasi gerekir. Color ozelligi ise sinifin sonunda tanimlanir ki color degiskenine erisim saglanabilsin.

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!