TOC

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

Classes:

Introduction to C# classes

في الكثير من برامج تعليم البرمجة يتم تأخير عرض المعلومات عن الفئات (classes). على العموم, لأن #C تعتمد أساسا على البرمجة كائنية التوجه (Object Oriented programming ) وبالتالي الفئات (classes) فسوف نُلقي نظرة سريعة , من خلال مُقدمة بسيطة, على أعم المُميزات.

في البداية, يمكن تعريف الفئة بأنها عبارة عن مجموعة من المهام (methods ) و المتغيرات (variables). وبذلك تصف الفئة هذه المهام و المتغيرات وفي أغلب الأحيان تستطيع خلق نموذج (instance ) من تلك الفئة والتي تٌسمى إصطلاحا بالكائن (object). في هذا الكائن يمكنك أستخدام تلك المهام و المتغيرات. في طبيعة الحال, يمكنك خلق العديد من تلك الكائنات. الفئات و البرمجة كائنية التوجه بشكل عام هو موضوع واسع. نحن سوف نُغطي بعض جوانبه في هذا الفصل , والفصول التالية, وليس كلها.

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

Okay, lots of new stuff here, but almost all of it is based on things we've already used earlier in this tutorial. As you can see, we have defined a new class, called Car. It's declared in the same file as our main application, for an easier overview, however, usually new classes are defined in their own files. It defines a single variable, called color, which of course is used to tell the color of our car. We declared it as private, which is good practice - accessing variables from the outside should be done using a property. The Color property is defined in the end of the class, giving access to the color variable.

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!