720 likes | 818 Views
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.
E N D
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
Run on .net framework • Developed in, say, Visual Studio • For all kinds of software • Web, • Service, etc • silverlight • Windows Form,
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.
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
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.
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
Run! • Call a type/instance’method • In which other methods may be called • Data may be changed • Then stop.
Coding • So in c#, the coding is about typing and instantiating • Types first.
Kinds of Types by declaration • enum • struct • class • interface • delegate
By reference • enum • struct • class • interface • delegate • byValue • byReference
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
Types vs Types • Subtype • Any instance of a is also instance of b • Association • A’s property is b
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
Example public enum Color{ Red, Blue, Green }
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
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
members • Define: • Fields • Constructor/destructor • Methods/properties/indexer • Events • Inner types • ChessBoard • Square-used only here • Different for declaration kinds of types
Field class Student{ public string id; private DateTime dob; } //Note the type and varName
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
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
Method class Stduent{ public int age(){ //return currentTime-dob; } }
Property class Stduent{ public bool performance{ protected get{ //return grades; } private set{ //forbid } } } //property is like a method without parameter
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
Event Event is a field where a series of methods wrapped as delegates will be triggered.
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
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;
struct public struct Point{ double X; double Y; public void Translate(){ … } } //note struct has only one implicit supertype object.
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
class public class Student{ //fields //methods //properties //indexer //event //subtypes }
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!
Use your defined type to introduce a var Student s; Color c; BiOp o; Ishape shape; Point p;
new s=new Student();
Invoke members s.run(); //method, in which other instances might be created and invoked s.sex; //data
Console application • We can code different projects • Console is a simple one
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
Coding • One and only one class has a static Main method • The system will invoke this type’s Main and start the application.
C# windows
solution subitem project Source code opend
Hi, C# class Program { static void Main() { System.Console.Write("Hi"); } }
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.