المتغيرات والثوابت في لغة C#

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

ما هو المصطلح المستخدم لوصف حقل في الذاكرة يتم فيه تخزين البيانات ويمكن تغيير محتوياته عدة مرات داخل البرنامج؟

  • الفئة (Class)
  • الثابت (Constant)
  • المتغير (Variable) (correct)
  • الإجراء (Method)

أي من الخيارات التالية يمثل الفرق الأساسي بين المتغيرات والثوابت في لغة #C؟

  • المتغيرات يجب تعريفها داخل الإجراءات، بينما الثوابت يمكن تعريفها على مستوى النموذج.
  • المتغيرات يمكن تغيير قيمتها أثناء تنفيذ البرنامج، بينما الثوابت تحتفظ بقيمتها الثابتة. (correct)
  • المتغيرات تستخدم لتخزين أنواع مختلفة من البيانات، بينما الثوابت مخصصة للأرقام فقط.
  • المتغيرات حساسة لحالة الأحرف، بينما الثوابت ليست كذلك.

أي من القواعد التالية يجب اتباعها عند تسمية المتغيرات في #C؟

  • يمكن أن يكون الاسم مطابقًا للكلمات المحجوزة في اللغة.
  • يجب أن يبدأ الاسم برقم.
  • يمكن أن يحتوي الاسم على رموز وعلامات خاصة.
  • يجب ألا يحتوي الاسم على مسافات. (correct)

ماذا يحدث إذا حاولت استخدام متغير في #C قبل الإعلان عنه؟

<p>يظهر خطأ في وقت التصميم. (D)</p>
Signup and view all the answers

أي نوع من البيانات يجب استخدامه لتخزين عدد صحيح يتراوح بين -32,768 و 32,767؟

<p>short (C)</p>
Signup and view all the answers

أي من أنواع البيانات التالية يستخدم لتخزين قيمة منطقية (إما صح أو خطأ) في #C؟

<p>bool (B)</p>
Signup and view all the answers

ما هي القيمة الافتراضية للمتغير المنطقي (bool) في #C إذا لم يتم تحديد قيمة ابتدائية له؟

<p>false (A)</p>
Signup and view all the answers

أي من العمليات التالية تعتبر تخصيصًا صحيحًا لقيمة المتغير age من النوع int؟

<p><code>int age = 25;</code> (A)</p>
Signup and view all the answers

ماذا تعني حساسية حالة الأحرف (Case Sensitive) في سياق أسماء المتغيرات في #C؟

<p>البرنامج يميز بين الأحرف الكبيرة والصغيرة في أسماء المتغيرات، ويعتبرها مختلفة. (A)</p>
Signup and view all the answers

أي من الخيارات التالية يعرض طريقة صحيحة للإعلان عن متغير باسم userName من النوع string وتخصيص القيمة "John" له في سطر واحد؟

<p><code>string userName = &quot;John&quot;;</code> (B)</p>
Signup and view all the answers

في #C، أين يجب تعريف المتغيرات لكي تكون متاحة لجميع الإجراءات (methods) داخل نفس النموذج (Form)؟

<p>خارج جميع الإجراءات، مباشرة داخل تعريف الفئة (Class) الخاصة بالنموذج. (A)</p>
Signup and view all the answers

ماذا يطلق على المتغيرات التي يتم تعريفها داخل إجراء معين في #C؟

<p>متغيرات محلية. (B)</p>
Signup and view all the answers

ما هو المقصود بـ "فترة حياة المتغير" (Life Time) في #C؟

<p>المدة التي يظل فيها المتغير محتفظًا بقيمته في الذاكرة. (D)</p>
Signup and view all the answers

في #C، كيف تعلن عن ثابت من النوع float باسم PI وتخصص له القيمة 3.14؟

<p><code>const float PI = 3.14;</code> (D)</p>
Signup and view all the answers

لماذا نستخدم الثوابت المعرفة من قبل المبرمج (const) بدلاً من كتابة القيم مباشرة في الكود؟

