C# Lec-1
C# Lec-1
C# Lec-1
First C# Program
using System;
namespace FirstProgram
{
class HelloWorld
{
static void Main(string[] args)
{
/* Student ID = abc123
my first program in C#
*/
Console.WriteLine("Hello World");
Console.ReadKey();
//End Program
}
}
}
Constructions of Note
The first line of the program using System; the using keyword is used to
include the System namespace in the program. A program generally has
multiple using statements.
The next line has a class declaration, the class HelloWorld contains the data
and method definitions that your program uses. Classes generally contain
multiple methods. Methods/Functions define the behavior of the class. However,
the HelloWorld class has only one method Main.
Constructions of Note
The next line defines the Main method, which is the entry point for all C#
programs. The Main method states what the class does when executed.
Comments are used for explaining code. Compilers ignore the comment
entries. The multiline comments in C# programs start with /* and terminates with
the characters */ as shown below:
/* Student ID = abc123
my first program in C# */
Single-line comments are indicated by the '//' symbol. For example,
// End Program
Constructions of Note
The Main method specifies its behavior with the
statement Console.WriteLine("Hello World");
The last line Console.ReadKey(); is for the VS.NET Users. This makes the
program wait for a key press and it prevents the screen from running and
closing quickly when the program is launched from Visual Studio .NET.
Important points
C# is case sensitive.
All statements and expression must end with a semicolon (;).
The program execution starts at the Main method.
Data Types
The variables in C#, are categorized into the following types:
Value types
Reference types
Pointer types
Value Type
Value type variables can be assigned a value directly.
The value types directly contain data. Some examples are int, char, and float,
which stores numbers, alphabets, and floating point numbers, respectively.
When you declare an int type, the system allocates memory to store the value.
Type Represents Range Default
Value
bool Boolean value True or False False
byte 8-bit unsigned integer 0 to 255 0
char 16-bit Unicode character U +0000 to U +ffff '\0'
28 28 0 to 28
decimal 128-bit precise decimal values with 28-29 (-7.9 x 10 to 7.9 x 10 ) / 10 0.0M
significant digits
-324 308
double 64-bit double-precision floating point type (+/-)5.0 x 10 to (+/-)1.7 x 10 0.0D
38 38
float 32-bit single-precision floating point type -3.4 x 10 to + 3.4 x 10 0.0F
Defining Variables
Syntax for variable definition in C# is:
<data_type> <variable_list>;
Some valid variable definitions are shown here:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variables
Defining Variables
Syntax for variable definition in C# is:
<data_type> <variable_list>;
Some valid variable definitions are shown here:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variables
Initializing Variables
Variables are initialized (assigned a value) with an equal sign followed by a constant
expression. The general form of initialization is:
<data_type> <variable_name> = value;
Arithmetic Operators
Relational Operators
Logical Operators
Arithmetic Operators
Operator Description Example A = 10 , B = 20
== Checks if the values of two operands are equal or not, if yes (A == B) is not true.
then condition becomes true.
!= Checks if the values of two operands are equal or not, if values (A != B) is true.
are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of (A > B) is not true.
right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right (A < B) is true.
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the (A >= B) is not true.
value of right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
Logical Operators
Operator Description Example A= true B=false
&& Called Logical AND operator. If both the operands are (A && B) is false.
non zero then condition becomes true.
! Called Logical NOT Operator. Use to reverses the !(A && B) is true.
logical state of its operand. If a condition is true then
Logical NOT operator will make false.
Decision Making
Decision making statements help you to make decision based on certain
conditions. These conditions are specified by a set of decision making
statements having Boolean expressions which are evaluated to a Boolean
value true or false.
If statement
If-Else statement
If-Else-If statement
Switch statement
if Statement
An if statement consists of a boolean expression followed by one or
more statements
Syntax
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
if Statement
using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if (a < 20)
{
/* if condition is true then print the following */
Console.WriteLine("a is less than 20");
}
Console.WriteLine("value of a is : {0}", a);
Console.ReadLine();
}
}
}
If-Else statement
An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
Syntax
if (boolean_expression)
else
}
If-Else-If statement or ladder
The If-Else-If or ladder is a set of statements that is used to test a series of
conditions.
Switch statement
Switch statement acts as a substitute for long If-Else-If ladder that is used to test
a series of conditions. A switch statement contains one or more case labels
which are tested against the switch expression..
Loops
A loop statement allows us to execute a statement or a group of
statements multiple times and following is the general from of a loop
statement in most of the programming languages
for loop
while loop
do….while loop