Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

الكلمة المفتاحية 'this'

جزء من قسم Object Oriented Programming في رحلة C# على Coddy — الدرس 4 من 70.

تشير الكلمة الأساسية this إلى المثيل الحالي للفئة. تُستخدم للوصول إلى أعضاء المثيل والتمييز بين المعلمات والحقول التي تحمل الاسم نفسه.

استخدم this للتمييز بين الحقول والمعاملات

public class Person
{
    private string name;
    private int age;
    
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

بدون this، سيقوم المعامل بحجب الحقل

// لن يعمل هذا كما هو متوقع
public Person(string name, int age)
{
    name = name;  // يقوم بتعيين المعامل لنفسه!
    age = age;    // يقوم بتعيين المعامل لنفسه!
}

استخدم this مع الخصائص

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    
    public Product(string name, decimal price)
    {
        this.Name = name;
        this.Price = price;
    }
    
    public void DisplayInfo()
    {
        Console.WriteLine($"{this.Name}: ${this.Price}");
    }
}

استخدم this لاستدعاء منشئ آخر في نفس الفئة

public class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }
    
    public Rectangle(int width, int height)
    {
        this.Width = width;
        this.Height = height;
    }
    
    // منشئ للمربعات
    public Rectangle(int size) : this(size, size)
    {
    }
}

مثال على الاستخدام:

Rectangle rect = new Rectangle(5, 10);
Rectangle square = new Rectangle(5);  // ينشئ 5x5

تشير الكلمة المفتاحية this دائماً إلى مثيل الكائن الحالي. وهي مفيدة بشكل خاص عندما تتطابق أسماء المعلمات مع أسماء الحقول أو الخصائص، مما يجعل الكود الخاص بك أكثر وضوحاً ويمنع الأخطاء.

challenge icon

التحدي

متوسط

أكمل فئة Product بإضافة منشئ (constructor) يستخدم الكلمة المفتاحية this لتعيين قيم المعلمات للخصائص. قم أيضاً بتنفيذ الطريقة GetDescription() باستخدام this للوصول إلى الخصائص.

يجب أن تعيد الطريقة GetDescription() القيمة التالية: "{Name} costs ${Price}"

ورقة مرجعية

تشير الكلمة الأساسية this إلى المثيل الحالي للفئة وتُستخدم للوصول إلى أعضاء المثيل.

استخدم this للتمييز بين الحقول والمعاملات التي لها نفس الاسم:

public class Person
{
    private string name;
    private int age;
    
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

بدون this، يحجب المعامل الحقل ويسند القيمة لنفسه:

// لن يعمل هذا كما هو متوقع
public Person(string name, int age)
{
    name = name;  // يسند المعامل لنفسه!
    age = age;    // يسند المعامل لنفسه!
}

استخدم this مع الخصائص:

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    
    public Product(string name, decimal price)
    {
        this.Name = name;
        this.Price = price;
    }
    
    public void DisplayInfo()
    {
        Console.WriteLine($"{this.Name}: ${this.Price}");
    }
}

استخدم this لاستدعاء منشئ آخر في نفس الفئة (تسلسل المنشئات):

public class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }
    
    public Rectangle(int width, int height)
    {
        this.Width = width;
        this.Height = height;
    }
    
    // منشئ للمربعات
    public Rectangle(int size) : this(size, size)
    {
    }
}

Rectangle square = new Rectangle(5);  // ينشئ 5x5

جرّب بنفسك

using System;

class Program
{
    static void Main()
    {
        string name = Console.ReadLine();
        decimal price = decimal.Parse(Console.ReadLine());
        
        Product product = new Product(name, price);
        
        Console.WriteLine($"Product: {product.Name}");
        Console.WriteLine($"Price: ${product.Price}");
        Console.WriteLine(product.GetDescription());
    }
}
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس Object Oriented Programming