<p>لتسهيل تغيير القيمة في المستقبل وتجنب تكرارها في عدة أماكن. (C)</p>
Signup and view all the answers

أي دالة (method) تستخدم لتحويل قيمة رقمية إلى نص (string) في #C؟

<p>ToString() (D)</p>
Signup and view all the answers

ما هي وظيفة الدالة ()Parse في #C؟

<p>تحويل قيمة نصية إلى قيمة رقمية. (B)</p>
Signup and view all the answers

إذا كان لديك متغير باسم number من النوع int وقيمته 123، كيف يمكنك عرضه في مربع نص باسم textBox1؟

<p>كل ما سبق صحيح (B)</p>
Signup and view all the answers

في #C، ماذا يحدث إذا حاولت جمع قيمتين نصيتين (string) باستخدام العامل +؟

<p>يتم دمج القيمتين النصيتين معًا. (D)</p>
Signup and view all the answers

لنفترض أن لديك مربع نص باسم textBox1 يحتوي على الرقم "25" ومربع نص باسم textBox2 يحتوي على الرقم "10". ما هي نتيجة الكود التالي: textBox3.Text = textBox1.Text + textBox2.Text;؟

<p>2510 (A)</p>
Signup and view all the answers

كيف يمكنك تحويل المتغير floatNumber الذي قيمته 3.14 إلى النوع int؟

<p>الخياران (ب) و (ج) صحيحان (C)</p>
Signup and view all the answers

ما هي وظيفة الكلمة المفتاحية ToString()؟

<p>تحويل البيانات إلى نصوص. (D)</p>
Signup and view all the answers

أي طريقة تصف بشكل صحيح كيفية تعامل #C مع العمليات الحسابية عند تخصيص قيمة لمتغير؟

<p>يتم تقييم الجانب الأيمن من العملية أولاً، ثم يتم تخزين الناتج في المتغير الموجود على الجانب الأيسر. (B)</p>
Signup and view all the answers

