1 / 72

One of the most prevalent powerful programming languages

C#. One of the most prevalent powerful programming languages. Hi, C#. class Program { static void Main() { System.Console.Write("Hi"); } }. C# Reads C-Sharp Is a computer programming language Server Side Asp.net Client Side SilverLight Is an ECMA standard, spearheaded by MS.

abel
Download Presentation

One of the most prevalent powerful programming languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# 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. Type

  6. 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.

  7. 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

  8. 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.

  9. 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

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

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

  12. Typing

  13. Kinds of Types by declaration • enum • struct • class • interface • delegate

  14. By reference • enum • struct • class • interface • delegate • byValue • byReference

  15. 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 i a 1A3D136u 1

  16. Types vs Types • Subtype • Any instance of a is also instance of b • Association • A’s property is b

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

  18. Type def Syntax

  19. Example public enum Color{ Red, Blue, Green }

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

  21. 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

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

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

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

  25. destructor public class Student{ ~Student{ //close database, for example } } • 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

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

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

  28. 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

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

  30. 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

  31. By declaration types

  32. 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;

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

  34. 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

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

  36. 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!

  37. Instantiation

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

  39. new s=new Student();

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

  41. A console application in VS

  42. Console application • We can code different projects • Console is a simple one

  43. 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

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

  45. C# windows

  46. solution subitem project Source code opend

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

  48. Choosing Types

  49. 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.

More Related