C# Summer course - Lecture 1

20
Introduction to C# FCIS Summer training 2010, 1 st year. Mohamed Samy

description

C# summer training for FCIS, 2010. Lecture 1

Transcript of C# Summer course - Lecture 1

Page 1: C# Summer course - Lecture 1

Introduction to C#

FCISSummer training 2010, 1st year.

Mohamed Samy

Page 2: C# Summer course - Lecture 1

Aims of this course This course is an introduction to:

The Event-driven programming model Writing GUI applications Object oriented programming The C# language

While learning, these resources will help you: The lecture The labs The C# reference, released in parts ...but you need to practice, or all this will be

worthless Seriously :(

Page 3: C# Summer course - Lecture 1

Tools This course uses Visual C# Express edition

(which is a free download from Microsoft)− http://www.microsoft.com/express/Windows/

Page 4: C# Summer course - Lecture 1

New ideas about programming...

Event-driven programming Object oriented programming

Better for modeling Objects have state and behavior (attributes and

methods). Classes, subclasses. Interface vs. implementation.

Page 5: C# Summer course - Lecture 1

Event-driven programming

Event-driven program

main( )

{

on event1 do { … }

on event2 do { … }

on event 3 do { … }

waitForEvents( );

}

Normal program

main( )

{

doSubtask1( );

doSubtask2( );

doSubtask3( );

}

Page 6: C# Summer course - Lecture 1

Event-driven programming

Examples of events: Keyboard and mouse actions. Typing in a text box. Renaming a file. Arrival of data from the network. Adding an item to a collection. ...

Page 7: C# Summer course - Lecture 1

OOP – Modeling the problem A word processor can use objects to represent:

− Paragraphs and pages− Toolbars and dialogs− Graphical objects

Page 8: C# Summer course - Lecture 1

OOP – Modeling the problem A strategy game can use objects to represent:

− Units and buildings− Teams− Strategies and algorithms

Page 9: C# Summer course - Lecture 1

OOP – Modeling the problem A desktop operating system can use objects

to represent:− Files and folders− Hardware devices

Page 10: C# Summer course - Lecture 1

What's an Object?

An object has state and behavior. It represents state via attributes and behavior

via methods. It has an interface and an internal

implementation. Separating the interface from the implementation is encapsulation.

Multiple object types can have the same interface, thus opening the door for polymorphism.

Page 11: C# Summer course - Lecture 1

Objects and classes

Classes describe the common attributes and behavior of related objects.

You can do OOP without classes, but most OOP languages have them.

A C# class has fields (data members, member variables) and methods (member functions).

Fields and methods can be static or non-static.

− Non-static methods are also called instance methods or object methods. Same for fields.

Classes can be superclasses or subclasses. Superclasses provide abstraction over types.

Page 12: C# Summer course - Lecture 1

Let's do some programming

"X owns Y" program The bouncing head program

Page 13: C# Summer course - Lecture 1

What can be done with objects?

Creating new objects Reading and writing fields Reading and writing properties Calling methods.

Page 14: C# Summer course - Lecture 1

GUI without the designerusing System.Windows.Forms;

class Program

{

static void Main()

{

Form f;

f = new Form();

f.Text = "A simple program";

f.SetBounds(20, 20, 400, 400);

Application.Run(f); // for the main form only!!

}

}

Page 15: C# Summer course - Lecture 1

Namespaces C# (actually, .net) program elements are

organized into namespaces. To access an element in another namesoace,

either use its full name nsName.elementName or import all the names from the namespace with using ...;

To define your own namespaces, you can do this: namespace MyNS

{class ABC { ... }

class DEF { … }

}

Page 16: C# Summer course - Lecture 1

Instantiation The declaration

Form f;

Tells the compiler the type of ' f ', but it does not create a new object.

The only way to create an object is with new

f = new Form( ); New does 3 things

− Allocate memory for the object− Initialize the object− Return a reference to the object as the value of the new

expression. Note: A reference is a number that's used to access

an object. It's similar (but not the same) to pointers.

Page 17: C# Summer course - Lecture 1

Setting properties The statement

f.Text = "A simple program";

is the setting of a property (properties looks like member variables, but act like functions).

You can also set member variables with the same syntax. Member variables work mostly like C++'s structs.

Page 18: C# Summer course - Lecture 1

Setting properties / calling methods The statement

f.SetBounds(20, 20, 400, 400);

Is a method call. In this case SetBounds is an instance method since it needs an object reference ' f ' to be called.

The statement

Application.Run(f);

Is calling the static method Run defined in the class Application. It is a static method since it doesn't need an object reference; but only a class name.

Page 19: C# Summer course - Lecture 1

Terminology

Related names Element

Instance Object

Data member, Member variable, attribute Field

Member function Method

Class method Static method

Instance method, object method. Non-static method

NoteWhile the names are conceptually related, they do not necessarily have the exact same meaning.

Page 20: C# Summer course - Lecture 1

Next time...

Reference types, Arrays, foreach... Graphics Handling KB events

Remember to download the reference!