ماذا يحدث عند محاولة تعيين قيمة من نوع بيانات أكبر (مثل long) إلى متغير من نوع بيانات أصغر (مثل int

<p>يحدث فقدان للبيانات أو قد يظهر خطأ، لأن القيمة قد تتجاوز سعة المتغير. (D)</p>
Signup and view all the answers

في أي جزء من الكود يتم تعريف المتغيرات العامّة (Global Variables)؟

<p>خارج جميع الدوال مباشرة داخل الفئة. (C)</p>
Signup and view all the answers

أيٌ من الخيارات التالية يصف بشكلٍ صحيح نطاق المتغير المحلي (Local Variable)؟

<p>يمكن الوصول إليه فقط من الدالة التي تم تعريفه فيها. (D)</p>
Signup and view all the answers

في #C, ما هي النتيجة المتوقعة للكود التالي؟ int a = 5; a = 2 * a + 2;

<p>12 (D)</p>
Signup and view all the answers

ما هي الكلمة المفتاحية التي تستخدم لتعريف متغير لا يمكن تغيير قيمته بعد الإعلان عنه؟

<p>const (D)</p>
Signup and view all the answers

أي نوع من المتغيرات يتم تهيئته تلقائيًا بالقيمة 0 إذا لم يتم تحديد قيمة ابتدائية له؟

<p>متغيرات رقمية (B)</p>
Signup and view all the answers

لنفترض أن لديك الكود التالي في #C: double x = 5.7; int y = Convert.ToInt32(x); ما هي قيمة المتغير y بعد تنفيذ هذا الكود؟

<p>6 (A)</p>
Signup and view all the answers

في #C، ما هي الطريقة الصحيحة لتعريف متغير سلسلة نصية باسم message وتعيين القيمة "Hello World" له؟

<p><code>string message = &quot;Hello World&quot;;</code> (A)</p>
Signup and view all the answers

ماذا يحدث إذا قمت بتعريف متغيرين بنفس الاسم داخل نفس النطاق (scope) في #C؟

<p>سيحدث خطأ في وقت الترجمة (compile-time error) بسبب تكرار التعريف. (D)</p>
Signup and view all the answers

أي من الخيارات التالية يمثل النوع الأكثر ملاءمة لتخزين قيمة عددية صحيحة صغيرة مثل عمر شخص؟

<p>byte (C)</p>
Signup and view all the answers

في #C، ماذا تعني الكلمة المفتاحية var عند تعريف متغير؟

<p>أن نوع المتغير سيتم تحديده تلقائيًا بناءً على القيمة التي يتم تعيينها له. (D)</p>
Signup and view all the answers

Flashcards

المتغيرات (Variables)

حقول في الذاكرة لتخزين البيانات، قابلة للتغيير أثناء البرنامج.

الثوابت (Constants)

حقول في الذاكرة قيمتها ثابتة لا تتغير أثناء تشغيل البرنامج.

قواعد تسمية المتغيرات

يجب أن يبدأ بحرف هجائي (a-z, A-Z). حساسة لحالة الأحرف. لا يحتوي على رموز أو علامات خاصة. إذا كان الاسم مكون من أكثر من كلمة، لا تستخدم مسافات أو شرطات. لا يكون كلمة محجوزة في اللغة.

الإعلان عن المتغير

تحديد اسم ونوع المتغير ليتم حجز المساحة اللازمة له في ذاكرة الحاسوب.

Signup and view all the flashcards

int

نوع بيانات يخزن أرقامًا صحيحة (بدون كسور).

Signup and view all the flashcards

float

نوع بيانات يخزن أرقامًا عشرية (بكسور).

Signup and view all the flashcards

string

نوع بيانات يخزن سلسلة من الحروف.

Signup and view all the flashcards

bool

نوع بيانات يخزن قيمة منطقية (إما True أو False).

Signup and view all the flashcards

تخصيص القيمة للمتغير

يتم تخزين القيمة في المتغير.

Signup and view all the flashcards

المتغير المحلي

المتغيرات التي يتم تعريفها داخل إجراء معين.

Signup and view all the flashcards

متغير على مستوى النموذج

المتغيرات التي يتم تعريفها خارج الإجراءات في الجزء الخاص بإنشاء الـ Form ، مداها على مستوى النموذج

Signup and view all the flashcards

المتغيرات العامة

يمكن استخدامة في كافة النماذج الأخرى ويظل محتفظا بقيمته حتى نهاية البرنامج

Signup and view all the flashcards

متغيرات محلية Local Variables) على مستوى الإجراء

يتم تعريفها داخل الإجراء، مداها على مستوي الإجراء الذي تم الإعلان عنها بداخله.

Signup and view all the flashcards

في الذاكرة

صفحة الكود الخاصة بأى Form تحتفظ بهذه المتغيرات بقيمتها حتي ينتهي تنفيذ الإجراء فيقوم C#.NET بإزالتها من الذاكرة.

Signup and view all the flashcards

الثابت

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

Signup and view all the flashcards

أستخدمه مباشرة

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

Signup and view all the flashcards

تغير طفيف

لتعديل كل العمليات المعتمدة علي هذه القيمة مباشرة .

Signup and view all the flashcards

قيمة نصية

أي قيمه يتم وضعها بين علامتي تنصيص "" يتعامل معها البرنامج على أنها قيمة نصية حتى ولو كانت أرقاما .

Signup and view all the flashcards

حتي ولو بالكتابة

ويمكن كتابة القيمة نفسها بين علامتي تنصيص، أو اسم متغير أو عنصر تحكم به القيمة.

Signup and view all the flashcards

int xx

وهو إجراء ستم خاص بفئات الأرقام int ، لذا أعد كتابة الأمر الخاص بعملية الجمع كالتالي:

Signup and view all the flashcards

Study Notes

  • These notes summarize key concepts about variables and constants in C#.

Variables, Constants

  • After studying this topic, one should be able to:
    • Define variables and constants.
    • Differentiate between the use of variables and constants.
    • Choose a valid name for a variable.
    • Identify invalid variable names.
    • Summarize the different types of variables and constants.
    • Declare variables and constants.
    • Identify the error message that appears when using an undeclared variable.
    • Assign values to variables and constants.
    • Distinguish between using public variables and local variables.
    • Declare public variables and local variables.
    • Explain the meaning of a variable's lifetime and scope.
    • Differentiate between constants declared by the programmer and constants defined in C#.
    • Convert numerical data into character data so that it can be placed in a text box.
    • Convert between different data types.

