One of the most prevalent powerful programming languages.
-
Upload
malcolm-welch -
Category
Documents
-
view
228 -
download
2
Transcript of One of the most prevalent powerful programming languages.
![Page 1: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/1.jpg)
One of the most prevalent powerful programming languages
![Page 2: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/2.jpg)
Hi, C#
class Program{
static void Main(){
System.Console.Write("Hi");
}}
![Page 3: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/3.jpg)
C#Reads C-SharpIs a computer programming language
Server Side Asp.net
Client Side SilverLight
Is an ECMA standard, spearheaded by MS
![Page 4: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/4.jpg)
Run on .net frameworkDeveloped in, say, Visual StudioFor all kinds of software
Web, Service, etc silverlight
Windows Form,
![Page 5: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/5.jpg)
![Page 6: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/6.jpg)
![Page 7: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/7.jpg)
TypedEvery value is typedC# is strong typed, different from ES
Every value is introduced after type declaredThe type cannot be changed all the val’s life.
![Page 8: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/8.jpg)
Type before InstanceEvery 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
![Page 9: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/9.jpg)
Type definition and instantiationIn 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.
![Page 10: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/10.jpg)
Type definition and instantiationNote,
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
![Page 11: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/11.jpg)
Run!Call a type/instance’method
In which other methods may be calledData may be changedThen stop.
![Page 12: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/12.jpg)
CodingSo in c#, the coding is about typing and
instantiatingTypes first.
![Page 13: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/13.jpg)
![Page 14: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/14.jpg)
Kinds of Types by declarationenumstructclassinterfacedelegate
![Page 15: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/15.jpg)
By referenceenumstruct
classinterfacedelegate
byValue
byReference
![Page 16: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/16.jpg)
By val vs by refBy val By ref
Literal in var Pointer in varLiteral in another place
long i=1; object a=1;
1 1A3D136ui a
1
![Page 17: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/17.jpg)
Types vs TypesSubtype
Any instance of a is also instance of bAssociation
A’s property is b
![Page 18: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/18.jpg)
SubTypeObject
Number
StudentInteger
Negative
Person
Male
Negative Integer
Student Male
FunctionColor
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
![Page 19: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/19.jpg)
![Page 20: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/20.jpg)
Examplepublic enum Color{
Red,Blue,Green
}
![Page 21: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/21.jpg)
Syntax
Case sensitiveType’s initial is conventionally capitalized
Type def comprises signature and body
{static} {public/…} {abstract/sealed} TypeKeyword typename:supertype[1],…{…//the body
}
![Page 22: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/22.jpg)
Signature Modifiersstatic
No instance can be created; not inheritable.access
public: Visitable everywhereprivate: only hereinternal: in this project/packageprotected: in the subtypesprotected internal: subtypes or within package
abstractNo instance; subtype can
sealedNo inheritace
![Page 23: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/23.jpg)
membersDefine:
FieldsConstructor/destructorMethods/properties/indexerEventsInner types
ChessBoard Square-used only here
Different for declaration kinds of types
![Page 24: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/24.jpg)
Fieldclass Student{
public string id;private DateTime dob;
}
//Note the type and varName
![Page 25: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/25.jpg)
ctor
To init fieldsNo returnCan take argsSame name as classTypes with no fields have no ctor
class Student{Sex sex;public Student(Sex sex){
this.sex=sex;
}}
![Page 26: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/26.jpg)
destructor
Clear up, e.g., release held resources such as db.No returnClass name preceded by ~Not necessary
Often explicitly done by other methods like close/Finalizer/destroyer, etc
public class Student{~Student{
//close database, for example}
}
![Page 27: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/27.jpg)
Methodclass Stduent{
public int age(){//return currentTime-dob;
}
}
![Page 28: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/28.jpg)
Propertyclass Stduent{
public bool performance{protected get{
//return grades;}private set{
//forbid}
}
}
//property is like a method without parameter
![Page 29: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/29.jpg)
Indexerpublic 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
![Page 30: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/30.jpg)
Event
Event is a field where a series of methods wrapped as delegates will be triggered.
![Page 31: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/31.jpg)
Static memberpublic 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
![Page 32: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/32.jpg)
![Page 33: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/33.jpg)
enumpublic 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;
![Page 34: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/34.jpg)
structpublic struct Point{
double X;double Y;public void Translate(){
…}
}
//note struct has only one implicit supertype object.
![Page 35: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/35.jpg)
interfaceinterface 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
![Page 36: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/36.jpg)
classpublic class Student{
//fields//methods//properties//indexer//event//subtypes
}
![Page 37: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/37.jpg)
delegatepublic 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!
![Page 38: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/38.jpg)
![Page 39: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/39.jpg)
Use your defined type to introduce a var
Student s;Color c;BiOp o;Ishape shape;Point p;
![Page 40: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/40.jpg)
new
s=new Student();
![Page 41: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/41.jpg)
Invoke members
s.run(); //method, in which other instances might be created and invoked
s.sex; //data
![Page 42: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/42.jpg)
![Page 43: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/43.jpg)
Console applicationWe can code different projectsConsole is a simple one
![Page 44: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/44.jpg)
In VSAn 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
![Page 45: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/45.jpg)
CodingOne and only one class has a static Main
method The system will invoke this type’s Main and
start the application.
![Page 46: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/46.jpg)
C#
windows
![Page 47: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/47.jpg)
solution
project
Source code opend
subitem
![Page 48: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/48.jpg)
Hi, C#
class Program{
static void Main(){
System.Console.Write("Hi");
}}
![Page 49: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/49.jpg)
![Page 50: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/50.jpg)
struct and enum is where to hold literal valuesclass type declaration is where to hold
functions/fieldsVar of such type is a referenceInstances hold fields
delegate/interface is references
The reference chain will finally lead to literal values.
![Page 51: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/51.jpg)
Class vs struct
reference value
In most cases, the two might both do.
![Page 52: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/52.jpg)
Class vs interface
Having instances
Single inheritance
No instance
Multiple inheritance
Reference type
![Page 53: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/53.jpg)
Why interfaceMultiple inheritanceConceptual, type diagram
You don’t have to care much about detail
![Page 54: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/54.jpg)
Classclass is the most used typestatic void Main method of a class is the entry
point of a program.
![Page 55: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/55.jpg)
Type diagramIn visual studio
Draw diagram to illustrate the subtype relationsYou can also add members to types on the
diagramVisual studio will generate the source code for
you.
![Page 56: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/56.jpg)
![Page 57: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/57.jpg)
ConstructAn 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.
![Page 58: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/58.jpg)
constructAn instance’s constructor can designate which
of the supertype’s constructor to be calledThe default one is the one with no paras.
![Page 59: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/59.jpg)
destructDestruction from bottom up.
![Page 60: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/60.jpg)
virtual overrideA member can be
overridden in subtypeA{
virtual m(){}}B:A{
override m(){}
}A b=new B();b.m(); //call B.m()
![Page 61: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/61.jpg)
Override vs overload
Overload iswithin a same type where methods of
the same name taking different type
of paras
Override isAbout the
inheritance among types
Method of the same name in subtype overrun that in supertype.
![Page 62: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/62.jpg)
PolymorphismFor 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
![Page 63: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/63.jpg)
![Page 64: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/64.jpg)
Func<int, int> a=(int x)=>x+1;
![Page 65: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/65.jpg)
![Page 66: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/66.jpg)
C# Delegates and Events
Defining and using Delegates three steps:
1. Declaration2. Instantiation3. Invocation
![Page 67: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/67.jpg)
C# Delegates and Events
Delegate Instantiationdelegate 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
![Page 68: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/68.jpg)
Event
![Page 69: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/69.jpg)
public delegate void FireThisEvent();
class MyEventWrapper{ private event FireThisEvent fireThisEvent;
public void OnSomethingHappens() { if(fireThisEvent != null) fireThisEvent(); }
public event FireThisEvent HandleFireThisEvent { 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.
![Page 70: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/70.jpg)
[…]class A{
[…]void m(){}
}
![Page 71: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/71.jpg)
AttributesQuerying 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 ); }}
![Page 72: One of the most prevalent powerful programming languages.](https://reader038.fdocuments.net/reader038/viewer/2022102818/56649ef65503460f94c09c23/html5/thumbnails/72.jpg)
The End
Thanks