0% found this document useful (0 votes)
6 views5 pages

Basic

The document provides an overview of variables and data types in C#, including value and reference types, constants, and read-only fields. It explains type casting, operators (arithmetic, relational, logical), and control flow statements such as if, switch, and loops (for, while, do-while). Examples are given for each concept to illustrate their usage in programming.

Uploaded by

Syed Nibras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Basic

The document provides an overview of variables and data types in C#, including value and reference types, constants, and read-only fields. It explains type casting, operators (arithmetic, relational, logical), and control flow statements such as if, switch, and loops (for, while, do-while). Examples are given for each concept to illustrate their usage in programming.

Uploaded by

Syed Nibras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Variables and Data Types

In C#, variables store data and are associated with a specific type. They can be:

 Value Types: Store actual data (e.g., int, double, bool, char, struct).
 Reference Types: Store references to memory locations (e.g., string, class, object,
array).

Declaration Example:

int age = 30; // Integer

double salary = 50000.50; // Floating-point number

char grade = 'A'; // Character

bool isActive = true; // Boolean

string name = "John Doe"; // String

2. Constants and Read-only Fields

 Constants (const): Values that never change during the program's execution. Must be
initialized at the time of declaration.

const double Pi = 3.14159;

Read-only Fields (readonly): Values assigned either at the time of declaration or in the constructor but
cannot be changed afterward.

class Example {

readonly int readOnlyValue;

public Example() {

readOnlyValue = 100; // Assigned in the constructor

}
3. Type Casting and Conversion

 Implicit Casting: Automatic conversion when no data is lost.

int num = 10;

double result = num; // Implicitly cast to double

double value = 9.8;

int intValue = (int)value; // Truncates to 9

string str = "123";

int number = Convert.ToInt32(str);

4. Operators

Arithmetic Operators

Perform mathematical operations:

int sum = 5 + 3; // Addition

int diff = 5 - 3; // Subtraction

int prod = 5 * 3; // Multiplication

int quot = 5 / 3; // Division

int mod = 5 % 3; // Modulus

Relational (Comparison) Operators

Used for comparisons:

bool isEqual = (5 == 3); // false

bool isNotEqual = (5 != 3); // true


bool isGreater = (5 > 3); // true

bool isLess = (5 < 3); // false

Logical Operators

Used for combining Boolean expressions:

bool result = (5 > 3) && (3 > 1); // Logical AND

bool result2 = (5 > 3) || (3 < 1); // Logical OR

bool result3 = !(5 > 3); // Logical NOT

5. Control Flow Statements

if Statement

if (age > 18) {

Console.WriteLine("You are an adult.");

} else {

Console.WriteLine("You are not an adult.");

switch Statement

int day = 3;

switch (day) {

case 1:

Console.WriteLine("Monday");

break;

case 2:

Console.WriteLine("Tuesday");
break;

case 3:

Console.WriteLine("Wednesday");

break;

default:

Console.WriteLine("Other day");

break;

Loops

 For Loop:

for (int i = 0; i < 5; i++) {

Console.WriteLine("Iteration " + i);

While Loop:

int counter = 0;

while (counter < 5) {

Console.WriteLine("Counter: " + counter);

counter++;

Do-While Loop:

int count = 0;
do {

Console.WriteLine("Count: " + count);

count++;

} while (count < 5);

You might also like