Presentation is loading. Please wait.

Presentation is loading. Please wait.

One of the most prevalent powerful programming languages.

Similar presentations


Presentation on theme: "One of the most prevalent powerful programming languages."— Presentation transcript:

1 One of the most prevalent powerful programming languages

2 Hi, C# class Program { static void Main() { System.Console.Write("Hi"); }

3 C# Reads C-Sharp Is a computer programming language  Server Side  Asp.net  Client Side  SilverLight Is an ECMA standard, spearheaded by MS

4 Run on.net framework Developed in, say, Visual Studio For all kinds of software Web,  Service, etc  silverlight Windows Form,

5

6

7 Typed Every value is typed C# is strong typed, different from ES Every value is introduced after type declared The type cannot be changed all the val’s life.

8 Type before Instance Every value is an instance of a type. Many instances are abstracted by type Each instance can have their own state  Stored in each instance Their behaviors are of the same model  Stored in type.  Vary due to data of each instance

9 Type definition and instantiation In C#, We define types, the model  Behavior/events  Data storage space, field Instantiate  Create a new thing from the model  Store different values in the data field.

10 Type definition and instantiation Note, Type can have its own behavior/data, which will not be copied/referenced by instances.  Such as constant/static ones Other members can be inherited

11 Run! Call a type/instance’method In which other methods may be called Data may be changed Then stop.

12 Coding So in c#, the coding is about typing and instantiating Types first.

13

14 Kinds of Types by declaration enum struct class interface delegate

15 By reference enum struct class interface delegate byValue byReference

16 By val vs by ref By val By ref Literal in var Pointer in var Literal in another place long i=1; object a=1; 1 1 1A3D136u i a 1 1

17 Types vs Types Subtype Any instance of a is also instance of b Association A’s property is b

18 SubType Object Number Student Integer Negative Person Male Negative Integer Student Male Function Color Object always top Directional Inherit from multiple Supertype chain can be 0,1,2, … steps long. The instance of a type is also instance of any type on the supertype chain Supertype chain can be 0,1,2, … steps long. The instance of a type is also instance of any type on the supertype chain

19

20 Example public enum Color{ Red, Blue, Green }

