TOC

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

صنف :

Introduction to C# classes

في الكثير من برامج تعليم البرمجة , المعلومات المتعلقة با (اصناف ) سوف تكون محفوظة لوقت طويلة . على اي حال منذ ان بدأت سي شارب بالبرمجة كائنية التوجه , نحن سوف نبحث عن الاساس . مقدمة في معظم اهم ميزاته الآن.

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

بالنسبة للجزء المتعلق ب Hello World , قمنا و لأول مرة بالتعرف على فئة (class) مستخدمة , لأن لغة c# كلها مبنية ضمن مفهوم الفئات (classes) , دعنا الآن نقوم بتوسعة مثالنا (Hello world) مع فئة (class) سنقوم ببنائها بأنفسنا

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!