0% found this document useful (0 votes)
5 views

Unit 1 Data Types PDF

The document provides an overview of data types in C#, including built-in types like string, int, float, char, and bool, along with examples of literals and variables. It explains operators, control statements, loops, jump statements, classes, objects, arrays, and the concepts of boxing and unboxing. Each section includes code snippets to illustrate the concepts discussed.

Uploaded by

user-724064
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 1 Data Types PDF

The document provides an overview of data types in C#, including built-in types like string, int, float, char, and bool, along with examples of literals and variables. It explains operators, control statements, loops, jump statements, classes, objects, arrays, and the concepts of boxing and unboxing. Each section includes code snippets to illustrate the concepts discussed.

Uploaded by

user-724064
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Types

A data type specifies the type of value that a variable can store in C#. C# includes many built-in
data types such as:

 string – stores text

 int – stores whole numbers

 float – stores decimal numbers

 char – stores single characters

 bool – stores true or false values

Example:

using System;

public class Program

public sta c void Main()

string stringVar = "Hello World";

int intVar = 100;

float floatVar = 10.2f;

char charVar = 'A';

bool boolVar = true;

Console.WriteLine(stringVar);

Console.WriteLine(intVar);

Console.WriteLine(floatVar);

Console.WriteLine(charVar);

Console.WriteLine(boolVar);

Literals and Variables


 Literal: A fixed value wri en directly in the source code.

o Example: 100 in int x = 100; (100 is a literal).

 Variable: A named storage loca on that holds data.


Types of Literals in C#:

1. Boolean Literal (true, false)

2. Integer Literal (10, 200, etc.)

3. Real Literal (3.14, 10.5, etc.)

4. Character Literal ('A', 'B', etc.)

5. String Literal ("Hello", "World", etc.)

6. Null Literal (null)

Operators
Operators are symbols used to perform opera ons in C#.
Types of operators:

 Arithme c Operators (+, -, *, /, %)

 Rela onal Operators (>, <, >=, <=, ==, !=)

 Logical Operators (&&, ||, !)

 Bitwise Operators (&, |, ^, <<, >>)

 Assignment Operators (=, +=, -=, *=, /=, %=)

 Miscellaneous Operators (sizeof, typeof, ?:, is, as)

Program Control Statements


Control statements allow decision-making and looping.

If-Then-Else

Example:

int i = 10;

if (i > 0)

Console.WriteLine("Posi ve Number");

else

Console.WriteLine("Nega ve Number");
Switch Statement

Example:

string day = "Monday";

switch (day)

case "Monday":

Console.WriteLine("Go to work");

break;

default:

Console.WriteLine("Default Case");

break;

Loops

While Loop:

int i = 123, count = 0;

while (i > 0)

count++;

i = i / 10;

Console.WriteLine("Number of digits: " + count);

Do-While Loop:

int i = 0, count = 0;

do

count++;

i = i / 10;

} while (i > 0);

Console.WriteLine("Number of digits: " + count);


For Loop:

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

Console.WriteLine("Itera on " + i);

Foreach Loop:

string[] names = { "John", "Doe", "Jane" };

foreach (string name in names)

Console.WriteLine(name);

Jump Statements
Goto Statement:

label1:

Console.WriteLine("Jump to label1");

goto label1;

Break Statement:

for (int i = 0; i < 10; i++)

if (i == 5)

break;

Console.WriteLine(i);

Con nue Statement:

for (int i = 0; i < 10; i++)

if (i == 5)

con nue;

Console.WriteLine(i);

}
Return Statement:

void MyFunc1()

if (x == 1)

return;

Classes and Objects


 Class: A blueprint for crea ng objects.

 Object: An instance of a class.

Example:

using System;

public class Student

int id;

string name;

public sta c void Main(string[] args)

Student s1 = new Student();

s1.id = 101;

s1.name = "Suman Jaiswal";

Console.WriteLine(s1.id);

Console.WriteLine(s1.name);

Arrays and Strings


 Array: Stores mul ple values of the same type.
Integer Array:

int[] array = new int[4] {10, 20, 30, 40};

Console.WriteLine(array[0]); // Output: 10

String Array:

string[] week = { "Sunday", "Monday", "Tuesday" };

Console.WriteLine(week[1]); // Output: Monday

Resize an Array:

int[] arr = new int[5] { 1, 2, 3, 4, 5 };

Array.Resize(ref arr, 3);

ForEach with Arrays:

int[] numbers = { 10, 30, 50 };

foreach (int num in numbers)

Console.WriteLine(num);

Check if an Array Contains a Value:

string[] week = { "Sunday", "Monday", "Tuesday" };

string value = "Tuesday";

if (Array.IndexOf(week, value) > -1)

Console.WriteLine(value + " exists!");

else

Console.WriteLine(value + " does not exist!");

Boxing and Unboxing


 Boxing: Conver ng a value type (like int) to an object.

 Unboxing: Conver ng an object back to a value type.

Example:

int x = 123;

object obj = x; // Boxing

int y = (int)obj; // Unboxing

You might also like