Features of C#
Simple Consistent Modern Object Oriented Type Safe Versionable Compatiable Interoperable and Flexible
Total.net / Programming in C# / Session 3/ 2 of 38
Simple: Treats integer and Boolean types as 2 differently types Consistance: All Types are treated as objects and developers can extend the type system simply and easily Modern: It supports garbage collection. Rich intrinsic model for error handling Security Model
Total.net / Programming in C# / Session 3/ 3 of 38
Object Oriented
Data Abstraction Encapsulation Inheritance Polymorphism
Type Safety
All dynamically allocated objects and arrays are intialized to zero. Use of unintialized variables produces an error message by the compiler .
Versionable
Making new versions of software modules work with the existing applications is known as Versioning
C# checks for unintialized variables and gives error messages at compile time.In C++ an unintialized variable goes undetected thus resulting in unpredictable output. Arrays are classes in C# and therefore they have built in functionality for operations such as sorting and searching.
Total.net / Programming in C# / Session 3/ 9 of 38
Java does not support enumerations,indexers. C# does not allow type definitions in interfaces while java interfaces can have const type data. In Java switch statement can have only integer expression while C# supports either an integer or strings.
Total.net / Programming in C# / Session 3/ 12 of 38
Session Objectives
Explain the need for C# Discuss classes in C# Discuss flow control of a program Explain the fundamental data types in C# Discuss the concept of Boxing and UnBoxing Discuss Structures Appreciate the new features of structures in C# Discuss Enumeration types
Total.net / Programming in C# / Session 3/ 13 of 38
Introduction to C#
Takes Full Advantage of the .net Platform A modern replacement for C++. Enhances developer productivity and increases safety, by enforcing script type checking.
C# Program Flow
A simple C# program -
C# Constructs (1)
Variables in C# are declared as follows -
C# Constructs (2)
If you want to use any keyword as the variable name, you have to prefix the variable name with @.
C# Constructs (3)
Output -
Default Values
The default values of the common data types -
Escape Characters
\.. double quotes. \ single quotes. \nnewline. \t..tab Eg:Path=C:\\myfolder\\myfile.txt. Or Path=@c:\myfolder\myfile.txt.
Total.net / Programming in C# / Session 3/ 20 of 38
The highlighted line acts as a placeholder where the value of the specified variable (result) will be displayed.
Total.net / Programming in C# / Session 3/ 22 of 38
The program will accept a line from the user and echo it back as output.
Total.net / Programming in C# / Session 3/ 23 of 38
The if construct
Used for performing conditional branching. Syntax -
Selection Statement
The above piece of code will display an error message -
Syntax
Perform a certain set of instructions a certain number of times or while a specific condition is true. Types of iteration constructs -
Iteration Constructs
The break statement - to break out of the loop at anytime. The continue statement - to skip the current iteration and begin with the next iteration.
Total.net / Programming in C# / Session 3/ 29 of 38
The do loop
Syntax -
Output -
Foreach Example
This will work String[] str={one,two,three} foreach(string s in str) { System.Console.WriteLine(s= ); } This will not work int [] arr={1,2,3}; foreach(int num in arr) { Num+=1; } Here one requires to use for block with counter.
Constructors in C#
A constructor in C# has a same name as the class
Destructors in C#
A destructor in C# also has a same name as the class.
ref keyword
private void M1(ref int number) { number=number+2; } Calling function int n=10; M1(ref n) System.Console.WriteLine(n); Output..12
Total.net / Programming in C# / Session 3/ 38 of 38
Output parameter
It is a parameter that is passed from the called method to the method that called it-that is in reverse direction. They are useful when you want a method to return more than a single value. Output parameters are passed by reference and do not require initialization before use. Public void aWord(out string Word) { Word=Vibrant; } Public void showword() { String Word; aWord(out Word); Console.WriteLine(Word); } Total.net / Programming in C# / Session 3/ 39 of 38
Value Types
Pointers
Reference Types
Predefined Types
Predefined Types
Enumerations Structures
Objects Strings
Value Types
int X ,Y; X=15; Y=X; X=30; The value of Y? Ans= Y is 15 X and Y are two separate variables and has no effect when change is made.
Total.net / Programming in C# / Session 3/ 43 of 38
Value Types
Example -
Output -
Reference Type
System.Windows.Forms.Form x,y; X=new System.Windows.Forms.Form(); X.Text=This is Form1; Y=X; X.Text=This is form2; The value of Y? Ans Y is now This is Form2 Here the variable does not contain a Form but just points to the instance of the form.In case of Y=X, Variables X and Y now points to the same instance of the form.Thus returning the same value.
Total.net / Programming in C# / Session 3/ 45 of 38
Reference Types
Example -
Output
Data Types In C#
C# provides us with a Unified Type System.
All data types in C#, are derived from just one class, the object class
Static Members
Members are not associated with any particular object or the class. Only one instance possible
Arrays(1)
A group of values of similar data type. Belong to the reference type and hence are stored on the heap.
The declaration of arrays in C# follow the syntax given below DataType[number of elements] ArrayName;
int[6] arr1;
Total.net / Programming in C# / Session 3/ 54 of 38
Arrays(2)
Declaring an Array Variable Does Not Create an Array!
You must use new to explicitly create the array instance long[ ] row = new long[4]; 0 0 0 0 Array elements have an implicit default value of zero Variable
int[,] grid = new int[2,3];
grid
0 0 0 0 0 0
Total.net / Programming in C# / Session 3/ 55 of 38
ArrayList
For dynamic array,as c# does not have rediminising.(one can change its size). ArrayList Arr=new ArrayList(); Arr.Add(one); Arr.Add(two); Arr.Add(three); String items=Convert.Tostring(Arr[0]);
Total.net / Programming in C# / Session 3/ 56 of 38
Structures
Custom data Types Can have methods Can have constructors Cannot implement inheritance
Enumerators (1)
Are a set of named constants.
Enumerators (2)
Enumerators in C# have numbers associated with the values. By default, the first element of the enum is assigned a value of 0 and is incremented for each subsequent enum element. The default value can be overridden during initialization.
System.String class
String Str=This is a test string ; Str=Str.Trim()// This is a test string Str=Str.substring(0,4);// This Str=Str.Replace(is,at)// That Str=Str.ToUpperCase()//THIS IS A TEST STRING
Total.net / Programming in C# / Session 3/ 60 of 38
Error handling
Exception Objects
Exception
IOException SystemException OutOfMemoryException OverflowException CoreException Represents fatal run-time errors Represents non-fatal run-time errors
NullReferenceException
try { File source = new File("code.cs"); int length = (int)source.Length; char[ ] contents = new char[length]; ... } catch (System.Exception caught) { Console.WriteLine(caught); }
Error handling
To run the C# file, type the name of the file without the extension.
Total.net / Programming in C# / Session 3/ 68 of 38