Chapter 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Chapter 1

1. Language and Environment


1.1. Introducing windows programming
Microsoft windows is a computer operating system that provides graphical objects used by
people to interact with a computer. These objects are called windows, must be primarily
created by a person or a software development company and then installed on a computer
that will be used. To create one of these objects, you usually use a computer programming
language such as C++, Java, Python, C# etc…
One of these objects/windows is usually not enough to make it particularly useful for people.
Another approach consists of grouping various these objects/windows and assemble them
into something called a computer software/application, a computer program, a windows
application/software etc... All these expressions mean the same.
For this course we will use a computer programming language called C# which was
specifically built/created for windows application development but it can also be used to
create software applications that run under other platforms too such as windows store apps,
windows phone etc…
In the 1980s, most applications written to run on the Windows operating system were written
in C++.
Even though C++ is an OOP language, it’s arguably a difficult language to master and the
programmer is responsible for dealing with such housekeeping tasks such as memory
management and security. These housekeeping tasks are difficult to implement and often
neglected which results in buggy applications that are difficult to test and maintain.
In the 1990s, the Java programming language became popular. Because it’s a managed
programming language, it relieves the programmer from having to worry about the
housekeeping code. Managed languages provide a generalized way (through a base set of
common classes) to handle the housekeeping details such as memory management and
garbage collection. This allows the programmer to concentrate on the business logic and
frees them from having to worry about the error-prone housekeeping code. As a result,
programs are more compact, reliable, and easier to debug.
Seeing the success of Java and the increased popularity of the Internet, Microsoft developed
its own set of managed programming languages. Microsoft wanted to make it easier to
develop both Windows and Web-based applications. These managed languages rely on
the .NET Framework to provide much of the functionality to perform the housekeeping code
required in all applications. During the development of the .NET Framework, the class
libraries were written in a new language called C#. The principal designer and lead architect
of C# is Anders Hejlsberg. Hejlsberg was previously involved with the design of Turbo
Pascal and Delphi. He leveraged this previous experience to design an OOP language that
built on the successes of these languages and improved upon their shortcomings. Hejlsberg
also incorporated syntax similar to C into the language in order to appeal to the C++ and Java
developers.
Some of the goals of creating the .NET Framework, the Common Language Runtime (CLR),
and the C# language was to introduce modern concepts such as object orientation, type
safety, garbage collection, and structured exception handling directly into the platform.
Another goal of Microsoft has always been increasing programmer productivity. Since its
initial release in 2002, Microsoft has continued to improve and innovate the .NET
Framework along with their core languages built on top of the framework – C# and Visual
Basic. Microsoft is also committed to providing .NET developers the tools necessary to have
a highly productive and intuitive programming experience.
1.2. What is c#
C# is a modern, general purpose (meaning it can be used to develop many types of software
systems such as desktop and web applications), object oriented programming language
developed by Microsoft together with the .net platform and approved by European Computer
Manufacturers Association (ECMA) and international Standards Organization (ISO).
There is highly diverse software developed with C# and on the .Net platform: office
applications, web applications, websites, desktop applications, mobile applications, games
and many more.
C# is high-level programming language that is similar to java and C, C++ and to some
extent, languages like Delphi, all C# programs are object oriented, they consist of set of
definitions in classes that contain methods and methods contain program logic- the
instructions which the computer executes.
C# is one of the most popular programming languages. It is used by millions of developers
worldwide. Because C# is developed by Microsoft as part of their modern platform for
development and execution of applications, the .Net framework, the language widely spread
among Microsoft-oriented companies, organizations and individual developers.
It is impossible to discuss C# without discussing the Microsoft .NET Framework that
supports C# and many other programming languages. The .NET Framework is essentially a
giant collection of tools for software development. It was designed to allow traditional text-
based applications, GUI applications, and Web-based programs to all be built with equal
ease. Currently, over 90 languages are supported by the .NET Framework. That means
applications written in any of these languages have access to the tools provided within
the .NET Framework. Applications can even be written that mix and match modules in
various languages.
The .Net Framework is a framework for developing and implementing software for personal
computer, web etc…
It was designed and maintained by Microsoft Corporation. The .Net has a rich collection of
class library (called the Base class Library) to implement GUI, Query, Database, Web
services etc…
Programs developed with .Net needs a virtual machine to run on a host. This virtual machine
is called Common Language Runtime (CLR). Since the compiler doesn’t produce native
machine code, and its product is interpreted by the CLR, this allows for better security.
The C# language is a distributed together with a special environment on which it is executed,
called the Common Language Runtime (CLR). This environment is part of the .Net Platform,
which includes CLR, a bundle of standard libraries providing basic functionality, compilers
debuggers and other development tools. Thanks to the framework CLR programs are
portable and, once written they can function with little or no changes on various hardware
platforms and operating systems. C# programs are most commonly run on MS Windows, but
the .Net Framework and CLR also support mobile phones and other portable devices based
on windows mobile, windows phone and windows 8 etc… C# programs can still be run
under Linux, FreeBSD, IOS, and android, MacOS X and other operating systems through the
free .Net Framework implementation Mono, which however, is not officially supported by
Microsoft.
1.3. Common Language Infrastructure (CLI)
The Common Language Infrastructure (CLI) is an open specification (technical standard)
developed by Microsoft and standardized by ISO and ECMA that describes executable code
and runtime environment that allows multiple high-level languages to be used on different
computer platforms without being rewritten for specific architectures. This implies it is
platform agnostic. The .Net Framework, .Net Core, Mono, DotGNU and portable.Net are
implementations of the CLI.
The common Language Infrastructure (CLI) is a specification that allows several different
programming languages to be used together on a given platform. The CLI has a lot of
components, typically referred to by three-letter abbreviations (acronyms). Here are the most
important parts of the Common Language Infrastructure (CLI):
 Common Intermediate Language (CIL) including a common Type system (CTS),
 Common Language Specification (CLS) - shared by all languages.
 Virtual Execution System (VES)
 Metadata about types, dependent libraries, attributes and more.