21 Syntax Case sensitive Type’s initial is conventionally capitalized Type def comprises signature and body {static} {public/…} {abstract/sealed} TypeKeyword typename:supertype[1],…{ …//the body }

22 Signature Modifiers static No instance can be created; not inheritable. access public: Visitable everywhere private: only here internal: in this project/package protected: in the subtypes protected internal: subtypes or within package abstract No instance; subtype can sealed No inheritace

23 members Define: Fields Constructor/destructor Methods/properties/indexer Events Inner types  ChessBoard  Square-used only here Different for declaration kinds of types

24 Field class Student{ public string id; private DateTime dob; } //Note the type and varName

25 ctor To init fields No return Can take args Same name as class Types with no fields have no ctor class Student{ Sex sex; public Student(Sex sex){ this.sex=sex; }

26 destructor Clear up, e.g., release held resources such as db. No return Class name preceded by ~ Not necessary Often explicitly done by other methods like close/Finalizer/destroyer, etc public class Student{ ~Student{ //close database, for example }

27 Method class Stduent{ public int age(){ //return currentTime-dob; }

28 Property class Stduent{ public bool performance{ protected get{ //return grades; } private set{ //forbid } //property is like a method without parameter

29 Indexer public class HbueStudent{ public Student this[int x]{ //return student one by one } HbueStudent hs=new HbueStudent(); hs[0];//a student //note the keyword this. //it’s like a method

30 Event Event is a field where a series of methods wrapped as delegates will be triggered.

31 Static member public class Circle { static public double Tau=6.28; double area(){ // } //Note if a member is static, it’s a member of the type itself. //It cannot be inherited. For exmaple Disc inherit Circle’s area, but Circle.Tau insteadof Disc.Tau

32

33 enum public enum Sex{ Male, Female } //Male can be regarded as the static property of Sex, so its initial is capital. //Object is implicitly the supertype. can not inherit any ther. //By value //stored as int //used like: Sex s=Sex.Male;

34 struct public struct Point{ double X; double Y; public void Translate(){ … } //note struct has only one implicit supertype object.

35 interface interface IShape { public double area(); //… } // no field; no implementation; //implementation will be in classes subtypes //it’s a placeholder for multiple inheritance, as is impossible for class

36 class public class Student{ //fields //methods //properties //indexer //event //subtypes }

37 delegate public delegete double BiOp(double x, double y); //inherit Delegate/MultiDelegate implicitly //can only store corresponding functions BiOp a=(double x, double y)=>return x+y; BiOp b=()=>return;//wrong!

38

39 Use your defined type to introduce a var Student s; Color c; BiOp o; Ishape shape; Point p;

40 new s=new Student();

41 Invoke members s.run();//method, in which other instances might be created and invoked s.sex;//data

42

43 Console application We can code different projects Console is a simple one

44 In VS An application is a project or many projects When creating projects, select Console, select c# as the lang. One or more files/folders will be added to the project. C# source code file’s extension is.cs

45 Coding One and only one class has a static Main method The system will invoke this type’s Main and start the application.

46 C# windows

47 solution project Source code opend subitem

48 Hi, C# class Program { static void Main() { System.Console.Write("Hi"); }

49

50 struct and enum is where to hold literal values class type declaration is where to hold functions/fields Var of such type is a reference Instances hold fields delegate/interface is references The reference chain will finally lead to literal values.

51 Class vs struct reference value In most cases, the two might both do.

52 Class vs interface Having instances Single inheritance No instance Multiple inheritance Reference type

53 Why interface Multiple inheritance Conceptual, type diagram You don’t have to care much about detail

54 Class class is the most used type static void Main method of a class is the entry point of a program.

55 Type diagram In visual studio Draw diagram to illustrate the subtype relations You can also add members to types on the diagram Visual studio will generate the source code for you.

56

57 Construct An instance has all the fields along the subtype chain. Its constructor will call supertype[1]’s constructor, which in turn will call upward, and so on, till object, where constructor init fields, then return, letting the lower chain to init subtype’s fields. This process ensures supertype’s fields initiated before subtype’s constructor reference those fields in its running.

58 construct An instance’s constructor can designate which of the supertype’s constructor to be called The default one is the one with no paras.

59 destruct Destruction from bottom up.

60 virtual override A member can be overridden in subtype A{ virtual m(){} } B:A{ override m(){} } A b=new B(); b.m();//call B.m()

61 Override vs overload Overload is within a same type where methods of the same name taking different type of paras Override is About the inheritance among types Method of the same name in subtype overrun that in supertype.

62 Polymorphism For instances of the same type, but of different subtypes, Same-named methods might be different due to overriding. E.g. All animals will eat, but the ways of eating differ

63

64 Func a=(int x)=>x+1;

65

66 C# Delegates and Events Defining and using Delegates three steps:  Declaration  Instantiation  Invocation

67 C# Delegates and Events Delegate Instantiation delegate void MyDelegate(int x, int y); class MyClass { private MyDelegate myDelegate = new MyDelegate( SomeFun ); public static void SomeFun(int dx, int dy) { } static public void Main(){ myDelegate(3,5); } Type declare instantiate

68 Event

69 public delegate void FireThisEvent(); class MyEventWrapper { private event FireThisEvent fireThisEvent; public void OnSomethingHappens() { if(fireThisEvent != null) fireThisEvent(); } public event FireThisEvent Handle FireThisEvent { add { fireThisEvent += value; } remove { fireThisEvent -= value; } } //somewhere else, someone will add to the events. And when something happens, onSomethingHappes will fire all the delegate in that events.

70 […] class A{ […] void m(){} }

71 Attributes Querying Attributes [HelpUrl("http://SomeUrl/MyClass")] class Class1 {} [HelpUrl("http://SomeUrl/MyClass"), HelpUrl("http://SomeUrl/MyClass”, Tag=“ctor”)] class Class2 {} Type type = typeof(MyClass); foreach (object attr in type.GetCustomAttributes() ) { if ( attr is HelpUrlAttribute ) { HelpUrlAttribute ha = (HelpUrlAttribute) attr; myBrowser.Navigate( ha.Url ); }

72 The End Thanks


Download ppt "One of the most prevalent powerful programming languages."

Similar presentations


Ads by Google