امثلة عن المتغيرات VARIABLES في #C

  • 2020-10-04

توصيف

فيما يلي امثلة عن المتغيرات variables  واستخدامها في #C

 

انشاء متحول و اسناد قيمة له:

// string variable
string name = "John";
Console.WriteLine(name); // for print 
// number variable
int myNum = 15;
Console.WriteLine(myNum); // for print
// other types
double myDoubleNum = 5.99;
char myLetter = 'D';
bool myBool = true;
string myText = "Hello";

 

كود جمع متغيرين من نوع String:

string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
Console.WriteLine(fullName);

 

كود جمع متغيرين من نوع Number:

int x = 5;
int y = 6;
Console.WriteLine(x + y); // Print the value of x + y

 

كود ادخال الاسم من الكيبورد و طباعته :

// Type your username and press enter
Console.WriteLine("Enter username:");

// Create a string variable and get user input from the keyboard and store it in the variable
string userName = Console.ReadLine();

// Print the value of the variable (userName), which will display the input value
Console.WriteLine("Username is: " + userName);

 

كود تعريف متغير من نوع boolean وطباعته:

bool isCSharpFun = true;
bool isFishTasty = false;
Console.WriteLine(isCSharpFun);   // Outputs True
Console.WriteLine(isFishTasty);   // Outputs False

 

#Casting in C

امثلة على قص انواع البيانات في #C

 

1) القص الضمني Implicit Casting :

int myInt = 9;
double myDouble = myInt;       // Automatic casting: int to double

Console.WriteLine(myInt);      // Outputs 9
Console.WriteLine(myDouble);   // Outputs 9

 

 

2) القص الصريح Explicit Casting :

double myDouble = 9.78;
int myInt = (int) myDouble;    // Manual casting: double to int

Console.WriteLine(myDouble);   // Outputs 9.78
Console.WriteLine(myInt);      // Outputs 9

 

3) طرق تحويل النمط Type Conversion Methods

int myInt = 10;
double myDouble = 5.25;
bool myBool = true;

Console.WriteLine(Convert.ToString(myInt));    // convert int to string
Console.WriteLine(Convert.ToDouble(myInt));    // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble));  // convert double to int
Console.WriteLine(Convert.ToString(myBool));   // convert bool to string

 

كود ادخال رقمين من الكيبورد وجمعهما وطباعة نتيجة الجمع:

int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(x+y);

هل كان المحتوى مفيد؟

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...