The Common Type System (CTS), is a set of data types and operations that are shared by all
CTS-compliant languages.
The Common Language Specification (CLS), is a set of base rules to which any language
targeting the CLI should conform in order to interoperate with other CLS-compliant
languages. The CLS rules define a subset of the Common Type System.
The Virtual Execution System (VES) loads and executes CLI-compatible programs, using the
metadata to combine separately generated pieces of code at runtime.
The metadata information about the program structure is language-agnostic, so that it can be
referenced between languages and tools, making it easy to work with code written in a
language the developer is not using.

1.4. Common Intermediate Language (CIL)


Common intermediate language (CIL), formerly called Microsoft Intermediate Language
(MSIL), is the lowest-level human readable programming language defined by the Common
Language Infrastructure (CLI) specification and is used the .Net Framework, .Net Core, and
Mono.
Languages which target CLI-compatible runtime environment compile to CIL, which is
assembled into an object code that has a byte code style format. CIL is an object oriented
assembly language, and is entirely stack-based. Its byte code is translated into native code or
most commonly executed by a virtual machine.
During the compilation of CLI programming languages, the source code is translated into
CIL code rather than into platform or processor-specific object code. CIL is a CPU and
platform independent instruction set that can be executed in any environment supporting the
common language infrastructure, such as the .Net runtime on windows, or the cross platform
mono runtime.in theory, this eliminates the need to distribute different executable files for
different platforms and CPU types. CIL code is verified for safety during runtime, providing
better security and reliability than natively compiled executable files.
The execution process looks like this:
1. Source code is converted to CIL i.e. common intermediate Language, which is the
CLI’s equivalent to assembly language for a CPU.
2. CIL is then assembled into a form of so-called bye code and CLI assembly is created.
3. Upon execution of a CLI assembly, its code is passed through the runtime’s JIT (just
in time) compiler to generate native code. Ahead of time compilation may also be
used, which eliminates this step, but at the cost of executable-file portability.
4. The computer’s processor executes the native code.
1.5. Common IDE’s
Microsoft provides a comprehensive Integrated Development Environment (IDE) for the
development and testing of software with the .Net. Following are some of the development
tools for C# programming:
 Visual Studio
 Visual Studio Express
 Visual Web Developer
