C#
C#
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!
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.
Console.Write(s1);
Console.Write(s1_1);
Console.Write(s2);
Print Operation - Console.Write & Console.WriteLine
Example Code for Write(): Output :
Console.Write(s1);
Console.Write(s1_1);
Console.Write(s2);
Console.WriteLine(s1);
Console.WriteLine(s1_1);
Console.WriteLine(s2);
Print Operation – Passing Values in Console.WriteLine()
string s1 = "Kunal";
string s1_1 = "SatishBhai";
string s2 = "Thaker";
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
String s9 = “12345”;
Console.WriteLine("Number is : {0:N}",s9); Number is : 12345
++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
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
Example Code :
Output :
Hot
Cold
C# - Miscellaneous Operators – ?: Operators
Example Code :
Output :
Example Code :
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 :
Outcome :
Number 1 is : 74
Number 2 is : 96
Number 1 is less than Number 2
C# - Miscellaneous Operators