2.02k likes | 2.06k Vues
Programming .ENT for ISP. Yingcai Xiao. (1) Local: CLI (Command Line Interface) GUI (Graphical User Interface) NUI (Natural User Interface) (2) Web: Server Side, Web Applications, Web Services 4-Tir Enterprise Applications, (3) Cloud:
E N D
Programming .ENT forISP Yingcai Xiao
(1) Local: CLI (Command Line Interface) GUI (Graphical User Interface) NUI (Natural User Interface) (2) Web: Server Side, Web Applications, Web Services 4-Tir Enterprise Applications, (3) Cloud: IaaS (Infrastructure as a Service): HW PaaS (Platform as a Service): OS SaaS (Software as a Service): SW Types of Programming
.NET Framework http://en.wikipedia.org/wiki/.NET_Framework
Framework Class Library (FCL) : provides standard libraries for developing common .Net applications. • Common Language Runtime (CLR ): provides the runtime environment for MSIL code. • .NET runtime environment comes with Windows • .NET development environment comes with Visual Studio .NET Framework Compositions
.Net Framework Class Library Common Language Runtime Web Services Distributed Applications Browser Accessible Remote Applications Local Applications (CLI, GUI, NUI) Other Applications (Cloud, Mobile, IoT) .NET Application Types OS
.Net Framework Class Library System Windows Web Data (Database) Enterprise Services XML (Data Description) String, … Forms (GUI) UI Services Connection DataSet XmlDocument Language Integrated Query, Windows Presentation Foundation, Windows Communication Foundation, …
Internet Information Services (IIS): web server • Commerce Server: e-commerce server • SQL Server: database server • Exchange Server: MS exchange services • Mobile Information Server: wireless server • Internet Security and Acceleration (ISA) Server: firewall, proxy, … • BizTalk: B2B (Business-to-Business) server .NET Enterprise Servers
An API • first introduced in Windows 10 • develop universal apps that run on Windows 10, Windows 10 Mobile, Xbox One and HoloLens Universal Windows Platform (UWP)
Versions of .NET http://en.wikipedia.org/wiki/.NET_Framework#mediaviewer/File:DotNet.svg
C# A .NET Programming Language
C#: (0) C^(+)^(2n); n = 0, 1, 2, => C, C++, C# (1) The most advanced programming language to date (3) OOP (Object-oriented Programming) (4) EDP (Event Driven Programming) (5) Reflection (6) Strongly typed (7) Platform-independent: code runs on any platform with .NET runtime (8) FCL (Framework Class Library) (9) Visual Studio IDE Programming Tools: C# & .NET
http://www.learncs.org/ https://www.tutorialspoint.com/csharp/ http://csharp.net-tutorials.com/basics/introduction/ http://csharp.net-tutorials.com/debugging/introduction/ https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/index http://www.completecsharptutorial.com/ https://www.javatpoint.com/c-sharp-tutorial https://www.youtube.com/watch?v=lisiwUZJXqQ http://www.tutorialsteacher.com/online-test/csharp-test C# References
C# Basics Moving from C++/Java to C#
Common Type System (CTS) • CTS defines Data Types for .NET Framework. • C# is designed with CTS in mind. • C# has the most efficient implementation of CTS.
Data Types in (CTS) System-defined Types (SDTs): Primitives (int, float, …) User-defined Types (UDTs): Classes Properties Structs Interfaces Enumerations Events Delegates Generics Templates
Common Type System (CTS) • Named Space: grouped code, used to resolve naming conflicts. namespace mine { int i=10; } namespace his { int i=20; } mine.i = his.i;
Named Space Example namespace WP1.CS.UA { class Hello { public Hello() { System.Console.WriteLine("Hello, world."); } } } namespace WP2.CS { class Hello { public Hello() { System.Console.WriteLine("Hello, again!"); } } }
Named Space Example namespace WP { class Test { public static void Main() { WP1.CS.UA.Hello mc = new WP1.CS.UA.Hello(); WP2.CS.Hello mc2 = new WP2.CS.Hello(); } } }
Classes Class: a group of code and data to be instantiated to form objects. Four categories of class members: Fields: member variables Methods: member functions Properties: fields exposed using accessor (get and set) methods Events: notifications a class is capable of firing
Example: How to define a class (user-defined data type) class Rectangle { // Fields protected int width = 1; protected int height = 1; // Methods public Rectangle () { } public Rectangle (int cx, int cy) { width = cx; height = cy; }
Example: How to define a class (user-defined data type) // Accessor Methods public void setWidth(int w) { width = w; } public int getWidth() { return width; } public void setHeight(int h) { height = h; } public int getHeight() { return height; } public int area() { int a; a = height*width; return a; } // End of Rectangle class
Example: How to use a class (user-defined data type) Rectangle rect = new Rectangle(2,4); rect.setHeight(8); rect.setWidth(rect.getWidth() * 2); double darea = (double) rect.area();
Data types describe the memory layout of objects. • The name of an object is the name of the memory space stores its data value. • For example, inti = 8; “i” is the name of the memory space for storing the data value 8. i What is Data Type?
A pointer in C++ is a memory location that stores an address. Rectangle *rect = new Rectangle (3, 4); rect 0x12345678 C++ Pointer • Dereferencing rect-> int area = rect->Area(); • Please note the notation difference between a “pointer/reference” and a “name” in this lecture.
A method is a (function) pointer that points to the code location of the method in the “text” memory, i.e., the pointer stores the address of the code location of the method in the “text” memory. Area 0x01234567 (text memory) C++ Function Pointer http://www.cs.uakron.edu/~xiao/ics-f99/fun-ptrs.html
Class Name; In CTS:“Rectangle rect” declares a reference of class Rectangle. rect “rect” is the name of a memory space that stores a reference. This is similar to Java. Instantiating a Class (in CTS & C#) A “reference” is an internal pointer, it needs to “point” to an object before being dereferenced.
Rectangle rect = new Rectange (3, 4); // Use the second constructor rect 0x12345678 References in C# • Dereferencing int area = rect.Area();
The root class of all other classes. • So, an object of any class is an “Object” So we can write: Object obj = new Rectangle (3, 4); • Constructor: Object () • String output: ToString() • Read matadata: GetType() • Clean up: Finalize() • https://msdn.microsoft.com/en-us/library/system.object%28v=vs.110%29.aspx The “Object” class
System.Object : root class for all other classes. • Every class inherits the Finalize( ) method from System.Object. • It is called just before an object is destroyed by the garbage collector of CLR. • The time of call is determined by CLR not by the program. • Use System.GC.Collect() to force a garbage collection (system wide, time consuming). • Destructor in C++ is called just before an object is destroyed by the program, when the object is freed (for heap objects) or out of scope (for stack objects). The Object Class: System.Object
More C# Features (similar to C++ or Java)
What happens if a run-time error occurs before the PC reaches the return statement in a method? And you don’t have a computed value (say, the square root of the input, or the file handle of a disk file) to return yet. Where do you place PC after returning? How can you tell the caller don’t continue on the normal execution path since there is no returning value? How can you tell the caller what went wrong? Exception Handling
Exception Handling (object-oriented event-driven runtime error handling) The CLR defines how exceptions are thrown and how they’re handled. (How exception “events” are generated and how they’re handled by exception “event” handlers). • An exception is thrown when a run-time error occurs. • You can thrown an exception any where in the code. • You can throw an exception in any language and catch it in any other. • You can throw exceptions across machines Exception Handling
CLR’s Exception Handling Mechanism: try, catch, finally, and throw File file = null; // Do not define it in the try block. Why? try { file = new File ("Readme.txt"); if(file != null) { /* Do the work here. */} … } catch (FileNotFoundException e) { Console.WriteLine (e.Message); } catch ( … ) { … } catch ( … ) { … } } finally { // Always come here even if there were exceptions. if (file != null) file.Close (); } Exception Handling
• The CLR calls the handler that most closely matches the type of exception thrown. • All of the exception types defined in the FCL are derived directly or indirectly from System.Exception. • FCL exception classes contain a Message property, which holds an error message describing what went wrong, and a StackTrace property, which details the call chain leading up to the exception. • Exception handlers can be nested. • Code in a finally block is guaranteed to execute, whether an exception is thrown or not. • An exception that is best handled by the caller rather than the callee. • Programmers can throw exceptions defined in FCL or their own exceptions derived from System.ApplicationException. if (value > 0) width = value; else throw new ArgumentOutOfRangeException ( "Width can’t be negative."); Exception Handling
Interfaces • An interface is a group of zero or more abstract methods • Abstract methods have no default implementation. • Abstract methods are to be implemented in a child class or child struct. • Subclassing an interface by a class or struct is called implementation of the interface. • An interface can be implemented but not instantiated. You can’t use an interface class to create an object. • Interfaces can also include properties and events, but no data. • An interface defines a contract between a type and users of that type. Used to define software interface standards. • All interface methods are public, no specifiers needed. • A class can implement multiple interfaces. Interfaces
interface ISecret { void Encrypt (byte[] inbuf, byte[] outbuf, Key key); void Unencrypt (byte[] inbuf, byte[] outbuf, Key key); } //no implementation, just prototyping. class Message : ISecret { public void Encrypt (byte[] inbuf, byte[] outbuf, Key key) { /* implementation here */ } public void Unencrypt(byte[] inbuf, byte[] outbuf, Key key) { /* implementation here */ } } Message msg = new Message(); // e.g. check if object msg implements interface ISecret if (msg is ISecret) { // type checking, // an object of a child type is also an object of the parent type, but not the other way around ISecret secret = (ISecret) msg; // from child to parent, explicit cast secret.Encrypt (...); } Interface Example
An abstract class is a class that can’t be instantiated, i.e., one can’t use an abstract class to create an object. • The definition of an abstract class looks like a regular class except the preceding keyword“abstract”. • It can have member fields and methods. • It can only be used as a base class for subclassing. • Its subclasses can inherit its methods as default implementation. (They can overwrite those methods too.) • It is not allowed to inherit from multiple abstract classes. Abstract Class
Both can’t be instantiated. • Both defines standardsfor their subclass to implement. • An abstract class defines the minimal implementation of its subclasses. • An interface has no implementation at all. • A child class can’t subclass from more than one abstract classes. No multiple inheritance for abstract classes. • A child class can implement more than one interfaces. Multiple inheritance allowed for interfaces. • Abstract classes and interfaces can be used together. Abstract Class vs. Interface Classes
abstract class DefaultTokenImpl { private readonly string name; public string ToString() { return name; } protected DefaultTokenImpl(string name) { this.name = name; } } interface IToken { string ToString(); } interface IVisitable { void Accept(ITokenVisitor visitor); } interface IVisitableToken : IVisitable, IToken { } class KeywordToken : DefaultTokenImpl, IVisitableToken { public KeywordToken(string name) : base(name) { } void IVisitable.Accept(ITokenVisitor visitor) { visitor.VisitKeyword(ToString());} } Abstract Class & Interface Examples
KeywordToken subclasses the abstract class DefaultTokenImpl • It also implements the interface IVisitableToken (which implements interfaces IVisitable and IToken) • It implements the Accept abstract method specified in interface IVisitable (a parent of IVisitableToken) • It inherits the default implementation of ToString from the abstract class DefaultTokenImpl to implement the ToString abstract method specified in interface IToken (the other parent of IVisitableToken). Abstract Class & Interface Examples
Generics : parameterized types According to Microsoft (http://msdn2.microsoft.com/en-us/library/512aeb7t(VS.80).aspx) • Use generic types to maximize code reuse, type safety, and performance. • The most common use of generics is to create collection classes. • The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace. • You can create your own generic interfaces, classes, methods, events and delegates. • Generic classes may be constrained to enable access to methods on particular data types. • Information on the types used in a generic data type may be obtained at run-time by means of reflection. C# and CLR Generics
Enumerations • a set of named constants • similar to enum in C++ (not defined in Java until JDK 1.5) • implicitly derives from System.Enum enum Color { Red, Green, Blue } Color.Red // Red Color.Green // Green Color.Blue // Blue Color mycolor; mycolor = Color.Green; Console.WriteLine(mycolor); // Green Enumerations
Inside Enumerations • Defining a set of named constants. • By default, it starts with 0; can be changed to any number. enum Color { Red = 10, Green, Blue } • By default, it increments by 1 for each subsequent name. • You can sign a value to each name. enum Color { Red = 10, Green = 20, Blue = 21 } • You can increment a enum variable. Color mycolor; mycolor = Color.Green; mycolor += 1; Console.WriteLine(mycolor); // Blue Enumerations
C# Features Beyond C++/Java