C# Summer course - Lecture 1


Published on

C# summer training for FCIS, 2010. Lecture 1

Published in: Technology
1 Comment
1 Like
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C# Summer course - Lecture 1

  1. 1. Introduction to C# FCISSummer training 2010, 1st year. Mohamed Samy
  2. 2. 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 :(
  3. 3. Tools  This course uses Visual C# Express edition (which is a free download from Microsoft) − http://www.microsoft.com/express/Windows/
  4. 4. 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.
  5. 5. Event-driven programming Normal program Event-driven program main( ) main( ) { { doSubtask1( ); on event1 do { … } doSubtask2( ); on event2 do { … } doSubtask3( ); on event 3 do { … } waitForEvents( ); } }
  6. 6. 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.  ...
  7. 7. OOP – Modeling the problem  A word processor can use objects to represent: − Paragraphs and pages − Toolbars and dialogs − Graphical objects
  8. 8. OOP – Modeling the problem  A strategy game can use objects to represent: − Units and buildings − Teams − Strategies and algorithms
  9. 9. OOP – Modeling the problem  A desktop operating system can use objects to represent: − Files and folders − Hardware devices
  10. 10. Whats 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.
  11. 11. 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.
  12. 12. Lets do some programming  "X owns Y" program  The bouncing head program
  13. 13. What can be done with objects?  Creating new objects  Reading and writing fields  Reading and writing properties  Calling methods.
  14. 14. GUI without the designer using 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!! } }
  15. 15. 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 { … } }
  16. 16. 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 thats used to access an object. Its similar (but not the same) to pointers.
  17. 17. 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.
  18. 18. 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 doesnt need an object reference; but only a class name.
  19. 19. Terminology Related names ElementInstance ObjectData member, Member variable, FieldattributeMember function MethodClass method Static methodInstance method, object method. Non-static method Note While the names are conceptually related, they do not necessarily have the exact same meaning.
  20. 20. Next time...  Reference types, Arrays, foreach...  Graphics  Handling KB events  Remember to download the reference!