C# Summer course - Lecture 1
-
Upload
mohamedsamyali -
Category
Technology
-
view
1.888 -
download
0
description
Transcript of C# Summer course - Lecture 1
Introduction to C#
FCISSummer training 2010, 1st year.
Mohamed Samy
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 :(
Tools This course uses Visual C# Express edition
(which is a free download from Microsoft)− http://www.microsoft.com/express/Windows/
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.
Event-driven programming
Event-driven program
main( )
{
on event1 do { … }
on event2 do { … }
on event 3 do { … }
waitForEvents( );
}
Normal program
main( )
{
doSubtask1( );
doSubtask2( );
doSubtask3( );
}
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. ...
OOP – Modeling the problem A word processor can use objects to represent:
− Paragraphs and pages− Toolbars and dialogs− Graphical objects
OOP – Modeling the problem A strategy game can use objects to represent:
− Units and buildings− Teams− Strategies and algorithms
OOP – Modeling the problem A desktop operating system can use objects
to represent:− Files and folders− Hardware devices
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.
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.
Let's do some programming
"X owns Y" program The bouncing head program
What can be done with objects?
Creating new objects Reading and writing fields Reading and writing properties Calling methods.
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!!
}
}
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 { … }
}
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.
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.
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.
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.
Next time...
Reference types, Arrays, foreach... Graphics Handling KB events
Remember to download the reference!