C# Sharp Learning Notes August 2023 in Urdu
Shortcuts of Visual Studio C#
ctrl+alt then select for comment.MULTICURSER FUNCTIONALITY
ctrl k aor c comment ky lia
ctrl + D —line replicate duplicate. \\ not working
alt daba kar line oper nechy
C# First Code
Console.WriteLine(“Hello G”);
Console.Write(“Love for All Hatred for None. “);
Console.WriteLine(“Pakistan Zindabad”);
Console.ReadLine();
Namespace, Class Program
Using System;
mean System namespace ke classes use karain gy. like console.writeline(); etc.
namespace Hello
{}
mean namespace aik container hy jis main yah program likh kar save kar rahy hain. is ky ander classes aor dosray namspace.
name space sy code better organize hota hy/
class program
data aor methods ka aik container hy. jis main data members of function hoty hain. jo ap ky program ko jan deta hy. function bnaya 2 number ko add kar ky deta hy.
maslan jo
static void Main(string{} args)
yah aik function hy jo bana banaya mila.
main function sy c# ka function start hota hy.
console.writeline(hello);
yah aik instruction hy.
console.write(); ka matlab hy aik line likho aor next line main na jao.
console.readline(); mery lia ruko input ky lia.
Comment Type
comment system in c#
single
// This is a comment.
long
/*
paragraph
*/
Types of Variable
variable. apky container jo valus save karty hain.
maslan variable balte aor value balte ky ander pane. DataTypes
int harry = 34;
console.writeLine(“My age is” + harry);
Console.ReadLine();
ab apko variable ke qismain btata ho.
integer – int adeel = 20; // can be + – and 0 // 4 bytes
Long – Long harry = 2121122342; // int ke tarha number ly sakta hy lakin long. inko semicoluns main dalny ke zarorat nahe. aor +- 0. int sy bra.
floating point number – float that = …… ; // 4 bytes
Double- double harry = 2.12 //15 decimal tak deta hy. float sy acha
character – char a = ‘A’; //takes 2 bytes(A) single coline main likhty hain
Boolean – bool isGreat = true; // takes 1 bit.. 1 byte = 8 bit ..true ya false hota hy. sach hy to true warna false. banda sacha hy ya jotha hy.
String – string inp = “adeel”; //2 bytes per character
string inp = console.readline();
Console.WriteLine(inp);
Console.ReadLine();
// is ka matlab hy console.readline(); input ly ga aor wo inp container main dal dy ga. kio ky inp = console.readline(); aor console.readline(); data input leta hy.
varible ban jaya aor use na ho to us ky nechy green line ay jate hy,
Variable / Data Types
W3 School Examples:
int myNum = 5; // Integer (whole number)
double myDoubleNum = 5.99D; // Floating point number
char myLetter = ‘D’; // Character
bool myBool = true; // Boolean
string myText = “Hello”; // String
Data Type Size Description
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single quotes
string 2 bytes per character Stores a sequence of characters, surrounded by double quotes
int x = 5 , y = 6, z = 50;
Console.WriteLine(x + y + z);
What is Type casting?
type casting. aik data type ko dosre data type main convert karna.
2 tarha ke hote hy
1. implicit casting. smaller to larger automatic khud he ho jate hy with this patteren char > int > long > float > double
int a = 4;
double y = a;
int b = ‘y’; //yah nahe ho sakte only ya ‘y’ colins ky sath ascii value dy ga. 121 ya aski valu y ko alot hy aor binary code ko. jo 01111001 hy.
—-
2. explicit casting. manual karne parte hy larger to smaller.
double x = 10.10;
int y = (int) x;
=—–
W3School
Implicit Casting (automatically) – converting a smaller type to a larger type size
char -> int -> long -> float -> double
Explicit Casting (manually) – converting a larger type to a smaller size type
double -> float -> long -> int -> char
—–EXAMPLES
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9
Another way of Type Casting
Bool convert nahe hota pahle dono types main lakin 3rd type main bool string main convert ho jata hy.
another way to convert VERIBLE to another.
IS MAIN methods ko use kar ky kisi main be convert kar sakty hain.
—
W3 SCHOOL
Type Conversion Methods
It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):
—
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
——-
String
Lets study string varible… string ka matlab text main input lena hota hy.
Console.WriteLine(Your Name please?);
String nam = Console.ReadLine();
Console.WriteLine(“Wellcome” + nam) // + dono ke akatha kar raha hy. concadinate.
Console.WriteLine(How many candies do you want?);
string candies = Console.ReadLine();
// number iput lay rahy hain string main to data type text to + jama nahe hote. is lia string text number ko int data type variable main convert karna ho ga.
//hary kahta hy string varible ky use sy agy sab string ban jata hy…. pahla operent agar string hy to sab string.
// phir chote bracets dalain. priority mil jate hy. is function ko pahly chalao
Console.WriteLine(“4 more candies as bonus” + candies + 4);
Console.WriteLine(“4 more candies as bonus” + convert.int32(candies) + 4);
// phir be masla hal nahe howa.
Console.WriteLine(“4 more candies as bonus” + (convert.int32(candies) + 4));
candies ko convert.int32(candies)
String ky bohat sy methods hoty hain chand yah hain.
string ky char ko acces karna
string hello = “Hello World”
Console.WriteLine(hello[0]) //pahly character ko acces .
Console.WriteLine(hello.Length)
Console.WriteLine(hello.ToUpper)
Console.WriteLine(hello.ToLower)
kis number ya word start ho raha hy.
Console.WriteLine(hello.IndexOf(“Hello”))
jo number likhain gy us index sy line start ho jaya ge/
Console.WriteLine(hello.Substring(1));
concatenation
– Console.WriteLine(a + b):
– Console.WriteLine Concat(hello, a, b):
– string interpolation
-string 1a = “name”
string 2a = “Fname”
Console.WriteLine($”your name is {1a}. Your father name is {2a}. );
string
escape sequence characters.
string ky ander
\* sy ap “” add kar sakty hain
\n sy new line add kar sakty hain.
\t tab space add karny kay lia.
Operators and Operant
arithemetic -+/*
asignment operators += -= *= /=
logical and or && || !
comaparison operators > < >= <=
- Arithmetic Operators are used to perform mathematical calculations.
- Assignment Operators are used to assign a value to a property or variable. Assignment Operators can be numeric, date, system, time, or text.
- Comparison Operators are used to perform comparisons.
- Concatenation Operators are used to combine strings.
- Logical Operators are used to perform logical operations and include AND, OR, or NOT.
- Boolean Operators include AND, OR, XOR, or NOT and can have one of two values, true or false.
+ – etc operators hain. operents jo ander code hy.
operators types
Exercise:
Use the correct operator to increase the value of the variable x by 1.
int x = 10;
x
++
;
W3Schools
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations:
Operator | Name | Description | Example | Try it |
---|---|---|---|---|
+ | Addition | Adds together two values | x + y | Try it » |
– | Subtraction | Subtracts one value from another | x – y | Try it » |
* | Multiplication | Multiplies two values | x * y | Try it » |
/ | Division | Divides one value by another | x / y | Try it » |
% | Modulus | Returns the division remainder | x % y | Try it » |
++ | Increment | Increases the value of a variable by 1 | x++ | Try it » |
— | Decrement | Decreases the value of a variable by 1 | x– |
Assignment Operators in C# sharp
W3 Schools
A list of all assignment operators: yah value store karaty hain variables main.
Comparison Operators in C# sharp
Logical Operators
&& Logical and Returns True if both statements are true x < 5 && x < 10
|| Logical or Returns True if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns False if the result is true !(x < 5 && x < 10)
If Else in c# sharp
decisions kaisy lia jaty hain.
if else.
{
Console.WriteLine(“Enter age”);
string agestrng = Console.ReadLine();
int age = Convert.ToInt16(agestrng);
if (age <5)
{
Console.WriteLine(“not born yet”); //phaly yah condition check ho ge.
}
else if (age <17)
{
Console.WriteLine(“Complete studies”); //phir yah condition check ho ge.
}
else if (age >= 18 && age <50)
{
Console.WriteLine(“can take test.”); //phir yah condition check ho ge.
}
else
{
Console.WriteLine(“no”);
}
Console.ReadLine();
}
Switch
int age = 19;
switch (age)
{
case 18:
Console.WriteLine(“no”);
break;
case 19:
Console.WriteLine(“1 more”);
break;
default:
Console.WriteLine(“yes”);
break;
}
Console.ReadLine();
Loops
while loop
int num = 0;
while (num < 10 )
{
Console.WriteLine(num+1);
num++;
}
Console.ReadLine();
//1 sy 10 print karana hy to bare command likhain gy lakain while loop sy condidtion laga kar asane sy by shak lakho mian ginte print karwa lain.
do while
int num = 0;
do
{
Console.WriteLine(num+1);
num++;
}
while (num > 10);
Console.ReadLine(); //kam sy sy kam 1 bar zaror chaly ga like try ball…
For Loop
for(int num = 0; num<5; num++)
{
Console.WriteLine(num+1);
// num++; lagain gy agar juft ya taak number lenay ho gy.
}
w3school
// Outer loop
for (int i = 1; i <= 2; ++i)
{
Console.WriteLine(“Outer: ” + i); // Executes 2 times
//Inner loop
for (int j = 1; j <= 3; j++)
{
Console.WriteLine(” Inner: ” + j); // Executes 6 times (2 * 3)
Break and Continue
Break:
for (int a = 0; a < 7; a++)
{
Console.WriteLine(a+1);
break; // oper waly loop kar aik bar karo aor phir bass mazeed ke zarorat nahe.
}
Console.ReadLine();
Continue
for (int abc = 0; abc<7; abc++)
{
if (abc == 4)
{
continue;
}
Console.WriteLine(abc);
}
Console.ReadLine();
//continue mean leave this particular iteration of the loop
C# Arrays w3school
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars[0]);
// Outputs Volvo
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Change an Array Element
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
// Now outputs Opel instead of Volvo
Array Length
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars.Length);
// Outputs 4
Other Ways to Create an Array
// Create an array of four elements, and add values later string[] cars = new string[4]; // Create an array of four elements and add values right away string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"}; // Create an array of four elements without specifying the size string[] cars = new string[] {"Volvo", "BMW", "Ford", "Mazda"}; // Create an array of four elements, omitting the new keyword,
and without specifying the size string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; // last one\is fast and easy to read.