0% found this document useful (0 votes)
13 views

3.1. C# Programming

C# basics

Uploaded by

Menelik Mesfin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

3.1. C# Programming

C# basics

Uploaded by

Menelik Mesfin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 3

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

▪ C# is a true OOL therefore, everything must be placed inside class.


▪ In the program, class keyword is used to define a new class.
▪ SampleOne is a class name, {} indicates block, public static void Main() defines
a method named Main that can be accessed by any one without creating an
instance of the class and doesn’t return any value.
▪ Every C# executable program must contain Main() method in one of its
classes, it is an entry point for the execution of the program.
6
A Simple C# Program, …. Cont’
▪ System.Console.WriteLine(“C# is sharper than C++.”) is an output statement
that uses WriteLine static method of Console class that is found in System
namespace.
▪ Namespace is a collection of related .NET library classes.
▪ In order to avoid use of System keyword with Console in every required line of
the program, it is possible to include the System namespace using Using
directive at
beginning of a program.
▪ The compiler then looks for the used methods of the classes included in the
used namespace during compilation.
▪ C# also supports single line(//…) and multiple line(/*…*/) comments.

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;

• E.g. int x; float y; double d; object o;


• The process of giving initial values to variables is known as initialization.
Syntax: var_nam=value
16
C# Default Values
• Static variables, instance variables, and array elements are
assigned a default value if they are not explicitly given values.

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

•Arithmetic and relational operators and expressions 21


Operators and Expressions(cont..)

22
Operators and Expressions(cont..)

• Logical and assignment Operators and expressions


23
Operators and Expressions(cont..)

•Increment and decrement operators

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;

• Skipping a part of a loop using continue keyword;

• Jumping from loops using goto 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..)

• Types of method parameters:


• Value parameters: parameters passed by value;
• Reference parameters: passed by reference using ref keyword;
• Output parameters: used to pass results back from a method using
out keyword;
• Parameter arrays: used in a method definition to enable it to
received variable number of parameters.
• The keyword params is used.
39
C# Methods(cont..)

40
Method Overloading
• Creating more than one method with the same name but different in
parameter list and definitions.

• Overloaded methods must differ in parameter and/or type of parameter they


take.

• Return type of a methods doesn’t play any role in method overloading.

41

You might also like