(6) c sharp introduction_advanced_features_part_i

Embed Size (px)

DESCRIPTION

- Namespaces - Assemblies - Error Handling with Exceptions - Enums - Value Types and Reference Types

Citation preview

  • 1. (6) Introduction of C# Basics Advanced Features Part INico Ludwig (@ersatzteilchen)

2. 2TOC (6) Introduction of C# Advanced Features Part I Namespaces Assemblies Error Handling with Exceptions Enums Value Types and Reference Types 3. 3Namespaces .Net namespaces allow to organize types generally. .Net's core types are defined in the namespace System. A namespace reduces the risk of clashing type names. Type names can be qualified with the namespace name. E.g. the types in libraries are held in own library namespaces. Syntactical specialties: Nesting: a namespace can contain other nested namespaces (like boxes in boxes). A namespace directive imports all types of a namespace with the using keyword. Only complete namespaces can be imported. Complex namespace names (e.g. nested ones) can be aliased. The . and :: punctuators can be used to access aliased namespaces. The global:: keyword allows to re-refer the global namespace (used rarely). 4. 4Namespaces in C# Codeusing System; // A namespace directive.using SIO = System.IO; // Namespace System.IO aliased as SIO.namespace Namespaces { // A namespace definition.namespace OtherNamespace { // A nested namespace definition.public class Program{}}public class Program {public static void Main(string[] args) {System.IO.File file = null; // A fully qualified type.SIO.File file2 = null; // A via-alias qualified type.SIO::File file3 = null; // A via-alias qualified type.}}} 5. 5Assemblies Assemblies are the smallest units of security and deployment. They allow modular development and separation of common code. Assemblies are self contained. Assemblies are represented by standalone executables or dlls. Types within assemblies can be encapsulated with the access modifier internal. The types defined in a namespace can be spread among assemblies. Often a certain assembly defines its types in its own namespace as a library. But this is not obligatory. 6. 6Referencing 3rd Party Libraries into an Application We can reference libraries by referencing assemblies into the application: After the reference was added, a contained API can be accessed via its namespace.// Use the namespace as qualifier:System.Windows.Forms.Form f = new System.Windows.Forms.Form();// Open the namespace with a namespace directive:using System.Windows.Forms; 7. 7Classical Handling of Run Time Errors Syntax errors vs. logic errors vs. run time errors. Classically, after an operation was done, the result should be checked. E.g. the content of a register is checked (in an assembly language). E.g. the returned value of a function is checked. If the result indicates error, the code has to branch... If an error condition was ignored (not checked), unexpected things may happen. This can lead to undefined behavior of an application. Dilemma: Effective code and error handling code is mixed. 8. 8Classical Handling of Run Time Errors in C Codechar* chars = (char*)malloc(10);// Was memory allocation successful?if (chars){FillBuffer(chars);printf("Buffer content: %s", chars);free(chars);}else{ // Oh, memory allocation was not successful!printf("Ouch! Could not create char array!");} 9. 9Exception Handling in C# Codetry // Run time error prone code goes here:{string theText = File.ReadAllText("Text.txt"); // Might throw an Exception.string a2ndText = File.ReadAllText("Text.txt"); // Might throw an Exception.}catch(FileNotFoundException exc) // We can handle FileNotFoundExceptions:{Console.WriteLine("FileNotFoundException thrown: "+exc.Message);}catch(Exception exc) // And we can handle other .Net Exceptions:{Console.WriteLine("Exception thrown: "+exc.Message);}finally // But under all circumstances this code will be executed:{Console.WriteLine("The last word is mine!");} 10. 10Run Time Error Handling with Exceptions Exception code separates effective code from error handling code syntactically. This is done by try and catch/finally blocks. Exception objects represent error conditions, they can't be ignored, but just... "happen" (are being thrown) somewhere in the try block. And then: Alternative 1: Caught and handled in the matching catch block. Alternative 2: Ignored! - The method will be exited immediately and the exception will be forwarded. The stack of the exception will be unwound. Alternative 3: Suppressed, results in a postcondition as nothing has happened. Exceptions are types that are designed in an inheritance hierarchy. catch handlers work like "filters" for exceptions. The dynamic type of an Exception is matched against handler signatures. Hierarchical Exceptions and dynamic type checking works very intuitively. 11. 11Enums Typisized set of integral constants. Useful for discrete integral constants or flags. Known from C/C++, Pascal and Java. Used to express options and flags in various .Net APIs. System.Windows.Forms.DialogResult System.IO.FileAttributes System.Text.RegularExpressions.RegexOptions An enum type implicitly inherits from the value type System.Enum. User defined enums can not define methods. But all enums provide some operator overloads, though. 12. 12public void Foo(){// Reference type System.String:String aString = "ABC";// Assignment copies the reference, but both// references point to the same object in heap.String sndString = aString;}Memory Situation of Reference TypesHeapStackaString = 0x23452113StacksndString = 0x23452113"ABC" 13. 13Value Types Part I Reference types: All the types we have created by now are reference types. References, i.e. variables of reference type, hold only a shortcut to a location in heap. When the references are exchanged between methods, the objects won't be copied. This is call by reference: We're only passing around shortcuts to the same object. References can also refer to nothing, then they have the value null. But sometimes "lightweight types" are sufficient to represent a concept. Such as int, Point, KeyValuePair, enum etc. These lightweight types are called value types in .Net, variables are called values. C# allows specifying user defined value types, so called structs. 14. 14Value Types Part II Local values differ from references: Values are created on the stack. They are default initialized! Reference types provide overhead, which value types do not need. They have different equality semantics by default (identity versus content equality). Value types in C#: Syntactically defined like classes, but with the keyword struct. As locals, they're created on the stack, even if the keyword new is used! Can't act as subtype or base type (structs are always sealed). Derive from type System.ValueType implicitly and can implement interfaces. Have a sequential memory layout (default) and act as backdoor to Win32 PODs. In C++ we can decide where (stack or heap) an object is created during creation. In C# we have to specify the "where" on the type definition (class or struct)! 15. 15public void Bar(){// Value type System.Drawing.Point:Point aPoint = new Point(1, 2);// Assignment copies the value. Both objects// have independent copies of the value// {1, 2}. Modification modifies just one copy!Point sndPoint = aPoint;sndPoint.Y = 4;}Memory Situation of Value TypesHeap (is uninvolved)StackaPoint = {X = 1, Y = 2}StackaPoint = {X = 1, Y = 2}sndPoint = {X = 1, Y = 4} 16. 16When to use Value Types Make the creation of user defined value types an exceptional case! When to use value types: Use them for small types. .Net enums are a good example for value types. Use them to interact with legacy (Win32), i.e. exploit the sequential memory layout. Exploit default content equality semantics (i.e. "value type" equality semantics). Contra value types: Don't use them if objects are often passed/returned to/from methods (call by value). Conversion from and to reference types should be avoided (boxing/unboxing). Value types can not be used in inheritance hierarchies. Value types can not be thread locked. C#'s structs are a completely different concept compared to C++' structs. 17. 17Thank you!