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

C#

Uploaded by

janakikunal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C#

Uploaded by

janakikunal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

C#

What is .NET Framework?


 Software Development Platform
 Developed by Microsoft for building and running windows applications.
 It consists of developer tools, programming languages, and libraries
What is C#?
 C# is a programming language of .Net Framework.
 It is an object-oriented programming language
C++ vs C#
 Lightweight language
 C++ is a lightweight language as compared to C# language as the libraries of C# language
need to be included before compilation due to which size of binaries in C# language is
more than C++ language.
 Performance
 C++ code runs faster than the C# code and makes a better solution for those applications
that require higher performance.
 Garbage Collection
 C# provides the automatic garbage collection while C++ does not provide the automatic
garbage collection, i.e., the objects are allocated or deallocated manually.
 Platform dependency
 C# language is a standardized language, so it works only on Windows operating system
while C++ supports all the platforms such as Windows, Unix, Linux, Mac, etc.
 Types of projects
 C++ language mainly works on those applications that communicate directly with the
hardware while C# language is mainly used for mobile, web, desktop or gaming
applications.
C++ vs C#
 Compiler warnings
 C++ allows you to do everything if the syntax is correct, but sometimes cause real
damage to the operating system. C# language is a much-protected language as compiler
gives errors and warnings without allowing you to create serious damage.
 Compilation
 C++ code is compiled to machine code while C# code compiles to CLR(Common
Language Runtime) which is interpreted by the JIT(Just In Time) compiler.
 Multiple Inheritance
 C++ language supports multiple inheritances, while C# language does not support the
multiple inheritances.
 Level of Difficulty
 C++ language contains more complex features than C# language while C# language is a
simple hierarchy which is quite easy to understand.
 Default access specifier
 In C++, the default access specifier is public while in C#, the default access specifier is
private.
C++ vs C#
 Object Oriented
 C++ language is not a complete object-oriented language while C# language is a pure
object-oriented programming language.
 Bound checking
 C++ language does not support the bound checking for arrays while C# language
supports the bound checking for arrays.
 For each loop
 C++ language does not support the for each loop while C# language supports the for
each loop.
 Use of pointers
 In C++, we can use the pointers anywhere in the program while in C# language, pointers
are used in the unsafe area.
 Switch statement
 In C++, string variable cannot be passed in the switch statement, but in C# language,
string variable can be passed in the switch statement.
C++ vs C#
 Standalone applications
 C++ language can be used to develop standalone applications, but C# language cannot
be used to develop standalone applications.
Hello World!
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
class: is a keyword which is used to define class.

Program: is the class name. A class is a blueprint or template from which objects are created. It can have data
members and methods. Here, it has only Main method.

static: is a keyword which means object is not required to access static members. So it saves memory.

void: is the return type of the method. It doesn't return any value. In such case, return statement is not required.

Main: is the method name. It is the entry point for any C# program. Whenever we run the C# program, Main()
method is invoked first before any other method. It represents start up of the program.

string[] args: is used for command line arguments in C#. While running the C# program, we can pass values. These
values are known as arguments which we can use in the program.

System.Console.WriteLine("Hello World!"): Here, System is the namespace. Console is the class defined in System
namespace. The WriteLine() is the static method of Console class which is used to write the text on the console.
Hello World! = Using System
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

If we write using System before the class, it means we don't need to specify System
namespace for accessing any class of this namespace. Here, we are using Console class
without specifying System.Console.
Hello World! = Using Public modifier
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

We can also specify public modifier before class and Main() method. Now, it can be
accessed from outside the class also.
Hello World! = Using Namespace
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

We can create classes inside the namespace. It is used to group related classes. It is
used to categorize classes so that it can be easy to maintain.
Hello World!

class Program using System;


{ class Program
static void Main(string[] args) {
{ static void Main(string[] args)
System.Console.WriteLine("Hello World!"); {
} Console.WriteLine("Hello World!");
} }
}

using System; using System;


public class Program namespace ConsoleApplication1
{ {
public static void Main(string[] args) public class Program
{ {
Console.WriteLine("Hello World!"); public static void Main(string[] args)
} {
} Console.WriteLine("Hello World!");
}
}
}
Print Operation

 To print the data on the console output screen the following method are used,
 Console.Write()
 Console.WriteLine()
 Console is a predefined class of System namespace.
 While Write() and WriteLine() both are the Console Class methods.
 The only difference between the Write() and WriteLine() is that
 Console.Write is used to print data without printing the new line,
 while Console.WriteLine is used to print data along with printing the new line.

