مرجع سريع

ورقة غش ميزات لغة C# 7.0

دليل مرجعي سريع لميزات C# 7.0: متغيرات out، مطابقة الأنماط، المجموعات وتفكيكها، الدوال المحلية، إرجاع المراجع، وفواصل الأرقام.

· تاريخ النشر

دليل مرجعي سريع لبناء الجملة والميزات المقدمة في C# 7.0 (.NET Framework 4.7 / .NET Core 2.0).

متغيرات out

التصريح عن متغيرات المخرجات مباشرة ضمن قائمة وسائط استدعاء الدوال:

// قبل C# 7:
int result;
if (int.TryParse(input, out result)) { ... }

// في C# 7:
if (int.TryParse(input, out int result))
{
    Console.WriteLine($"Parsed number: {result}");
}

// تجاهل وسائط out غير المطلوبة باستخدام _
if (int.TryParse(input, out _))
{
    Console.WriteLine("Input is valid numeric");
}

مطابقة الأنماط (Pattern Matching)

أنماط الأنواع باستخدام is

public void PrintArea(object shape)
{
    if (shape is Circle c)
    {
        Console.WriteLine($"Circle area: {Math.PI * c.Radius * c.Radius}");
    }
    else if (shape is Rectangle r)
    {
        Console.WriteLine($"Rectangle area: {r.Width * r.Height}");
    }
}

عبارات Switch مع الأنماط وعبارات when

switch (shape)
{
    case Circle c:
        Console.WriteLine($"Circle with radius {c.Radius}");
        break;
    case Rectangle r when r.Width == r.Height:
        Console.WriteLine($"Square {r.Width} x {r.Height}");
        break;
    case Rectangle r:
        Console.WriteLine($"Rectangle {r.Width} x {r.Height}");
        break;
    case null:
        throw new ArgumentNullException(nameof(shape));
    default:
        Console.WriteLine("Unknown shape");
        break;
}

المجموعات (Tuples) والتفكيك (Deconstruction)

تتطلب حزمة System.ValueTuple في الإصدارات القديمة:

إرجاع مجموعات بأسماء حقول

(string First, string Last, int Age) GetUser()
{
    return ("Alice", "Smith", 30);
}

var user = GetUser();
Console.WriteLine($"{user.First} is {user.Age} years old");

تفكيك المجموعات

// التفكيك إلى متغيرات فردية
var (first, last, age) = GetUser();

// أو تفريغها في متغيرات موجودة مسبقاً
string f, l;
int a;
(f, l, a) = GetUser();

الدوال المحلية (Local Functions)

التصريح عن دوال مساعدة متداخلة داخل نطاق الدالة الأصلية:

public int Factorial(int n)
{
    if (n < 0) throw new ArgumentException("Negative value", nameof(n));
    return Calculate(n);

    int Calculate(int x)
    {
        return x <= 1 ? 1 : x * Calculate(x - 1);
    }
}

تحسينات القيم الحرفية (Literals)

// فواصل الأرقام (_)
int million = 1_000_000;
long hexMask = 0xFF_EC_00_12;

// الأرقام الثنائية (0b)
byte byteFlag = 0b1010_0110;

المتغيرات المحلية والإرجاع عبر المرجع (ref)

إرجاع مراجع لمواقع التخزين بدلاً من نسخ القيم:

public ref int Find(int target, int[] numbers)
{
    for (int i = 0; i < numbers.Length; i++)
    {
        if (numbers[i] == target) return ref numbers[i];
    }
    throw new IndexOutOfRangeException();
}

int[] array = { 10, 20, 30 };
ref int val = ref Find(20, array);
val = 99; // array[1] becomes 99

أعضاء التعبير المختصر (Expression-Bodied Members)

توسيع بناء الجملة المختصر ليشمل دوال البناء والمفرغات ومحددات الخصائص:

class Item
{
    private string _name;

    // Expression-bodied constructor
    public Item(string name) => _name = name ?? throw new ArgumentNullException(nameof(name));

    // Expression-bodied property accessor
    public string Name
    {
        get => _name;
        set => _name = value ?? "default";
    }
}

موضوعات ذات صلة