The last two are freely available from Microsoft official website. Using these tools, you can
write all kinds of C# programs from simple command-line applications to more complex
application. You can also write C# source code files using a basic text editor like notepad, and
compile the code into assemblies using the command-line compiler, which is again a part of
the .Net Framework.
Visual C# express and Visual Web Developer express edition are trimmed down versions of
Visual studio and has same appearances. They retain most features of visual studio.
1.6 Overview of C#
C# (pronounced "See Sharp") is a modern, object-oriented, and type-safe programming
language. C# has its roots in the C family of languages and will be immediately familiar to C,
C++, Java, and JavaScript programmers.
1.6.1 C# program Structure
A c# program consists of the following parts:
 A namespace declaration
 A class
 Class methods
 Class attributes
 A main program
 Statements and expressions
 Comments
Here is a simple program which prints the string “hello world” into the screen:
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}

1.7. Literals, Variable and Data types


There are two kinds of types in C#: value types and reference types. Variables of value types
directly contain their data whereas variables of reference types store references to their data, the
latter being known as objects. With reference types, it's possible for two variables to reference
the same object and thus possible for operations on one variable to affect the object referenced
by the other variable. With value types, the variables each have their own copy of the data, and it
isn't possible for operations on one to affect the other (except for ref and out parameter
variables).
C#'s value types are further divided into simple types, enum types, struct types, and nullable
value types. C#'s reference types are further divided into class types, interface types, array types,
and delegate types.
The following outline an overview of c# type system
Value types
 Simple types
o Signed integral: sbyte, short, int, long
o Unsigned integral: byte, ushort, uint, ulong
o Unicode characters: char
o IEEE binary floating-point: float, double
o High-precision decimal floating-point: decimal
o Boolean: bool
 Enum types
o User-defined types of the form enum E {...}
 Struct types
o User-defined types of the form struct S {...}
 Nullable value types
o Extensions of all other value types with a null value
 Tuple value types
o User-defined types of the form (T1, T2, ...)
Reference types
 Class types
o Ultimate base class of all other types: object
o Unicode strings: string
o User-defined types of the form class C {...}
 Interface types
o User-defined types of the form interface I {...}
 Array types
o Single- and multi-dimensional, for example, int[] and int[,]
 Delegate types
o User-defined types of the form delegate int D(...)

1.8. Operators and Expressions


C# provides a number of operators supported by the built in types, for example, arithmetic
operators perform arithmetic operations with numeric operands and Boolean logical operators
perform logical operations with the bool operands. Certain operators can be overloaded. With
operator overloading, you can specify the operator behavior for the operands of a user-defined
type.
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C# has rich set of built-in operators and provides the following type of operators:
 Arithmetic Operators
 Relational Operators
 Logical (boolean) Operators
 Assignment Operator
 Bitwise Operators
 Misc Operators
In an expression, operator precedence and associativity determine the order in which the
operations are performed. You can use parentheses to change the order of evaluation imposed by
operator precedence and associativity.
1.8.1. Arithmetic Operators
The following operators perform arithmetic operations with operands of numeric types:
 Unary ++ (increment), -- (decrement), +(plus) and – (minus) operators
 Binary * (multiplication), / (division), % (remainder), + (addition ), and – (subtraction)
operators
Those operators are supported by all integral and floating-point numeric types.
Here are some examples of the mentioned operators
int i = 3;
Console.WriteLine(i); // output: 3
Console.WriteLine(i++); // output: 3
Console.WriteLine(i); // output: 4

double a = 1.5;
Console.WriteLine(a); // output: 1.5
Console.WriteLine(++a); // output: 2.5
Console.WriteLine(a); // output: 2.5

Console.WriteLine(5 % 4); // output: 1


Console.WriteLine(5 % -4); // output: 1
Console.WriteLine(-5 % 4); // output: -1
Console.WriteLine(-5 % -4); // output: -1

1.8.2. Relational Operators


The < (less than), > (greater than), <= (less than or equal), ==, and >= (greater than or equal)
comparison, also known as relational, operators compare their operands. Those operators are
supported by all integral and floating-point numeric types.
Console.WriteLine(7.0 < 5.1); // output: False
Console.WriteLine(5.1 < 5.1); // output: False
Console.WriteLine(0.0 < 5.1); // output: True

Console.WriteLine(7.0 > 5.1); // output: True


Console.WriteLine(5.1 > 5.1); // output: False
Console.WriteLine(0.0 > 5.1); // output: False

