3.1. C# Programming
3.1. C# Programming
Programming Fundamentals in C#
1
What is C#?
▪ C#(pronounced as “C sharp‟) is a new computer-programming
language developed by MS Corporation, USA.
▪ It is a fully object-oriented language like Java and is the first
component-oriented language.
▪ It has been designed to support the key features of .NET
Framework, the new development platform of MS for building
component-based software solutions.
▪ It is simple, efficient, productive, and type-safe language derived
from C and C++ languages.
▪ It is purely object-oriented modern language suitable for developing
window and web based applications.
2
What is C#?, …. Cont’
▪ It is designed for building robust, reliable, and durable components
to handle real-world applications
▪ It borrows Java’s features such as grouping of classes, interfaces,
and implementations together in one file so that programmers can
edit the code more easily.
▪ Also, it handles objects using references, the same way as Java by
using dot operator.
▪ C# uses VB’s (Visual basic) approach to form design, namely,
dragging controls from toolbox, dropping them onto forms, and
writing event handlers for them.
3
Characteristics of C#
▪ Versionable: provides support for versioning with the help of new and
override keywords.
▪ Compatible: enforces the .NET common language specifications and therefore
allows interoperation with other .NET languages.
▪ Interoperable: provides support for using COM objects, no matter what
language was used to author them.
▪ Flexible: although it doesn’t support pointers, we may declare certain classes
and methods as “unsafe‟ and then use pointers to manipulate them.
▪ Object oriented: is truly object-oriented, supports encapsulation, inheritance,
and polymorphism.
▪ Everything is an object, there is no more global methods(functions), constants, and
variables
4
Applications of C#
▪It can be used for variety of applications that are supported by the
.NET platform such as:
▪ Console applications;
▪ Windows Form applications;
▪ Developing Windows Form controls;
▪ Developing ASP.NET projects;
▪ Creating Web controls;
▪ Providing Web Services;
▪ Developing .NET component library.
5
A Simple C# Program
7
Overview of C# Program Components
• Keywords:
8
C# Program Components: Keywords(cont..)
9
C# Program Components: Identifiers
10
C# Program Components: Literals
• Literals are value constants assigned to variables(or results of expression) in a
program.
• Backslash literals are also another type of C# literals
11
C# Program Components: Literal sample
12
C# Program Components: Variables
13
C# Data Types
14
C# Data Types (cont..)
15
Variable Declaration and Initialization
• A variable must be declared before it is used b/c:
• It tells the compiler what the name of the variable is,
• It specifies what type of data the variable will hold,
• The place of declaration decides the scope of the variable;
• General syntax:
• type variable1,variable2,…variableN;
17
Constant Variables:
• variables whose values don’t change during execution of a program;
• are declared and initialized using const keyword; e.g:
• const int m=0;
• const int m; m=2;//illegal
• int n=9; const int m=n*2;//illegal
18
Scope of Variables
• Scope is a region of a code within which a variable can be
accessible.
• It depends on the type of the variable and place of declaration.
• Types of variables include:
• Static variables: declared at class level using static keyword, known as
fields;
• Instance variables: declared at class level;
• Array elements: if declared at class level treated as fields
• Value parameters: not declared with ref keyword
• Reference parameters: declared with ref keyword
• Output parameters: declared with out keyword
• Local variables: declared within methods
19
Scope of Variables(cont..)
20
Operators and Expressions
22
Operators and Expressions(cont..)
24
Operators and Expressions(cont..)
• Conditional operator
25
Operators and Expressions(cont..)
• Bitwise operators are used to manipulate data at bit level
• Bitwise Operators
26
Bitwise Operators(cont..)
27
Bitwise Operators Program
28
Operators and Expressions(cont..)
29
Decision Making and Looping
• C# provides four types of iteration/looping statements
• The while statement
• The do statement
• The for statement
• The foreach statement
30
The While Statement
31
The Do Statement
32
The For Statement
33
The Foreach Statement
34
The Jumping Statements
• Jumping from loops like for, do, while, and switch using
break keyword;
35
C# Methods
• Are functions declared inside classes to access data in a
class. Method should be declared before use.
36
C# Methods (cont..)
37
C# Methods(cont..)
• Once a method is declared and defined it
is then called invoked to perform the
required operations.
• Method invocation syntax:
Objectname.methodname
(actual-parameter-list);
38
C# Methods(cont..)
40
Method Overloading
• Creating more than one method with the same name but different in
parameter list and definitions.
41