Presentation is loading. Please wait.

Presentation is loading. Please wait.

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#

Similar presentations


Presentation on theme: "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#"— Presentation transcript:

1 Programming in C#

2 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.

3 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.

4 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.

5 C# Language Features Arrays Constructors and Destructors Indexers Main Properties Passing Parameters

6 Introducing the Building Blocks of the.NET Platform (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.

7 Compilation occurs in two steps in.Net Compilation of source code to intermediate Language. Compilation of IL to platform–specific code By the CLR

8 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

9 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

10 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.

11 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.

12 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

13 .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)

14 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: /// /// This class performs an important function. /// public class MyClass{}

15 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.const Constants must be initialized as they are declared. For example: class Calendar1 { public const int months = 12; }

16 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.

17 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.

18 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:

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

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

21 Keywords

22 Methods Building blocks of C# programs Every program is a class! The Main method Each console or windows application must have exactly one

23 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

24 // 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 }

25 // 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 }

26 Simple Program: Output

27 1 // 2 // Printing multiple lines in a dialog Box. 3 4 using System; 5 using System.Windows.Forms; 6 7 class Welcome4 8 { 9 Static void Main( string[] args ) 10 { 11 MessageBox.Show( “ Welcome\nto\nC#\nprogramming!" ); 12 } 13 } The System.Windows.Forms namespace allows the programmer to use the MessageBox class. This will display the contents in a message box as opposed to in the console window.


Download ppt "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#"

Similar presentations


Ads by Google