25
Programming in C#

Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Embed Size (px)

Citation preview

Page 1: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Programming in C#

Page 2: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

I. Introduction

C# (or C-Sharp) is a programming language. C# is used to write software that runs on the .NET Framework. Although C# is not the only language that you can use to target the .NET Framework, C# is one of the most popular because of its simplified C-based syntax.

Page 3: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

I. Introduction

In brief, C# (unlike C++, PERL, COBOL, Pascal, etc.) is a language that targets one and only one platform. This platform is the .NET Framework. However, the .NET Framework itself is a computing platform that is designed to be hosted by any operating system.

Page 4: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

I. Introduction

So you can see that although C# is designed to target only the Framework, the Framework itself is flexible enough to run your C# programs on many types of systems.

Page 6: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Introducing the Building Blocks of the .NETPlatform (the CLR, CTS, and CLS)

Central .Net Framework is its runtime execution environment , known as Common Language Runtime.

Code running under the control of CLR is often termed as Manage Code.

Before it executed in CLR,any source code that you develop in C# needs to be compiled .

Page 7: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Compilation occurs in two steps in .Net

Compilation of source code to intermediate Language.

Compilation of IL to platform–specific code

By the CLR

Page 8: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Common Language Runtime (CLR)

Central part of framework Executes programs

Compilation process Two compilations take place

Programs compiled to Microsoft Intermediate Language (MSIL)

Defines instructions for CLR

MSIL code translated into machine code Platform-specific machine language

Page 9: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Common Language Runtime (CLR) Why two compilations?

Platform independence .NET Framework can be installed on different platforms Execute .NET programs without any modifications to code .NET compliant program translated into platform independent

MSIL Language independence

MSIL form of .NET programs not tied to particular language Programs may consist of several . MSIL translated into

platform-specific code Other advantages of CLR

Execution-management features Manages memory, security and other features

Relieves programmer of many responsibilities More concentration on program logic

Page 10: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Common Type System

Another building block of the .NET platform is the Common Type System, or CTS. The CTS defines a set of rules that language compilers must follow to define ,reference, use and store reference and value types. Therefore, by following the CTS, objects written in different languages can interact with each other.

Page 11: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Common Language system

The CLS is the minimum specification of requirements that a language must support .This means that if you restrict your public methods to CLS ,all Languages supporting .Net can use your classes.

Page 12: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Visual Studio 2005 Integrated Development Environment

.NET initiative Independence from specific language or

platform Applications developed in any .NET-compatible

language Visual Basic.NET, Visual C++.NET, C# and more

Page 13: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

.NET and C#

.NET platform Web-based applications can be distributed to variety of

devices and desktops

C# Developed specifically for .NET Enable programmers to migrate from C/C++ and Java easily Event-driven, fully OO, visual programming language Has IDE Process of rapidly creating an application using an IDE is

called Rapid Application Development (RAD)

Page 14: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

XML Documentation Comments (C# Programming Guide)

In Visual C# you can create documentation for your code by including XML tags in special comment fields in the source code directly before the code block they refer to. For example:

/// <summary> /// This class performs an important

function. /// </summary> public class MyClass{}

Page 15: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Constants

Constants are immutable(not change) values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier.

Constants must be initialized as they are declared. For example:

class Calendar1 { public const int months = 12; }

Page 16: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Escape Sequences

Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ("). The following table lists the escape sequences and what they represent.

Page 17: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Escape Sequences

Character Meaning in Life \' Inserts a single quote into a string literal. \" Inserts a double quote into a string literal. \\ Inserts a backslash into a string literal. This can be quite helpful

when defining file paths. \a Triggers a system alert (beep). For console applications, this can

be an audio clue to the user. \n Inserts a new line (on Win32 platforms). \r Inserts a carriage return. \t Inserts a horizontal tab into the string literal.

Page 18: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

C# String Literal

Literals are how you hard-code strings into C# programs.

As another example, assume you wish to create a string literal that contains quotation marks,

another that defines a directory path, and a final string literal that inserts three blank lines after

printing the character data. To do so without compiler errors, you would need to make use of the \",

\\, and \n escape characters:

Page 19: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

example

Console.WriteLine("Everyone loves \"Hello World\"");

Page 20: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Keywords

Keywords are predefined reserved identifiers that have special meanings to the compiler .

Page 21: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Keywords

Page 22: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Methods

Building blocks of C# programs Every program is a class! The Main method

Each console or windows application must have exactly one

Page 23: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

Getting input

Data types built into C#

(string, int, double, char, long …15 types)

Console.ReadLine( ) Used to get a value from the user input

Page 24: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

// 2 // A first console program in C#. 3 4 using System; 5 6 class Welcome1 7 { 8 static void Main( string[] args ) 9 { 10 Console.WriteLine( "Welcome to C#

Programming!" ); 11 } 12 }

Page 25: Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#

// 2 // Printing a line with multiple statements. 3 4 using System; 5 6 class Welcome2 7 { 8 static void Main( string[] args ) 9 { 10 Console.Write( "Welcome to " ); 11 Console.WriteLine( "C# Programming!" ); 12 } 13 }