C# Lec-1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

C# Programming

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 the namespace declaration. A namespace is a collection of


classes. The FirstProgram namespace contains the class HelloWorld.

 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");

 WriteLine is a method of the Console class defined in the System


namespace. This statement causes the message "Hello, World!" to be
displayed on the screen

 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

int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0


long 64-bit signed integer type -9,223,372,036,854,775,808 to 0L
9,223,372,036,854,775,807
sbyte 8-bit signed integer type -128 to 127 0
short 16-bit signed integer type -32,768 to 32,767 0
uint 32-bit unsigned integer type 0 to 4,294,967,295 0
ulong 64-bit unsigned integer type 0 to 18,446,744,073,709,551,615 0
ushort 16-bit unsigned integer type 0 to 65,535 0
Variables

 A variable is nothing but a name given to a storage area


that our programs can manipulate.

 Each variable in C# has a specific type, which determines


the size and layout of the variable's memory the range of
values that can be stored within that memory

 The set of operations that can be applied to the variable.


Variables

 A variable is nothing but a name given to a storage area


that our programs can manipulate.

 Each variable in C# has a specific type, which determines


the size and layout of the variable's memory the range of
values that can be stored within that memory

 The set of operations that can be applied to the variable.


Variables
Type Example
Integral types sbyte, byte, short, ushort, int, uint, long, ulong, and char

Floating point types float and double

Decimal types decimal

Boolean types true or false values, as assigned


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

 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;

Some examples are:

int d = 3, f = 5; /* initializing d and f. */


byte z = 22; /* initializes z. */
double pi = 3.14159; /* declares an approximation of pi. */
char x = 'x'; /* the variable x has the value 'x'. */
Operators
 An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.

 Arithmetic Operators
 Relational Operators
 Logical Operators
Arithmetic Operators
Operator Description Example A = 10 , B = 20

+ Adds two operands A + B = 30


- Subtracts second operand from the first A - B = -10

* Multiplies both operands A * B = 200


/ Divides numerator by de-numerator B/A=2

% Modulus Operator and remainder of after an integer B % A = 0


division

++ Increment operator increases integer value by one A++ = 11

-- Decrement operator decreases integer value by one A-- = 9


Relational 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 OR Operator. If any of the two operands (A || B) is true.


is 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)

/* statement(s) will execute if the boolean expression is true */

else

/* statement(s) will execute if the boolean expression is false */

}
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

You might also like