String Type:
1) What is string?
string is a data type used to store text, like words or sentences.
Text is written inside double quotes " ".
string greeting = "Hello, World!";
2) Common Operations:
Join Strings: Combine two strings with +:
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
// "John Doe"
46
Get String Length: Count how many characters are in a string
string text = "Hello";
int length = [Link]; // 5
Change Case: Make the text uppercase or lowercase
string upper = "hello".ToUpper(); // "HELLO"
string lower = "HELLO".ToLower(); // "hello"
Input and Output with Strings:
[Link]("Enter your name: ");
string name = [Link]();
[Link]("Welcome, " + name);
47
What are Constants?
Constants in C# are values that never change during the
execution of a program. Example:
The value of Pi (3.14) or
The Days In Week = 7.
How to Declare a Constant in C#
Syntax:
const dataType constantName = value;
Example:
const int DaysInWeek = 7; // Pascal Case
48
dataType
The type of the constant (e.g., int, double, string).
constantName
The name of the constant (usually in PascalCase by convention).
value
The fixed value assigned to the constant.
Examples \\ Calculating the Area of a Circle in C#
formula: area = Pi * radius * radius;
49
using System;
class Program
{
static void Main()
{
// Define the value of Pi as a constant
const double Pi = 3.14159;
// Ask the user for the radius
[Link]("Enter the radius of the circle: ");
double radius = [Link]([Link]());
// Calculate the area
double area = Pi * radius * radius;
// Display the result
[Link]($"The area of the circle with
radius {radius} is {area}");
}
}
50
Basics of Input/Output in C# (Input/Output Statements)
In C#, we can read data from the user (input) or display information
to them (output) using the System library.
Input Statements
Used to read data from the user while the program is running.
The [Link]() method is primarily used for this
purpose. Examples:
Reading a String:
[Link]("Enter your name: ");
string name = [Link]();
51
Reading an Integer: To convert the input to an integer:
[Link]("Enter your age: ");
int age = Convert.ToInt32([Link]());
Reading a Double: To convert the input to a decimal number:
[Link]("Enter your salary: ");
double salary = [Link]([Link]());
Input Formatting: Add clear text to explain to the user what they
need to enter.
[Link]("Enter a number: ");
// Explains to the user what input is expected
52
Output Statements
Used to display information or results on the screen.
The methods [Link]() and [Link]() are used
for this purpose.
Difference Between Write and WriteLine:
[Link](): Prints the text and stays on the same line.
[Link](): Prints the text and moves to the next line.
Examples:
Simple Output Using WriteLine:
[Link]("Welcome to C#!");
53
Output Without Moving to a New Line Using Write:
[Link]("This text is on the same line. ");
[Link]("Still on the same line.");
Output Formatting:
how data is displayed when printed to the screen.
It helps make the output clear, readable, and structured.
1) String Interpolation (Recommended): Use $ before a string to
insert variables directly into it using {}.
2) Numeric Formatting: You can format numbers to show them
as decimals, currency, or percentages.
54
Simple Program Example:
using System;
class Program
{
static void Main()
{
string product = "Laptop";
double price = 799.99;
double discount = 0.15;
[Link]($"Product: {product}");
[Link]($"Price: {price:C}");
[Link]($"Discount: {discount:P}");
[Link]($"Final Price: {(price * (1 -
discount)):C}");
}
}
55
Output (Example):
Product: Laptop
Price: $799.99
Discount: 15.00%
Final Price: $679.99
56
Operators and Operations
In C#, operators are symbols used to perform operations on
variables and values. They includes:
1) Arithmetic Operators: Used to perform basic mathematical
operations like addition, subtraction, Etc..
57
Examples of Arithmetic Operations:
static void Main()
{
int a = 10;
int b = 3;
[Link]($"Addition: {a} + {b} = {a + b}");
[Link]($"Subtraction: {a} - {b} = {a - b}");
[Link]($"Multiplication: {a} * {b} = {a * b}");
[Link]($"Division: {a} / {b} = {a / b}");
[Link]($"Modulus: {a} % {b} = {a % b}");
}
}
Output:
Addition: 10 + 3 = 13
Subtraction: 10 - 3 = 7
Multiplication: 10 * 3 = 30
Division: 10 / 3 = 3
Modulus: 10 % 3 = 1
58
2) Assignment Operators:
Used to assign values to variables, often combined with
arithmetic operations.
The most basic is = (assignment).
59
Examples of Assignment Operators:
static void Main()
{
int x = 10;
[Link]($"Initial value: x = {x}");
x += 5; // x = x + 5
[Link]($"After x += 5: x = {x}");
x -= 3; // x = x - 3
[Link]($"After x -= 3: x = {x}");
x *= 2; // x = x * 2
[Link]($"After x *= 2: x = {x}");
x /= 4; // x = x / 4
[Link]($"After x /= 4: x = {x}");
x %= 3; // x = x % 3
[Link]($"After x %= 3: x = {x}");
}
}
60
Output:
Initial value: x = 10
After x += 5: x = 15
After x -= 3: x = 12
After x *= 2: x = 24
After x /= 4: x = 6
After x %= 3: x = 0
2) Comparison (Relational) Operators:
Used to compare two values, returning true or false.
61
Examples of Comparison Operators:
static void Main() Output:
{
int a = 10; a = 10, b = 5
int b = 5;
a == b: False
[Link]($"a = {a}, b = {b}");
[Link]($"a == b: {a == b}"); a != b: True
[Link]($"a != b: {a != b}");
[Link]($"a > b: {a > b}"); a > b: True
[Link]($"a < b: {a < b}");
[Link]($"a >= b: {a >= b}"); a < b: False
[Link]($"a <= b: {a <=
b}"); a >= b: True
}
} a <= b: False
62
Used in if-else statements, loops, and other conditional logic.
static Using
Example: void ifMain()
Statement
{
int age = 20;
if (age >= 18)
{
[Link]("You are an adult.");
}
else
{
[Link]("You are not an
adult.");
}
}
}
Output:
You are an adult.
63