غير محدد ( مرجع C# )

الكلمة الأساسية unchecked يستخدم لمنع تجاوز التدقيق لنوع تكامل العمليات الحسابية والتحويلات.

في السياق الغير محدد, إذا كان التعبير يعطي القيمة التي تقع خارج نطاق نوع الوجهة، فلا يتم الإشارة لتجاوز السعة. على سبيل المثال، لأن إجراء العمليات الحسابية في المثال التالي يتم إجراءها في قالب أو تعبير unchecked , حقيقة أن النتيجة كبيرة جداً بالنسبة لعدد يتم تجاهلها, و int1 يتم تعيين القيمة-2,147,483,639.

unchecked
{
    int1 = 2147483647 + 10;
}
int1 = unchecked(ConstantMax + 10);

إذا تمت إزالة البيئة unchecked , سيظهر خطأ في ترجمة. يمكن الكشف عن تجاوز السعة في وقت التحويل برمجياً لأن كل مصطلحات التعبير عبارة عن ثوابت.

التعبيرات التي تحتوي على مصطلحات غير ثابتة لا يتم تحديدها إلى الافتراضي في وقت التحويل برمجياً ولا وقت التشغيل. انظر محدد من (C# مرجع) لمزيد من المعلومات حول تمكين البيئة المحددة.

لأن التحقق من تجاوز السعة يستغرق الوقت, فاستخدام رمز غير محدد في حالات حيث لا يوجد خطر من تجاوز السعة يمكن من تحسين الأداء. ومع ذلك، إذا كان تجاوز السعة أمر محتمل, فإنه يجب استخدام بيئة محدده.

مثال

يبين هذا النموذج كيفية إلى استخدام الكلمة الأساسية unchecked .

class UncheckedDemo
{
    static void Main(string[] args)
    {
        // int.MaxValue is 2,147,483,647.
        const int ConstantMax = int.MaxValue;
        int int1;
        int int2;
        int variableMax = 2147483647;

        // The following statements are checked by default at compile time. They do not
        // compile.
        //int1 = 2147483647 + 10;
        //int1 = ConstantMax + 10;

        // To enable the assignments to int1 to compile and run, place them inside 
        // an unchecked block or expression. The following statements compile and
        // run.
        unchecked
        {
            int1 = 2147483647 + 10;
        }
        int1 = unchecked(ConstantMax + 10);

        // The sum of 2,147,483,647 and 10 is displayed as -2,147,483,639.
        Console.WriteLine(int1);


        // The following statement is unchecked by default at compile time and run 
        // time because the expression contains the variable variableMax. It causes  
        // overflow but the overflow is not detected. The statement compiles and runs.
        int2 = variableMax + 10;

        // Again, the sum of 2,147,483,647 and 10 is displayed as -2,147,483,639.
        Console.WriteLine(int2);

        // To catch the overflow in the assignment to int2 at run time, put the
        // declaration in a checked block or expression. The following
        // statements compile but raise an overflow exception at run time.
        checked
        {
            //int2 = variableMax + 10;
        }
        //int2 = checked(variableMax + 10);

        // Unchecked sections frequently are used to break out of a checked 
        // environment in order to improve performance in a portion of code 
        // that is not expected to raise overflow exceptions.
        checked
        { 
            // Code that might cause overflow should be executed in a checked
            // environment.
            unchecked
            { 
                // This section is appropriate for code that you are confident 
                // will not result in overflow, and for which performance is 
                // a priority.
            }
            // Additional checked code here. 
        }
    }
}

مواصفات لغة #C

لمزيد من المعلومات، راجع مواصفات لغة #C. مواصفات اللغة هي المصدر النهائي لبناء جملة C# واستخدامها.

راجع أيضًا:

المرجع

الكلمات الأساسية لـ #C

المفحوصة والغير مفحوصة (مرجع #C)

محدد من (C# مرجع)

المبادئ

دليل البرمجة لـ #C

موارد أخرى

مرجع C#‎