Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

Getting Started with C#

Chapter 1

1
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 2015.

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

4
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

5
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

6
First C# Program

 Let's do "Hello, World!" in C#

7
Create a New Project

8
Select Project Attributes

9
Template for a Console Application
Generated automatically by Visual Studio

10
The Hello World Program

11
Build the Project

12
Run the Program
Click here to run

13
The Hello World Program

14
Possibly Unfamiliar Features
Overview
(Details to Follow)

 Namespace
 using ... ;
 class Program
 static void Main (string[] args)

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

16
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

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

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

19
Editor Shows Errors

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


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

22
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()


23
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

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

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

26
Path to the Executable

27
Double Click the .exe File

28
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
29
From a Console Window

Copy Hello.exe to C:
Open Command Prompt window and cd to C:

30
Assignment

Before next class:

 Read Chapter 1.

 Do the examples from this class for


yourself.

31

You might also like