Console.WriteLine(7.0 <= 5.1); // output: False


Console.WriteLine(5.1 <= 5.1); // output: True
Console.WriteLine(0.0 <= 5.1); // output: True

Console.WriteLine(7.0 >= 5.1); // output: True


Console.WriteLine(5.1 >= 5.1); // output: True
Console.WriteLine(0.0 >= 5.1); // output: False
1.8.3. Logical (Boolean) operators
The following operators perform logical operations with bool operands:
 Unary ! (logical negation) operator.
 Binary & (logical AND), | (logical OR), and ^ (logical exclusive OR) operators. Those
operators always evaluate both operands.
 Binary && (conditional logical AND) and || (conditional logical OR) operators. Those
operators evaluate the right-hand operand only if it's necessary.
The unary prefix ! operator computes logical negation of its operand. That is, it produces true, if
the operand evaluates to false, and false, if the operand evaluates to true:
bool passed = false;
Console.WriteLine(!passed); // output: True
Console.WriteLine(!true); // output: False

The & operator computes the logical AND of its operands. The result of x & y is true if both x
and y evaluate to true. Otherwise, the result is false.
The & operator evaluates both operands even if the left-hand operand evaluates to false, so that
the operation result is false regardless of the value of the right-hand operand.
In the following example, the right-hand operand of the & operator is a method call, which is
performed regardless of the value of the left-hand operand:
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = false & SecondOperand();


Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False

bool b = true & SecondOperand();


Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

The conditional logical AND operator && also computes the logical AND of its operands, but
doesn't evaluate the right-hand operand if the left-hand operand evaluates to false.
The ^ operator computes the logical exclusive OR, also known as the logical XOR, of its
operands. The result of x ^ y is true if x evaluates to true and y evaluates to false, or x evaluates
to false and y evaluates to true. Otherwise, the result is false. That is, for the bool operands, the ^
operator computes the same result as the inequality operator !=.
Console.WriteLine(true ^ true); // output: False
Console.WriteLine(true ^ false); // output: True
Console.WriteLine(false ^ true); // output: True
Console.WriteLine(false ^ false); // output: False

The | operator computes the logical OR of its operands. The result of x | y is true if either x or y
evaluates to true. Otherwise, the result is false.
The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the
operation result is true regardless of the value of the right-hand operand.
In the following example, the right-hand operand of the | operator is a method call, which is
performed regardless of the value of the left-hand operand:
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = true | SecondOperand();


Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// True

bool b = false | SecondOperand();


Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

The conditional logical OR operator || also computes the logical OR of its operands, but doesn't
evaluate the right-hand operand if the left-hand operand evaluates to true.
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = true || SecondOperand();


Console.WriteLine(a);
// Output:
// True

bool b = false || SecondOperand();


Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
The logical OR operator | also computes the logical OR of its operands, but always evaluates
both operands.
1.8.4. Assignment Operators
The assignment operator = assigns the value of its right hand operand to a variable, a property,
indexer element given by its left-hand operand. The result of an assignment expression is the
value assigned to the left-hand operand. The type of the right-hand operand must be the same as
the type of the left-hand operand or implicitly convertible to it.
a = b = c

is evaluated as
a = (b = c)

for a binary operator op, a compound assignment expression of the form:


x op=y
is equivalent to:
x = x op y
For example: a +=b is equal to a = a+b
1.9. Expressions
Expressions are constructed from operands and operators. The operators of an expression
indicate which operations to apply to the operands. Examples of operators include +, -, *, /, and
new. Examples of operands include literals, fields, local variables, and expressions.
When an expression contains multiple operators, the precedence of the operators controls the
order in which the individual operators are evaluated. For example, the expression x + y * z is
evaluated as x + (y * z) because the * operator has higher precedence than the + operator.
The actions of a program are expressed using statements. C# supports several different kinds of
statements, a number of which are defined in terms of embedded statements.
A block permits multiple statements to be written in contexts where a single statement is
allowed. A block consists of a list of statements written between the delimiters { and }.
Declaration statements are used to declare local variables and constants.
Expression statements are used to evaluate expressions. Expressions that can be used as
statements include method invocations, object allocations using the new operator, assignments
using = and the compound assignment operators, increment and decrement operations using the
++ and -- operators and await expressions.

You might also like