What are Variables?

  • Data can be stored in memory without being shown to the user except when necessary.
  • Variables are named memory locations which can have data stored in them, for use within programs.
  • Variables can store numeric, textual, or other types of values.
  • It is necessary to assign a unique name to each variable; the variable can be accessed by its name.
  • The value of the variable can be changed at any time by placing new values in it, which replaces the old value.

What are Constants?

  • Variables have contents that may change during program execution by commands, not by themselves.
  • Constants are memory locations with contents that cannot be changed during program execution.
  • In a program to calculate the area of a circle with radius (r):
    • The area of the circle varies with the radius.
    • The value of (pi) is constant equals (3.14) and does not change.
  • The value of (r) can be stored in a (Variable), while the value of (pi) is constant and is stored in (Constant).

Naming Variables

  • Everything must have a name.
  • A variable's name is used when storing or retrieving values in that variable.

Rules for Naming Variables

  • A variable name must begin with a letter.
  • Variables are case-sensitive; capitalization must be consistent.
  • Names should not contain any special symbols such as periods or question marks.
  • Spaces and hyphens are not allowed in multi-word names; use underscores instead.
  • Each word in a variable name can begin with a capital letter.
  • Variable names cannot be reserved names in the language (e.g., IF, Print, Or).

Common Naming Errors

  • A variable name should not start with a digit.
  • A variable name should not contain special characters.
  • Variables should have names in English letters.
  • If a word is reserved by the programming language, it cannot be used as a variable name.

Declaring Variables and Data Types

  • Every variable has a type depending on the nature of the data.
  • The variable can hold a number, decimal, or text value.
  • Each data type has a specific size to reserve memory without wasting space.
  • Basic data is either characters or numbers. Numbers are either integers (without fractions) or floating points (with fractions).
  • There are variables that can store positive and negative numbers which are called Signed. And Unsigned variables that can store only positive numbers.
  • Declaring a variable involves specifying its name and type to reserve the required memory space.

General Form for Variable Declaration

  • Datatype Variable Name;

Numerical Data Types: Integers

  • The primitive numeric types represent signed integers if a signed modifier is used.
  • They conversely represent unsigned integers if that modifier is missing.

Signed Integer Types

  • byte: Stores integer values from 0 to 255 and occupies 1 byte.
  • sbyte: Stores integer values from -128 to 127 and occupies 1 byte.
  • short: Stores integer values from -32,768 to 32,767 and occupies 2 bytes.
  • ushort: Stores integer values from 0 to 65,535 and occupies 2 bytes.
  • int: Stores integer values from -2,147,483,648 to 2,147,483,647 and occupies 4 bytes.
  • uint: Stores integer values from 0 to 4,294,967,295 and occupies 4 bytes.
  • long: Stores integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and occupies 8 bytes.
  • ulong: Stores integer values from 0 to 18,446,744,073,709,551,615 and occupies 8 bytes.

Declaring Numerical Variables With Fractions

  • float: Stores fractional numbers and occupies 4 bytes. Its range 1.5 x 10^-45 to 3.4 x 10^38 with 7 decimal places.
  • double: Stores fractional numbers and occupies 8 bytes. Its range 5.0 x 10^-324 to 1.7 x 10^308 with 16 decimal places.
  • decimal: Stores fractional numbers and occupies 16 bytes. Its range 1.0 x 10^-28 to 7.9 x 10^28 with 29 decimal places.

Declaring Floating Points Variables

  • All decimal variables are of type double unless specifically defined as float or decimal.
  • When defining decimal, add the letter m or M after the value that will be stored in the variable, and in the case of float, add the letter f or F after the value.
  • If the numerical variables are not stored with a value, the default value will be (0).