Example Code : Output :

string s1 = "Kunal"; KunalSatishBhaiThaker


string s1_1 = "SatishBhai";
string s2 = "Thaker";

Console.Write(s1);
Console.Write(s1_1);
Console.Write(s2);
Print Operation - Console.Write & Console.WriteLine
Example Code for Write(): Output :

string s1 = "Kunal"; KunalSatishBhaiThaker


string s1_1 = "SatishBhai";
string s2 = "Thaker";

Console.Write(s1);
Console.Write(s1_1);
Console.Write(s2);

Example Code for WriteLine(): Output :

string s1 = "Kunal"; Kunal


string s1_1 = "SatishBhai"; SatishBhai
string s2 = "Thaker"; Thaker

Console.WriteLine(s1);
Console.WriteLine(s1_1);
Console.WriteLine(s2);
Print Operation – Passing Values in Console.WriteLine()

Example Code for WriteLine():

string s1 = "Kunal";
string s1_1 = "SatishBhai";
string s2 = "Thaker";

Console.WriteLine("String S1 : {0} \n"


+ "String S1_1 : {1} \n"
+ "String S2 : {2}",
s1, s1_1, s2);

Output :

String S1 : Kunal
String S1_1 : SatishBhai
String S2 : Thaker
Console.WriteLine(string format, object arg)
Code Output
Console.WriteLine("Currency is : {0:C}",123); Currency is : $123.00

Console.WriteLine("Currency is : {0:C}",-123); Currency is : ($123.00)

Console.WriteLine("Decimal Number is : {0:D}",123); Decimal Number is : 123

Console.WriteLine("Decimal Number is : {0:D}",-123); Decimal Number is : -123

Console.WriteLine("Scientific is : {0:E}",1); Scientific is : 1.000000E+000

Console.WriteLine("Fixed Point is : {0:F}",1); Fixed Point is : 1.00

Console.WriteLine("Fixed Point is : {0:F}",123.25689); Fixed Point is : 123.26

Console.WriteLine("Fixed Point is : {0:F}",123.21234); Fixed Point is : 123.21

Console.WriteLine("Number is : {0:N}",12.2356); Number is : 12.24

String s9 = “12345”;
Console.WriteLine("Number is : {0:N}",s9); Number is : 12345

Console.WriteLine("Percent is : {0:P}",123); Percent is : 12,300.00%

Console.WriteLine("Round Trip is : {0:R}",123.12345678901234567890); Round Trip is : 123.12345678901235


Variables

Variable Type Example


Decimal types decimal
Boolean types True or false
Integral types int, char, byte, short, long
Floating point types float and double
Nullable types Nullable data types
Data Types
Data Types How to define this in Code Memory Size Range
char char 1 byte -128 to 127
signed short short 2 byte -32,768 to 32,767
unsigned short ushort 2 byte 0 to 65,535
signed int int 4 byte -2,147,483,648 to -2,147,483,647
unsigned int uint 4 byte 0 to 4,294,967,295
signed long long 8 byte ?9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long ulong 8 byte 0 - 18,446,744,073,709,551,615
float float 4 byte 1.5 * 10-45 - 3.4 * 1038, 7-digit precision
double double 8 byte 5.0 * 10-324 - 1.7 * 10308, 15-digit precision
decimal decimal 16 byte at least -7.9 * 10?28 - 7.9 * 1028, with at least 28-digit precision
Example of Data Type
char input1 = 'a';
char input2 = '\u006A';
char input3 = '\x006A';
char input4 = '\x0030';
char input5 = '\x003D';
short input6 = -32768;
short input7 = 32767;
ushort input8 = 65535;
int input9 = -2147483648;
int input10 = 2147483647;
uint input11 = 4294967295;
long input12 = -9223372036854775808;
long input13 = 9223372036854775807;
ulong input14 = 18446744073709551615;
float input15 = 99999999999999999999999999999999999999.55555555555555555555555555555555555555555f;
double input16 = 9999999999999999999999999999999999999999999999999999999999999.55555555555555555555555555555555555555555d;
decimal input17 = 9999999999999999999;
This data is Character Type. Value is : a
This data is Character Type but Hex code is mentioned. Hex Code is 6A which is of j and Character Value is : j
This data is Character Type but Hex code is mentioned. Hex Code is 6A which is of j and Character Value is : j
This data is Character Type but Hex code is mentioned. Hex Code is 30 which is of 0 and Character Value is : 0
This data is Character Type but Hex code is mentioned. Hex Code is 30 which is of = and Character Value is : =
This data is Short Type. Value is : -32768
This data is Signed Short Type. Value is : 32767
This data is Unsigned Short Type. Value is : 65535
This data is Signed Integer Type. Value is : -2147483648
This data is Signed Integer Type. Value is : 2147483647
This data is UnSigned Integer Type. Value is : 4294967295
This data is Signed Long Integer Type. Value is : -9223372036854775808
This data is Signed Long Integer Type. Value is : 9223372036854775807
This data is UnSigned Long Integer Type. Value is : 18446744073709551615
This data is Float Type. Value is : 1E+38
This data is Double Type. Value is : 1E+61
This data is Decimal Type. Value is : 9999999999999999999
C# - Operators
 Unary Operator: Operator that takes one operand to perform the operation.
 Binary Operator: Operator that takes two operands to perform the operation.
 Ternary Operator: Operator that takes three operands to perform the operation.
