Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Getting Started with C# Chapter 1. 22 Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.

Similar presentations


Presentation on theme: "11 Getting Started with C# Chapter 1. 22 Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a."— Presentation transcript:

1 11 Getting Started with C# Chapter 1

2 22 Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a very simple Windows console application in C# using Visual Studio 2008.

3 33 Introduction to C# C# is an extension of ANSI C Similar to Java Microsoft Specific (Sort of) Technically an ECMA Standard Adds strong typing. Adds object oriented programming constructs. Classes, Inheritance, Polymorphism Takes away “dangerous” or error prone features. Pointers, malloc, explicit memory deallocation No concept of “memory address” “References” serve the purpose of pointers. Provides automatic, transparent garbage collection Lots of useful code in libraries

4 44 Introduction to C# Targeted to Microsoft platforms Specifically to the.NET framework PC applications for Windows Web applications for Microsoft web servers Both GUI and console applications are possible. An open source, cross-platform implementation of.NET Framework, Mono, is available: http://www.mono-project.com

5 55 Introduction to C# Supported by the Visual Studio IDE Very easy to do simple GUI applications Comparable to Visual Basic Possible to do large complex PC applications We will use Visual Studio 2008 Available on all College of Engineering and CSE lab and classroom computers. Free download available for USF students. https://www.dreamspark.com

6 66 Introduction to C# A Quick Look at Programming in C# (Changes and additions to standard C) Start with Console applications. Run from a command window (No GUI) Simplest possible C# programs Get started with object oriented programming Later look at Windows Forms applications GUI Based A little more complex

7 7 First C# Program Let's do "Hello, World!" in C#

8 88 Start Visual Studio 2008 Your Start Page will look different.

9 99 Create a New Project

10 10 Select Project Attributes

11 11 Template for a Console Application Generated automatically by Visual Studio

12 12 The Hello World Program

13 13 Build the Project

14 14 Run the Program Click here to run

15 15 The Hello World Program

16 16 Possibly Unfamiliar Features Overview (Details to Follow) Namespace using... ; class Program static void Main (string[] args)

17 17 Namespace Helps avoid name conflicts in large projects Not necessary, or particularly helpful, on small projects Visual Studio automatically wraps everything with namespace projectName { } OK to delete if you wish. Or just ignore Be careful when reusing source code.

18 18 using System; Recall the line: Console.WriteLine("Hello, World!"); “Console” is a class in the System namespace To refer to something outside the current namespace, you normally have to prefix the name with the namespace: System.Console.WriteLine("Hello, World!"); “ using System;” permits us to not do this. C# programs typically begin with lots of “using” statements

19 19 "using" Statements We could delete the other three "using" statements. Included in the template by Visual Studio because they are frequently needed But not needed by this program.

20 20 using System If we delete "using System" we get a compile time error. Compiler does not know about class Console.

21 21 Editor Shows Errors Notice squiggly underlines.

22 22 using System If we prefix Console with the namespace System, there is no error. namespace Hello { class Program { static void Main(string[] args) { System.Console.WriteLine("Hello, World!"); System.Console.ReadLine(); // Keep window open } "using System" saves typing and clutter.

23 23 Things to Notice In C# everything is in a class. Unlike C and C++ No significance in this simple console app. Very important in real programs. Think of “class Program” as a wrapper required by the environment. We will use C# classes later in the course. Similar to classes in C++ and Java.

24 24 Things to Notice Every C# program must have a “Main” static void Main(string[ ] args) Note capitalization. “static” means that the function is associated with the class as a whole rather than with a specific object. Like C++. We never instantiate class Program. “string[ ] args” permits arguments to be passed in from the command line as in standard C. Not normally used for Windows applications. OK to ignore. (Just leave the parentheses empty) static void Main()

25 25 Things to Notice Like standard C and C++ C# is case sensitive System.Console.Writeline("Hello, World!"); gets a compile error static void main() doesn’t work

26 26 Things to Notice Like standard C++ the characters // say that the rest of the line is a comment. Traditional C comments also work. /* This is a traditional C comment */

27 27 Running Outside the IDE We have an executable file: Hello.exe 27

28 28 Path to the Executable 28

29 29 Double Click the.exe File 29

30 30 Deploying the Program Hello.exe is a complete executable program. Called an assembly (Microsoft terminology) MSIL Microsoft Intermediate Language, not machine code “Managed code” Runs under the.NET Common Language Runtime (CLR) Compiled into machine code as needed. (JIT Compiler) Can be moved to another directory Can be copied to another Windows system that has the.NET Framework installed Can be run from the command line in a Windows console window 30

31 31 From a Console Window 31 Copy Hello.exe to C: Open Command Prompt window and cd to C:

32 32 Assignment Before next class: Read Chapter 1. Do the examples from this class for yourself.


Download ppt "11 Getting Started with C# Chapter 1. 22 Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a."

Similar presentations


Ads by Google