Logical variables

  • Store a value of True (Yes/Correct) or False (No/Wrong).
  • If the value of logical variable isn't set, the value is assumed to be false, and it should be noted that the word True or False is written in small letters.

Declaring Variables With text

  • string: Variables to set the names of people, sentences or paragraphs depending on the stored characters.
  • char: Single characters are stored, and each variable is 2 Bytes in size.
  • If the textual variables are not stored with a value, the variable will be Blank.

Dealing with Variables

  • This refers to storing the value in the variable and is called (Assignment), or using the value of the variable as its view or using the value of the variable in an arithmetic operation.

Assign a Variable to a Value

  • A value can be assigned to a variable directly or through the result of an operation, or with an arithmetic operation.
  • When a numerical value is assigned to variables, it is done in the previous codes, but in the case of String variables, the value must be placed between double quotes.
  • After declaring a variable and storing the required value in it, it can be used in different ways.
  • If you have a variable named Age, you can store the value of this variable in another variable as follows:
    • Xx = Age;
    • The computer handles the first statement as assigning the value 30 to the variable Sum.
    • The second statement calculates the value of the right side of the (=) sign, by placing the value of the Sum variable, which equals 30, and the right side becomes 30 + 10 and after performing the addition operation, the result equals (40).
    • The output result (40) is stored in the variable to the left of the sign (=), which means that the value of the Sum variable will change from the value (30) to a new value, which is (40).
  • It is possible to put the value of the variable Age in a text box as follows:
    • Textbox1 = Age;
  • You can display the value of the variable using a message box such as:
    • MessageBox.Show (Age);
  • Since C#.Net does not convert numerical variables into texts automatically and the text box cannot receive numbers unless it is in the form of texts, the numbers had to be converted to texts.

Variable Declaration Scope

  • Variable definitions can be placed in any procedure or in a class that contains this procedure.
  • The more limited the scope, the more restricted the use of the variable. Also, this depends on how long you want the variable to retain its value, which is called life time.
  • If the variables a, b are defined only in the procedure Button1_Click specific, the procedure Button2_Click of the second button cannot be utilized and dealt with.
  • Variables that are defined within a specific procedure cannot be used within another procedure, because they are considered local variables in this case. Only the procedure in which they are defined can use them.

How to Solve the Previous Problem

  • It is required when clicking the Button2 button so that the product of the numbers appears in the textBox1 text box, and here the range of the variables must be expanded meaning that all procedures at the form level can use these two variables.

Local Variables at Process Level

  • They are defined inside the procedure.
  • They range at the level of the procedure in which they were declared, meaning that only this procedure can use these variables, not other procedures.
  • These variables retain their value until the procedure is executed, and C#.NET removes them from memory.

Variables at The File or Form Level

  • They are the variables that are defined outside the procedures in the part of creating the form.
  • They range at the model level and include procedures meaning that the variable is available for any procedure in this model.
  • These variables remain in memory and retain their values throughout the operation of the model or file it contains, and end when the model stops.

Global Variables

  • They are used if there is more than one window or model inside your project, and you want these variables to be available to all these models.
  • Here, such variables are defined in class so that they can be used in all other models and retain their value until the end of the program as will be seen later.

Constants Specific Declared by The Programmer

  • These are constants that the programmer defines within the project to serve the purposes of the project, and they must be declared as in variables completely and with the same rules, but with the Const keyword as in the following example:
    • const float Tax_Rate = 0.07;
  • The advantage to this is so that if the (0.07) must be used multiple times, then it may not be required every number entry to be checked again to ensure that it is correct.

Converting Between Data Types

  • #C enables the conversion between different data types, i.e., the character value 250 can be converted to the numerical value 250 and vice versa, as well as for other data types.
  • Any value placed between quotation marks is treated as pure text in the code.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Computer Science Basics: Software & Data Types
37 questions
C++ Programming Basics
16 questions

C++ Programming Basics

SharpestCarbon8983 avatar
SharpestCarbon8983
Introduction to C++ Programming
5 questions
Variables y Tipos de Datos
45 questions
Use Quizgecko on...
Browser
Browser