C# - Unary Operators

++Variable
Increment
++
by 1
Variable++
Unary
Operators
--Variable
Decrement
--
by 1
Variable--
C# - Unary Operators
Post Increment
Example Code:

short input18 = 0;
Console.WriteLine("This is Increment Operator and it is of Unary Type Operator. Actual Input Number is : " + input18);
short input19 = input18++;
Console.WriteLine("Output of Unary Type Operator, Increment Operator is : " + input19);
Output:

This is Increment Operator and it is of Unary Type Operator. Actual Input Number is : 0
Output of Unary Type Operator, Increment Operator is : 0

Example Code: Pre Increment

short input18 = 0;
Console.WriteLine("This is Increment Operator and it is of Unary Type Operator. Actual Input Number is : " + input18);
short input19 = ++input18;
Console.WriteLine("Output of Unary Type Operator, Increment Operator is : " + input19);
Output:

This is Increment Operator and it is of Unary Type Operator. Actual Input Number is : 0
Output of Unary Type Operator, Increment Operator is : 1
C# - Unary Operators
Example Code:

short input18 = 0;
Console.WriteLine("This is Increment Operator and it is of Unary Type Operator. Actual Input Number is : " + input18);
short input19 = ++input18;
Console.WriteLine("Output of Unary Type Operator, Increment Operator is : " + input19);
Output:

This is Increment Operator and it is of Unary Type Operator. Actual Input Number is : 0
Output of Unary Type Operator, Increment Operator is : 1

Example Code:

short input20 = 0;
Console.WriteLine("This is Decrement Operator and it is of Unary Type Operator. Actual Input Number is : " + input20);
short input21 = ++input20;
Console.WriteLine("Output of Unary Type Operator, Decrement Operator is : " + input21);
Output:

This is Decrement Operator and it is of Unary Type Operator. Actual Input Number is : 0
Output of Unary Type Operator, Decrement Operator is : -1
C# - Binary Operators
 Arithmetic Operators
 Bitwise Operators
 Logical Operators
 Relational Operators
 Assignment Operators
C# - Arithmetic Operators
 Addition
 Subtraction
 Multiplication
 Division
 Modulo
C# - Binary Operators – Arithmetic Operators

Name of Symbol of Input Input


How to define in code Output
Operator Operator 1 2
Addition + 5 5 5+5 10
Subtraction - 5 5 5-5 0
Multiplication * 5 5 5*5 25
Division / 5 5 5/5 1
Modulo % 5 5 5%5 0
C# - Binary Operators – Bitwise Operators

Name of Symbol of Input Input


How to define in code Output
Operator Operator 1 2
AND & 5 5 5&5 5
OR | 5 5 5|5 5
NOT ~ 5 5 ~5 -6
XOR ^ 5 5 5^5 0
Left Shift << 5 - 5 << 1 10
Right Shift >> 10 - 10 >> 1 5
C# - Binary Operators – Logical Operators

Name of Symbol of Input Input


