1 / 180

Overview of .NET

Overview of .NET. Essential features C++ replaced by C# Simplify software development a new component model Simplify software upgrade Web services Simplify distributed computing. C#. The core language used in .NET Similar to Java, some features from C++ Main features

Download Presentation

Overview of .NET

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. Overview of .NET • Essential features • C++ replaced by C# • Simplify software development • a new component model • Simplify software upgrade • Web services • Simplify distributed computing

  2. C# • The core language used in .NET • Similar to Java, some features from C++ • Main features • C-pointer replaced by reference (no more *, &, ->) • User-defined type (class and object) • Managed heap (system deletes memory for you) • Extensive class library • (ADO.NET, ASP.NET, DirectX, Web Service) Java C++ C C#

  3. What is a component? • A component is a binary module that can be shared • Important to developers because • No need to release the source code to public • Upgrade can be done by replacing an old component with a new one at binary level System Component

  4. DLL (Dynamic Link Library) before .NET • Evolution of DLL • In C era, DLL is a library of shared functions • In C++ era, DLL is a binary component known as COM, which consists of a collection of objects • Problem • If an existing COM is replaced by a new one, the addresses of the functions will be changed • How to find the new addresses in the new COM without re-link the entire system?

  5. The problem with binary component replacement Old component 1234 foo() Old System Exec foo(), jump to address 1234

  6. Where is foo()? • Address of foo() is changed in new component • Old system doesn’t know about it and we do not want to relink the entire system • New component • xxx • foo() Old System Exec foo(), jump to address 1234

  7. COM’s solution Old System Exec foo(), - Get IUnknown - Call QueryInterface() - jump to new address • New component • xxx • foo() IUnknown QueryInterface()

  8. COM way • Lookup table approach • Get the IUnknown pointer from a known address in the new component • IUnknown points to a lookup table has a pointer to a function called QueryInterface() • Use this function to query the new addresses of the functions/objects • Convenient from the user point of view • Tedious from the developer point of view • A lot of extra coding

  9. DLL Hell • Install a new software that uses a ver 2.0 component that • The component is shared by many other software • Windows supports only one version • ver 1.0 COM is replaced by ver 2.0 • Other software are FORCED to use ver2.0 component • But these program were tested using ver1.0 • ver2.0 should be backward compatible with ver1.0 • Backward compatibility is difficult to do in practice • Software may break • The problem caused by replacing the old DLL with new DLL is known as DLL hell

  10. .NET solution • COM programming is complex, needs to manipulate the IUnknown pointer; we also have DLL hell • .NET • CLR – Common Language Run-time • Virtual machine approach (like Java’s JVM) • Source code (C#, VB, etc) are complied into byte code called MSIL (Microsoft Intermediate Language) • Byte code is mapped to executable code when it is loaded to the machine • CLR converts byte code to executable code, and fills in the actual physical addresses

  11. .NET solution • Pros • The byte code is abstract, and can be run on any platform • Because we are dealing with meta-code, CLR has the full information to resolve unknown addresses • the COM’s style of programming is obsolete • Multiple versions, side-by-side execution • .NET supports multiple versions, no more DLL hell • Con • Need to convert byte-code at loading time, therefore slower response

  12. Common Language Runtime (CLR) Common Type System Common Language Spec Overview of .NET C# VB C++ Perl Python Cobol Smalltalk … Class Library ADO.NET GUI XML/SOAP DirectX etc

  13. Advantages of .NET - simplification • Interesting features • Language interoperability • Can mix VB with C#, or any combinations !! • Because all languages are compiled into the same intermediate language (MSIL) code • Software distribution is much simplified • Resolving address references is done by CLR • The support of multiple versions • Hugh class libraries

  14. Common Language Runtime (CLR) • Provides the environment to support multiple languages, simplifies deployment and management • Loader, Just-in-time compiler, garbage collection, security, debugger, type checker, thread support, … • Common Type System (CTS) • Define the data types and programming constructs supported by the CLR • Common Language Specification (CLS) • A subset of CTS that all .NET languages must agree upon, so as to support language integration (e.g. mixing of VB and C#)

  15. Similarity with Java • Both environment are supported by virtual machine • In Java – the Java Virtual Machine (JVM) • In .Net – CLR • Java Platform, Enterprise Edition (JEE) • One language, multiple platforms • .NET • One platform, multiple languages • Open source .NET platform • Mono, DotGNU – both runs on Linux • C# is an open standard (!!)

  16. JEE vs .NET vs LAMP • JEE • Pro: system interoperability, can sells Java applications to many different platforms • Con: complex • .NET • Pro: support multiple languages integration • Con: MS platform (unless Mono provides real alternative) • LAMP (Linux/Apache/MySql/Perl/Python/PHP) • Pro: free • Con: open source, lack of support

  17. Compile and run simple C# program using System; class HelloWorld { public static void Main() { Console.WriteLine(“Hello C# world”); } } • Save: Hello.cs • Compile: csc Hello.cs • Run: Hello

  18. Output options of compiler • csc -? • Shows you the list of output options • csc /t:exe Hello.cs • Output: Hello.exe application, the default • csc /t:library Hello.cs • Output: Hello.dll assembly(a library component) • What is an assembly? • The basic unit of a binary file in .NET • Byte code from a single or multiple source files

  19. GUI version of “Hello c# world” using System; using System.Windows.Forms; //using the GUI library class HelloWorld { public static void Main() { MessageBox.Show("Hello c# world"); //a GUI box } } • Save as : HelloBox.cs • Compile: csc HelloBox.cs • Run: HelloBox • Display a box containing the message “Hello c# world”

  20. HelloApp.cs using System; class HelloWorld { public static void Main() { HelloMsg obj = new HelloMsg(); obj.Write(); } } HelloMsg.cs using System; using System.Windows.Forms; public class HelloMsg { public void Write() { MessageBox.Show ("Hello c# world"); } } Multiple source files Compile: csc HelloApp.cs HelloMsg.csRun: HelloApp

  21. To make a sharable component • To make HelloMsg.dll a sharable component • csc /t:library HelloMsg.cs • To link the component with the main program HelloApp.cs • csc /r:HelloMsg.dll HelloApp.cs • Run: HelloApp

  22. Deployment • To distribute software under .NET is a huge simplification compared to COM • Just copy the assembly files and distribute ! • To distribute a shared component is only slightly more complicated • Make the component a strong-name assembly • Add the assembly to Global Assembly Cache (GAC) so that it can be shared gacutil.exe /if HelloMsg.dll • Strong-name assembly uses public key cryptography to ensure authenticity (code is from the originator) and integrity (code has not been modified)

  23. Strong-Named Assembly • Generate a public-private key pair sn –k myKey.snk • Add code to source file to specify version and key info #if STRONG [assembly: System.Reflection.AssemblyVersion(“1.0.0.0”)] [assembly: System.Reflection.AssemblyKeyFile(“myKey.snk”)] #endif • Compile csc /define:STRONG /t:library /out:HelloMsg.dll HelloMsg.cs

  24. To protect the authenticity and integrity of the assembly • The pair of private and public keys • Locked (encrypted) the data by public key, unlocked by private key, or • Locked (encrypted) the data by private key, unlocked by public key • Private key is secret, public key is well known (contained in a digital certificate, published on Internet) • Hash the assembly and the public key into a hash code • Encrypted the hash code by the private key • Distribute the assembly, the public key and the encrypted hash code

  25. Distributed code Locked by private Key Assembly+public key Hashed Assembly+public key Unlocked by public key Hashed Hashed Assembly+public key Same? Hashed Assembly+public key

  26. Authenticity and integrity check 1) Decrypted the hash code using the public key 2) Hash the assembly and the public key • If the results are the same, then • “assembly+public” has not been changed • Otherwise the two hash codes will be different • pass integrity check • The public key is the right key in decrypting the hash key • Can check the authenticity of the public key from digital certificate

  27. Main() • *.exe must contains a Main() function • Different versions of Main() • public static void Main() • No input argument, no return value • public static void Main(string[] args) • Input arguments in a string array • public static int Main(string[] args) • Input in a string array, return an integer

  28. Class and object • A class is a definition of a user-definedtype (UDT) • Defines the variables and the methods • Does not take up memory • An object is an instance of a class • Takes up memory • Example • int x; Type Instance

  29. Class and object • intx = 10; • Invokes a construction int() to create an object called x • Data is localized and encapsulated • Information hiding, internal representation of the object is separated from outside, can change the object, e.g. number of bits per integer, (but not the methods (interface!)) without affecting the rest of the system x 10 INTERFACE + - * / int()

  30. Class and object • int is a system defined type • A class has • Attributes • The data in the object • Methods • The functions that can be used by the object • Constructors • Functions for creating the object • Constructor has the same name as the class • Use the class keyword to define your own type

  31. Example public class Student { private int ID; private string name; public Student(int ID, string name) { this.ID = ID; this.name = name; } public string getName() { return name; } } attribute constructor method

  32. The class has • two attributes (ID and name) • a constructor Student() • a method getName() • private means the thingy can be accessed (seen) only by methods within the class • Principle of encapsulation • methods should be public, attributes should be private • Since attributes are hidden, they can be changed freely (loose coupling)

  33. Constructor Student() • Create a Student object • Student s = new Student(7,”James Bond”); • Constructor Student() has the same name as the class • s is a reference (pointer) located in the stack which points to the object located in the heap Type constructor A reference to the object

  34. Function overloading • Overloading means different functions can have the same name, as long as their signatures are different • Signature = parameter list • e.g. constructors with same name but different signatures • public Student() {…} public Student(int ID, string name){…} • No need to specify the type of the return value, it must be the same as the class (Student)

  35. Function overloading • Default constructor – Student() • Default constructor takes no argument • Members in the object are initialized to their default values (0 for numbers, null for string, false for bool) • To redefine the default constructor • Write your own ! public Student() { ID = 0; //default value name = “unknown”; //default value }

  36. Other ways • If you have too many similar overloaded constructors, simplify them by the forwarding construct • public Student(string name) : this(0, name){} • public Student(int ID) : this(ID,”unknown”){} • Call is forwarded to • Student(int ID, string name)

  37. Constructor • Class is like a stamp that produces many objects • Each object has its own set of attributes • e.g. 2 Student objects created by constructor Student() • Student s1 = new Student(7,“James Bond”); Student s2 = new Student(8,“Goldfinger”); s1 s2 8 Goldfinger 7 James Bond

  38. Object identity and “this” • Object is uniquely identified by a reference pointer (e.g. s1, s2) • The reference pointer is used to uniquely identify a function or an attribute • e.g. s1.name, s2.getname() • When an object refers to its own attributes or functions, you can use the “this” pointer instead of the specific reference pointer • e.g. this.name

  39. “this” • Console.WriteLine(“name={0}”,s1.getName()); • s1.getName() invokes s1’s function getName() • The getName() in object s1 is called • The reference s1 is passed implicitly as the “this” pointer in getName() • public string getName() { return this.name; //return name; also OK } • When there is no ambiguity, “this” can be omitted

  40. Encapsulation • Don’t expose the details of an object to outside world • Why? If this information is used by outside world, you are less free to change the object in future • Attributes (data) should not be exposed (encapsulated) • private (the default) • Can only be accessed by members in the object • External world can only it via public methods • Method • private (the default), • Set to public mode only if you want to expose it

  41. Visibility • public • accessible anywhere • private • accessible only by the class (not even subclass) • protected • accessible by the class and its subclass • internal • accessible only within an assembly • protected internal • accessibly within assembly or the subclass

  42. public class A { public void foo() { B obj = new B(); obj.MethodPublic(); //OK obj.MethodPrivate(); //Not OK, private obj.MethodProtected(); //not OK, A is not a subclass of B obj.MethodInternal(); //OK if A and B are in same assembly } } public class B { public void MethodPublic(){} private void MethodPrivate(){} protected void MethodProtected(){} internal void MethodInternal(){} } Example of visibility

  43. Type (class) visibility • class also has visibility control • public or internal • Default is internal (if you don’t specify anything) • General rule • If you don’t specify the visibility explicitly, the default visibility is always the most restricted one • Method -> private • class-> internal

  44. Static method • Methods are usually at the object level • Need an object to invoke the method • e.g. s1.getName(); • Exception - static method • class level method, only one method per class • Unique, invoke using the class name instead of object • Convenient, the main reason of using static method • Not associated with any object, therefore cannot access attributes in an object • e.g. Console.WriteLine(“hello”); //WriteLine() is a static method in class Console

  45. Static data • Class level data (instead of object level data) • Unique (one and only one) copy of data per class • All objects in the same class share the static data • No ambiguity, can be accessed directly using the class name • e.g. Student.typename class Student { static private string typename= “Student”; . . . }

  46. Class level and object level’s method/data • Class A { static public void foo(); static private int x; public void bar(); private int y; } A object1 = new A(); A object2 = new A(); A object3 = new A();

  47. Class level and object level’s method/data Class level method/data Only one copy No need to identify the object e.g. A.x, A.foo() Class A int x foo() object1 object2 object3 Object level method/data Many copies Need to identify the object e.g. object1.bar() int y bar(); int y bar(); int y bar();

  48. Property • Data is usually private,retrieved only by public method • e.g. read by getXXX(); updated by setXXX() • Tedious, property provides a short-cut • public class Student { string name; public string Name { get {return name;} set {name = value;} } main() { Student s = new Student(7,”James Bond”); s.Name = “Goldfinger”; //replace setName() Console.WriteLine(“name is {0}”,s.Name); }

  49. C# reference • C/C++ pointer is error prone • int a=10; int *p; //p is a pointer p = &a; //p is the address of ‘a’ *p=20; //*p=a, deferencing • Pointer powerful but is also a major source of bugs • It can be used in two ways (address vs value) • p = the address of the data • *p = the data

  50. C# reference • We almost always use the pointer to obtain the value • Seldom use pointer to refer to the address of the value • C# and Java reference • a pointer that only refers to the value • Therefore can drop the * • i.e. an automaticdeferencingpointer without the * • Less powerful, but also less bugs • Can’t refer to the address of a variable in C# • Unless in “unsafe” mode

More Related