c# b 1 csc 298 writing a c# application. c# b 2 a first c# application // display hello, world on...

9
C# B 1 CSC 298 Writing a C# application

Upload: claire-wells

Post on 29-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 1

CSC 298

Writing a C# application

Page 2: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 2

A first C# application// Display Hello, world on the screenpublic class HelloWorld {

public static void Main(){

System.Console.WriteLine("Hello, world") ;}

}

comment

class definition

class method

use the System.Console class to write some text to the console

every C# statementends with a semi colon ;

Page 3: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 3

Code organization

In C# (as in Java), no stand alone function Code is written inside of blocks {} that are class

definitions public class HelloWorld { // my code is here … } for now always write public class. More on this later

Good habits from Java (not required in C#) Name the file that contains the definition of the public

class HelloWorld: HelloWorld.cs Write only one public class per C# file

Page 4: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 4

Comments Ignored by the computer. Essential for the reader. 2 types of comments: // and /* */

Everything between // and the end of the line is a comment

Everything between /* and */ is a comment. It can be used on several lines. You can't nest these comments (/* blabla /* blabla */ blabla */ gives an error)

Examples // this is a comment /* this is a comment that can be written on several lines

*/ Also XML comments /// (see VS.NET doc)

Page 5: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 5

The Main method An application always start executing with the Main

method (note the capital M) The Main method must be static It can be of type void or return an int It can take no arguments or an array of strings It can be of any visibility (public, private…)

public static int Main()

static void Main(String[] args)

// default to private

Page 6: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 6

Console Input/Output in C# Console: a class to access I/O functionalities for

the console (=DOS window) In: input stream, Out: output stream important static methods:

Write, WriteLine, ReadLine Console is available in the namespace System

Console.Write ("Enter your name: ");String name = Console.ReadLine();Console.WriteLine("Welcome " + name);

Page 7: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 7

System.Windows.Forms

A namespace that contains many classes to build graphical user interfaces

We will see many examples

Here is a simple example

MessageBox.Show("Hello, world!", "Message");

Page 8: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 8

The using statement Define a shortcut to write the names of classes of the

FCL in our programs. using System.Windows.Forms means:

get access to all of the classes in the namespace System.Windows.Forms

In the program, to access the classSystem.Windows.Forms.MessageBox

just type MessageBox

Page 9: C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void

C# B 9

Formatting output To concatenate strings (=sequence of characters), use +

Write("Hello, " + "World") prints Hello, World

Write(""+1+2) is not the same as Write(1+2). Why? For tabs and newlines, use '\t' and '\n'

Write("Hello\tWorld") prints Hello World Write("Hello\nWorld") prints

HelloWorld

See later for other ways