You are on page 1of 30

C# Programming

Language

Windows Application Development


C# Language Basics
• Case-sensitive
• Statement Termination(uses a semicolon (;))
• Blocks(code between pair of curly braces ({ }))

Windows Application Development


Comments
• Comments are not-executing lines, that are ignored by
the compiler.
• Two basic types of comments.
• Single-line : appear only for single line and starts with //
Eg:- //this is line comment
• Multiple-line : appear for line block and starts with /*
and ends with */
Eg:- /* this is
multiple-line comment */

Windows Application Development


Data types
Built-in value types

Windows Application Development


Declare and initialize variables
Syntax
datatype variableName ; // declaration
variableName = value; //initialization
OR
datatype variableName = value;
• Start the names of variables with a lowercase letter and
capitalize the first letter of each word after the first word
Eg:- int x;
int y = 10;
decimal taxPay=1000.25m;
char letter =‘c’;

Windows Application Development


Declare and Initialize a Constant contd.
Syntax
cont datatype constantName = value;
• Must initialize the value upon declaration statement
and after initialization constant value can not be
change any where with in the program
• Capitalize the first letter of each word of a constant
name (Pascal notation)
Eg:- const int X=10
const decimal TaxPay=1000.75m;

Windows Application Development


Variable Scope
• The scope of a variable determines what code has
access to it
• The lifetime of a variable is the period of time that it's
available for use
• if you declare a variable within a method, it has
method scope
• if you declare a variable within a class but not within a
method, it has class scope
• Access Modifiers – public, private , protected and
internal
Windows Application Development
Variable Scope Contd.

Windows Application Development


Arithmetic Operators

Windows Application Development


Arithmetic Operators Contd.
• Binary operators (+, -, *, /, %) operates on two
operands
• Unary operators (negative(-), positive(+), ++, -- )
operate on just one operand
Eg:- int x = 10;
int y = 5;
int result1 = x + y ;
int result2 = -y + x;
int result3 = --x;
int result4 = ++x;

char let1 = 'A';


char let2 = ++let1;

Windows Application Development


Assignment Operators

Windows Application Development


Order of Precedence for Arithmetic
Operators
1. Increment and decrement
2. Positive and negative
3. Multiplication, division and modules
4. Addition and subtraction
Eg:- shortcut arithmetic operators Eg:- order of precedence for
int x = 100; arithmetic operators

expanded way shortcut way int x = 10;


int y = 5;
x = x + 10; x += 10;
x = x - 10; x -= 10; int result1 = x * 1 - y; // result1= ?
x = x * 10; x *= 10; int result2 = x * (1 - y); // result2= ?

Windows Application Development


Numeric Types Casting
• Convert data from one data type to another type
• Implicit cast – Performed automatically (less precise
type to more precise type)
byteshortintlongdecemal
byteshortintdouble
byteshortfloatdouble
byteshort

Eg:-
int x = 75;
double y = x;

Windows Application Development


Numeric Types Casting
• Explicit cast – Performed purposely (more
precise type to less precise type)
Syntax
(type) expression ;
Eg:-
double a = 99.5;
int b = (int)a;

char letA = (char)65;

Windows Application Development


Use Methods to Convert Data Type

Windows Application Development


Use Methods to Convert Data Type Contd.

Eg:- Convertions

decimal amount = 999.99m;


//decimal--> string conversion
string convertedValue = amount.ToString();
//string--> decimal conversion
amount = Decimal.Parse(convertedValue);

//automatic ToString() call


double price = 99.99;
string priceString = "Price: $"+ price;

//string to decimal and string to int


decimal totalPrice = Convert.ToDecimal(txtTotPrice.Text);
int years = Convert.ToInt32(txtYears.Text);

Windows Application Development


Work with Strings
• A string can consist of any letters, numbers
and characters
• Assign string value with in double quotes
• Concatenation(joining strings. use + operator
to join strings)
• The += operator is a shortcut for appending a
string expression to a string variable

Windows Application Development


Work with Strings Contd.
Eg : - declare and initialize a string

string value1 ="abc123*";


string value2 = "";//string value2 = null; string value2 = string.empty;

join strings

string val1 = "Hello";


string val2 = "World";
int year =2013;
string newVal1 = val1+" "+val2;
string newVal2 = val1+" "+val2 + " " +year;

newVal1 += " " + year;

Windows Application Development


Escape Sequences
• Include some special characters with in a string by
using escape sequences(specially non-printable
characters)
• Escape sequences always begin with black slash (\)

Eg:-
string value1= "Hello\nWorld";
string path = "c:\\Files";

Windows Application Development


Use Methods to Convert Numbers to
Formatted Strings

Windows Application Development


Use Methods to Convert Numbers to
Formatted Strings Contd.

Windows Application Development


Declare and Use Enumerations
• An enumeration defines a set of related constants. Each constant is
known as a member of the enumeration
• By default, an enumeration uses the int type and set the first
constant to 0, the second to 1, and so on

Syntax for declaring and enumeration


enum EnumerationName : type
{
ConstantName1= value,
ConstantName2= value,
.......
}

Windows Application Development


Declare and Use Enumerations Contd.
Eg:-

Windows Application Development


Arrays
• Arrays allow you to store a series of values that have the same
data type
• All arrays start at a fixed lower bound of 0
• Last location of the arrays called upper bound index
• If you are not initializing values to the arrays always by default
values will assign
Type Default Value
Numeric 0
bool false
string null
char ‘\0’ - null

Windows Application Development


Declare and Use Arrays
Syntax – single-dimensional
type[] arrayName = new type[arraySize];
Or
type[] arrayName ={ v1, v2, v3 ….};
Syntax – two-dimensional
type[,] arrayName = new type[r_size,c_size];
Eg:- int[] num =new int[5]; //1D array
int[] num1 ={10,20,30,40,50};

int[,] twoD =new[2,4]; //2D array

Windows Application Development


Declare and Use Arrays Contd.
10 20 30 40 50

Eg:- num[0] = 10; // assigning value


num[1] = 20;
twoD[0,0] = 10; //assigning value into 2D array
twoD[0,1] = 20;

int number = num[0]; //accessing an array element value


int number1 = num[0,1]; //accessing 2D array element value

Windows Application Development


Additional Topics
• Math class
• Array List
• String manipulation

Windows Application Development


Notes

Windows Application Development


Exercise -1
Design and code the Invoice Total application

Windows Application Development


Exercise -2
• Design and code the enhanced Invoice Total
application

Windows Application Development

You might also like