1
C# Programming
Table of Content
1. Introduction to C# 2
2. C# Basic Programming 3
3. C# Type Conversion 6
4. C# Variables 8
5. C# Operators 12
6. Conditional Statements 17
7. Array 18
8. Object Oriented Programming Language 21
● Encapsulation 23
● Inheritance 31
● Polymorphism 38
● Abstraction 50
● Reflection 65
● Delegates 72
9. Exception Handling 76
10. Collection &Generic Collection 86
11. Multithreading 101
12. ThreadSafe in C# 106
, 2
Introduction to C#
C# is a modern, general-purpose, object-oriented programming language developed by Microsoft.
This is designed for Common Language Infrastructure (CLI), which consists of the executable code
and runtime environment that allows use of various high-level languages to be used on different
computer platforms and architectures.
The following reasons make C# a widely used professional language:
● Modern, general-purpose programming language
● Object oriented.
● Component oriented.
● Easy to learn.
● Structured language.
● It produces efficient programs.
● It can be compiled on a variety of computer platforms.
● Part of .Net Framework.
Strong Programming Features of C#
Although C# constructs closely follow traditional high-level languages, C and C++ and being an
object-oriented programming language, it has strong resemblance with Java, it has numerous strong
programming features that make it endearing to multitude of programmers worldwide.
Following is the list of few important features:
● Automatic Garbage Collection
● Standard Library
● Assembly Versioning
● Properties and Events
● Delegates and Events Management
● Easy-to-use Generics
● Conditional Compilation
● Simple Multithreading
● LINQ and Lambda Expressions
● Integration with Windows
C# Basic Programming
C# Hello World Example
, 3
A C# program basically consists of the following parts:
● Namespace declaration
● A class
● Class methods
● Class attributes
● A Main method
● Statements & Expressions
● Comments
Let us look at a simple code that would print the words "Hello World":
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
Hello World
Let us look at various parts of the above program:
1. 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.
2. The next line has the namespace declaration. A namespace is a collection of classes. The
HelloWorldApplication namespace contains the class HelloWorld.
3. The next line has a class declaration, the class HelloWorld contains the data and method
definitions that your program uses. Classes generally would contain more than one method.
Methods define the behavior of the class. However, the HelloWorld class has only one method
Main.
, 4
4. The next line defines the Main method, which is the entry point for all C# programs. The
Main method states what the class will do when executed
5. The next line /*...*/ will be ignored by the compiler and it has been put to add additional
comments in the program.
6. The Main method specifies its behavior with the statement Console.WriteLine("Hello
World");
7. 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.
8. 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.
It's worth to note the following points:
● C# is case sensitive.
● All statements and expression must end with a semicolon (;).
● The program execution starts at the Main method.
● File name could be different from the class name.
Compile & Execute a C# Program:
If you are using Visual Studio.Net for compiling and executing C# programs, take the following steps:
● Start Visual Studio.
● On the menu bar, choose File, New, Project.
● Choose Visual C# from templates.
● Choose Console Application.
● Specify a name for your project, and then choose the OK button.
● The new project appears in Solution Explorer.
● Write code in the Code Editor.
● Click the Run button or the F5 key to run the project. A Command Prompt window appears
that contains the line
Hello World.
The using Keyword
The first statement in any C# program is
using System;
C# Programming
Table of Content
1. Introduction to C# 2
2. C# Basic Programming 3
3. C# Type Conversion 6
4. C# Variables 8
5. C# Operators 12
6. Conditional Statements 17
7. Array 18
8. Object Oriented Programming Language 21
● Encapsulation 23
● Inheritance 31
● Polymorphism 38
● Abstraction 50
● Reflection 65
● Delegates 72
9. Exception Handling 76
10. Collection &Generic Collection 86
11. Multithreading 101
12. ThreadSafe in C# 106
, 2
Introduction to C#
C# is a modern, general-purpose, object-oriented programming language developed by Microsoft.
This is designed for Common Language Infrastructure (CLI), which consists of the executable code
and runtime environment that allows use of various high-level languages to be used on different
computer platforms and architectures.
The following reasons make C# a widely used professional language:
● Modern, general-purpose programming language
● Object oriented.
● Component oriented.
● Easy to learn.
● Structured language.
● It produces efficient programs.
● It can be compiled on a variety of computer platforms.
● Part of .Net Framework.
Strong Programming Features of C#
Although C# constructs closely follow traditional high-level languages, C and C++ and being an
object-oriented programming language, it has strong resemblance with Java, it has numerous strong
programming features that make it endearing to multitude of programmers worldwide.
Following is the list of few important features:
● Automatic Garbage Collection
● Standard Library
● Assembly Versioning
● Properties and Events
● Delegates and Events Management
● Easy-to-use Generics
● Conditional Compilation
● Simple Multithreading
● LINQ and Lambda Expressions
● Integration with Windows
C# Basic Programming
C# Hello World Example
, 3
A C# program basically consists of the following parts:
● Namespace declaration
● A class
● Class methods
● Class attributes
● A Main method
● Statements & Expressions
● Comments
Let us look at a simple code that would print the words "Hello World":
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
Hello World
Let us look at various parts of the above program:
1. 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.
2. The next line has the namespace declaration. A namespace is a collection of classes. The
HelloWorldApplication namespace contains the class HelloWorld.
3. The next line has a class declaration, the class HelloWorld contains the data and method
definitions that your program uses. Classes generally would contain more than one method.
Methods define the behavior of the class. However, the HelloWorld class has only one method
Main.
, 4
4. The next line defines the Main method, which is the entry point for all C# programs. The
Main method states what the class will do when executed
5. The next line /*...*/ will be ignored by the compiler and it has been put to add additional
comments in the program.
6. The Main method specifies its behavior with the statement Console.WriteLine("Hello
World");
7. 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.
8. 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.
It's worth to note the following points:
● C# is case sensitive.
● All statements and expression must end with a semicolon (;).
● The program execution starts at the Main method.
● File name could be different from the class name.
Compile & Execute a C# Program:
If you are using Visual Studio.Net for compiling and executing C# programs, take the following steps:
● Start Visual Studio.
● On the menu bar, choose File, New, Project.
● Choose Visual C# from templates.
● Choose Console Application.
● Specify a name for your project, and then choose the OK button.
● The new project appears in Solution Explorer.
● Write code in the Code Editor.
● Click the Run button or the F5 key to run the project. A Command Prompt window appears
that contains the line
Hello World.
The using Keyword
The first statement in any C# program is
using System;