How to define in code Output
Operator Operator 1 2
false false false
false true false
AND && Input1 && input2
true false false
true true true
false false false
false true true
OR || Input1 || input2
true false true
true true true
true - false
NOT ! !Input1
false true
C# - Binary Operators – Relational Operators
Symbol of Input How to define in
Name of Operator Input 1 Output
Operator 2 code
5 5 input1 == input2 true
Equal to ==
6 5 input1 == input2 false
5 5 input1 != input2 false
Not Equal to !=
6 5 input1 != input2 true
5 5 input1 > input2 false
Greater Than > 5 6 input1 > input2 false
6 5 input1 > input2 true
5 5 input1 < input2 false
Less Than < 5 6 input1 < input2 true
6 5 input1 < input2 false
5 5 input1 >= input2 true
Greater Than Equal to >= 6 5 input1 >= input2 true
4 8 input1 >= input2 false
5 5 input1 <= input2 true
Less Than Equal to <= 6 5 input1 <= input2 false
4 8 input1 <= input2 true
C# - Binary Operators – Assignment Operators

Symbol of How to define in


Name of Operator Input 1 Output
Operator code
Simple Assignment = 5 Input1 = 5 Input1 = 5
Add += 5 Input1 += 5 Input1 = 10
Subtract -= 5 Input1 -= 5 Input1 = 0
Multiply *= 5 Input1 *= 5 Input1 = 25
Division /= 5 Input1 /= 5 Input1 = 1
Modulus %= 5 Input1 %= 5 Input1 = 0
Left Shift <<= 5 Input1 <<= 1 Input1 = 10
Right Shift >>= 10 Input1 >>= 1 Input1 = 5
Bitwise AND &= 5 Input1 &= 0 Input1 = 0
Bitwise Exclusive OR ^= 5 Input1 ^= 0 Input1 = 5
Bitwise Inclusive OR |= 5 Input1 |= 5 Input1 = 0
C# - Miscellaneous Operators – ?: Operators
 This the conditional operator also known as the ternary conditional operator.
 It evaluates a Boolean expression and returns the result of one of the two
expressions, depending on whether the Boolean expression evaluates to true or
false,

is this condition true ? yes : no

Example Code :

string GetWeatherDisplay(double tempInCelsius) => tempInCelsius < 25.5 ? "Cold" : "Hot";


Console.WriteLine(GetWeatherDisplay(35.5));
Console.WriteLine(GetWeatherDisplay(00.5));

Output :

Hot
Cold
C# - Miscellaneous Operators – ?: Operators
Example Code :

Random rand = new Random();


int randomNumber = rand.Next(100);
string result = randomNumber < 50 ? "Sorry! Please Try Again. Your score is : " + randomNumber :
"Hurray! You passed the exam.Your score is : " + randomNumber;
Console.WriteLine(result);

Output :

Sorry! Please Try Again. Your score is : 49

Example Code :

int price = rand.Next(2000, 3000);


float discount = price < 2500 ? 0.20f : 0.30f;
int finalPrice = price < 2500 ? (int)Math.Round(price-(price * discount)) : (int)Math.Round(price-(price *
discount));
Console.WriteLine("Your purchase amount is : " + price + " And your discount is : " + discount + " So your Final
price is : " + finalPrice);

Output :
Your purchase amount is : 2811 And your discount is : 0.3 So your Final price is : 1968
Your purchase amount is : 2134 And your discount is : 0.2 So your Final price is : 1707
C# - Miscellaneous Operators – ?: Operators
Nested – ?: Operators
Example Code :

int number1 = rand.Next(0, 100);


int number2 = rand.Next(0, 100);
string resultNested = number1 == number2 ? "Both the Numbers are equal"
: number1 < number2 ? "Number 1 is less than Number 2"
: number1 > number2 ? "Number 2 is greater then Number 1"
: "Sorry!";
Console.WriteLine("Number 1 is : " + number1);
Console.WriteLine("Number 2 is : " + number2);
Console.WriteLine(resultNested);

Outcome :

Number 1 is : 74
Number 2 is : 96
Number 1 is less than Number 2
C# - Miscellaneous Operators

?? and ??= operators - the null-coalescing operators


 The null-coalescing operator ?? returns the value of its left-hand operand if it isn't
null; otherwise, it evaluates the right-hand operand and returns its result.
 The ?? operator doesn't evaluate its right-hand operand if the left-hand operand
evaluates to non-null.
 The null-coalescing assignment operator ??= assigns the value of its right-hand
operand to its left-hand operand only if the left-hand operand evaluates to null.
 The ??= operator doesn't evaluate its right-hand operand if the left-hand operand
evaluates to non-null.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy