SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
1 
(6) Introduction of C# Basics – Advanced Features 
– Part I 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● (6) Introduction of C# – Advanced Features – Part I 
– Namespaces 
– Assemblies 
– Error Handling with Exceptions 
– Enums 
– Value Types and Reference Types
3 
Namespaces 
● .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). 
● What does that mean: types can "clash"? 
● The CLI doesn't know the concept of namespaces, instead 
it knows the concept of "DottedNames" and all type names 
must be fully qualified in the IL code, though. 
● C# also allows defining nested types as members, i.e. 
type definitions in enclosing type definitions (e.g. classes 
in classes). The syntax to access nested types is similar to 
the syntax accessing nested namespaces (only the dot 
punctuator is allowed, e.g.: OuterType.InnerType). 
● The global:: keyword is often used, when code is 
automatically generated by tools. 
● When is such code getting generated? 
● Attention: namespaces are open! - So we can spread our 
namespace among several files, which is ok so far. But we 
should not “extend” namespaces of the .Net framework 
(like System) or namespaces from 3rd party vendors! The 
types we develop at Avid should be held in a namespace, 
being named following the coding conventions.
4 
Namespaces in C# Code 
using 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. 
} 
} 
} 
● Here namespaces are used to make two equally 
names not clash. - This downright a bad design. 
The idea of namespaces is not to resolve name 
conflicts, but to organize type to make them 
better searchable.
5 
Assemblies 
● 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. 
● Types that are declared as internal are 
generally not accessible from other assemblies.
6 
Referencing 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; 
● This example shows how namespaces and 
assemblies relate.
7 
Classical 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. 
● Syntax errors are errors that are found by the 
compiler (e.g. typos in symbol names or 
keywords). 
● Logic errors are errors in the code that are not 
found by the compiler but result in wrong behavior 
of our code. 
● Run time errors are a kind of logic errors that 
result in an irrecoverable state of the program.
8 
Classical Handling of Run Time Errors in C Code 
char* 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 
Exception Handling in C# Code 
try // 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!"); 
} 
● However, if an Exception is thrown in the first line, 
the second line will not be reached at all. 
● Exceptions in comparison to return statements: 
Sometimes, if a very significant thing happens, 
we want to pass control through multiple 
functions. - Then a return statement is not 
enough and Exceptions come into play. 
● Error situations in ctors and many operators must 
be done with exceptions, because we don't have 
high flexibility on return types, however. 
● In VB the possibilities with Catch clauses are 
more elaborate: 
● An optional Exit Try can exit from a Try or Catch 
block, resuming the execution after the End Try 
block. 
● An optional When clause, being specified after 
a Catch clause, can contribute additional 
criteria that must meet together with the Catch's 
exception type, so that the handler will execute.
10 
Run 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. 
● The term Exception has nothing to do with 
frequency they "happen". 
● Exceptions may be thrown everywhere and 
every times, e.g. StackOverflowException, 
OutOfMemoryException or 
ExecutionEngineException.
11 
Enums 
● 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. 
● What does that mean "typisized set of integral 
constants"? 
● Constants that express instances of the same 
concept. E.g. the four directions would be 
represented with the constants North, East, 
South and West. Because we'll only use these 
constants' names in the code, we'll be never 
interested in their "real" integral values. A 
suitable name of that "integral set" type would 
be "Direction". 
● What are flags? 
● The idea is that a single value can represent a 
combination of values (so called flags).
12 
public 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 Types 
Heap 
Stack 
aString = 0x23452113 
Stack 
sndString = 0x23452113 
"ABC" 
● A variable of a reference type represents a 
pointer to an object on the heap. The variable is 
only a shortcut to the "real" object in the heap.
13 
Value 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<K, V>, 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. 
● Value types are further divided into simple types, 
enum types, struct types, and nullable types. 
● Reference types are further divided into class 
types, interface types, array types, and delegate 
types.
14 
Value 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)! 
● According local value objects: What is the "stack"? 
● Reference types' overhead: they do carry a vtable cataloging 
the virtual methods' addresses and a sync block index needed 
for synchronization in multithreaded scenarios. 
● Equality semantics: We can consider Reference types as 
"entity types" that have "identity continuation", whereas for 
value types we're not interested whether the value "green" is 
the same "green" as tomorrow. 
● Concerning value types can't act as a base type, there is one 
exception: actually the type System.Enum derives from 
System.ValueType and System.Enum is the base type of all 
enum types. 
● According value types in C#: What is a memory layout? 
● It defines how the fields of a type are laid out in memory for 
an instance: sequence, alignment and padding. By default, 
structs are laid out sequentially (compatible with Win32 
types) and classes are laid out automatically (the layout is 
automatically optimized for the type by the runtime). 
● What is a "POD"? Plain Old Datatype
15 
public 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 Types 
Heap (is uninvolved) 
Stack 
aPoint = {X = 1, Y = 2} 
Stack 
aPoint = {X = 1, Y = 2} 
sndPoint = {X = 1, Y = 4} 
● A variable of a value type directly represents all 
the values of the fields of the object on the stack.
16 
When 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. 
● In C++, classes vs. structs are means to control 
accessibility of contained and inherited members. 
In C#, classes vs. structs are used to define 
different sorts of types, reference types and value 
types.
17 
Thank you!

Contenu connexe

Tendances

New c sharp4_features_part_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_vNico Ludwig
 
Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)mircodotta
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developersAndrei Rinea
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNico Ludwig
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)Ron Munitz
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming IntroLou Loizides
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLou Loizides
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...AboutYouGmbH
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpMichael Stal
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
introduction to c #
introduction to c #introduction to c #
introduction to c #Sireesh K
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...Dierk König
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)Dierk König
 

Tendances (20)

New c sharp4_features_part_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_v
 
Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming Intro
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Php 7 crash course
Php 7 crash coursePhp 7 crash course
Php 7 crash course
 
C sharp
C sharpC sharp
C sharp
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
 
introduction to c #
introduction to c #introduction to c #
introduction to c #
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 

En vedette

Twapper Keeper API / UI Enhancements / Work Products
Twapper Keeper API / UI Enhancements / Work ProductsTwapper Keeper API / UI Enhancements / Work Products
Twapper Keeper API / UI Enhancements / Work ProductsJohn O'Brien III
 
Catedra upesista Reglamento estudiantil
Catedra upesista Reglamento estudiantilCatedra upesista Reglamento estudiantil
Catedra upesista Reglamento estudiantilTati Toro
 
Colorado State Address Dataset Automated Processing
Colorado State Address Dataset Automated ProcessingColorado State Address Dataset Automated Processing
Colorado State Address Dataset Automated ProcessingGeCo in the Rockies
 
Koniec Ery Informacji
Koniec Ery InformacjiKoniec Ery Informacji
Koniec Ery Informacjiekademia
 
Migrating to Flanders
Migrating to FlandersMigrating to Flanders
Migrating to Flanderslesoirbe
 
5 Options to Avoid Hand-Keying Utility Bills
5 Options to Avoid Hand-Keying Utility Bills5 Options to Avoid Hand-Keying Utility Bills
5 Options to Avoid Hand-Keying Utility BillsEnergyCAP, Inc.
 
Brent Civic Centre Launch (No Video) 15th Feb 11
Brent Civic Centre Launch (No Video)   15th Feb 11Brent Civic Centre Launch (No Video)   15th Feb 11
Brent Civic Centre Launch (No Video) 15th Feb 11Sdown
 
SCSI Express Standards Architecture
SCSI Express Standards ArchitectureSCSI Express Standards Architecture
SCSI Express Standards ArchitectureSCSIExpress
 
Tabligh e-hadayat -تبلیغ ہدایت
Tabligh e-hadayat -تبلیغ ہدایتTabligh e-hadayat -تبلیغ ہدایت
Tabligh e-hadayat -تبلیغ ہدایتmuzaffertahir9
 

En vedette (19)

Comenzar
ComenzarComenzar
Comenzar
 
Twapper Keeper API / UI Enhancements / Work Products
Twapper Keeper API / UI Enhancements / Work ProductsTwapper Keeper API / UI Enhancements / Work Products
Twapper Keeper API / UI Enhancements / Work Products
 
Catedra upesista Reglamento estudiantil
Catedra upesista Reglamento estudiantilCatedra upesista Reglamento estudiantil
Catedra upesista Reglamento estudiantil
 
Colorado State Address Dataset Automated Processing
Colorado State Address Dataset Automated ProcessingColorado State Address Dataset Automated Processing
Colorado State Address Dataset Automated Processing
 
iPas
iPasiPas
iPas
 
Koniec Ery Informacji
Koniec Ery InformacjiKoniec Ery Informacji
Koniec Ery Informacji
 
Migrating to Flanders
Migrating to FlandersMigrating to Flanders
Migrating to Flanders
 
5 Options to Avoid Hand-Keying Utility Bills
5 Options to Avoid Hand-Keying Utility Bills5 Options to Avoid Hand-Keying Utility Bills
5 Options to Avoid Hand-Keying Utility Bills
 
Brent Civic Centre Launch (No Video) 15th Feb 11
Brent Civic Centre Launch (No Video)   15th Feb 11Brent Civic Centre Launch (No Video)   15th Feb 11
Brent Civic Centre Launch (No Video) 15th Feb 11
 
Presentación1
Presentación1Presentación1
Presentación1
 
Zoe's Giant pandas-Power Point
Zoe's Giant pandas-Power PointZoe's Giant pandas-Power Point
Zoe's Giant pandas-Power Point
 
TELNET
TELNETTELNET
TELNET
 
R13 445-syn
R13 445-synR13 445-syn
R13 445-syn
 
Colour the Pictures
Colour the PicturesColour the Pictures
Colour the Pictures
 
SCSI Express Standards Architecture
SCSI Express Standards ArchitectureSCSI Express Standards Architecture
SCSI Express Standards Architecture
 
Cvpcpa Resolución 65
Cvpcpa Resolución 65Cvpcpa Resolución 65
Cvpcpa Resolución 65
 
Netwerk
NetwerkNetwerk
Netwerk
 
Tabligh e-hadayat -تبلیغ ہدایت
Tabligh e-hadayat -تبلیغ ہدایتTabligh e-hadayat -تبلیغ ہدایت
Tabligh e-hadayat -تبلیغ ہدایت
 
Froggi.voiceover
Froggi.voiceoverFroggi.voiceover
Froggi.voiceover
 

Similaire à (6) c sharp introduction_advanced_features_part_i

New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNico Ludwig
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_iiNico Ludwig
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_iiNico Ludwig
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
 
(4) c sharp introduction_object_orientation_part_i
(4) c sharp introduction_object_orientation_part_i(4) c sharp introduction_object_orientation_part_i
(4) c sharp introduction_object_orientation_part_iNico Ludwig
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
Coding style of Linux Kernel
Coding style of Linux KernelCoding style of Linux Kernel
Coding style of Linux KernelPeter Chang
 
introduction to server-side scripting
introduction to server-side scriptingintroduction to server-side scripting
introduction to server-side scriptingAmirul Shafeeq
 
Dot net interview questions and asnwers
Dot net interview questions and asnwersDot net interview questions and asnwers
Dot net interview questions and asnwerskavinilavuG
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_iNico Ludwig
 
The GO programming language
The GO programming languageThe GO programming language
The GO programming languageMarco Sabatini
 

Similaire à (6) c sharp introduction_advanced_features_part_i (20)

New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
Memory models in c#
Memory models in c#Memory models in c#
Memory models in c#
 
Core_Java_Interview.pdf
Core_Java_Interview.pdfCore_Java_Interview.pdf
Core_Java_Interview.pdf
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii
 
Java basics
Java basicsJava basics
Java basics
 
Annotations
AnnotationsAnnotations
Annotations
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
(4) c sharp introduction_object_orientation_part_i
(4) c sharp introduction_object_orientation_part_i(4) c sharp introduction_object_orientation_part_i
(4) c sharp introduction_object_orientation_part_i
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Coding style of Linux Kernel
Coding style of Linux KernelCoding style of Linux Kernel
Coding style of Linux Kernel
 
introduction to server-side scripting
introduction to server-side scriptingintroduction to server-side scripting
introduction to server-side scripting
 
Dot net interview questions and asnwers
Dot net interview questions and asnwersDot net interview questions and asnwers
Dot net interview questions and asnwers
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
 
C# - Igor Ralić
C# - Igor RalićC# - Igor Ralić
C# - Igor Ralić
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_i
 
The GO programming language
The GO programming languageThe GO programming language
The GO programming language
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 

Plus de Nico Ludwig

Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_vNico Ludwig
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivNico Ludwig
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiNico Ludwig
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiNico Ludwig
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_iNico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivityNico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivityNico Ludwig
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_viNico Ludwig
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_ivNico Ludwig
 
New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iiiNico Ludwig
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_iNico Ludwig
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNico Ludwig
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNico Ludwig
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNico Ludwig
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiNico Ludwig
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiNico Ludwig
 

Plus de Nico Ludwig (20)

Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_v
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_iv
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iii
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_ii
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_i
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_vi
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
 
New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iii
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_v
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 

Dernier

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Dernier (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

(6) c sharp introduction_advanced_features_part_i

  • 1. 1 (6) Introduction of C# Basics – Advanced Features – Part I Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● (6) Introduction of C# – Advanced Features – Part I – Namespaces – Assemblies – Error Handling with Exceptions – Enums – Value Types and Reference Types
  • 3. 3 Namespaces ● .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). ● What does that mean: types can "clash"? ● The CLI doesn't know the concept of namespaces, instead it knows the concept of "DottedNames" and all type names must be fully qualified in the IL code, though. ● C# also allows defining nested types as members, i.e. type definitions in enclosing type definitions (e.g. classes in classes). The syntax to access nested types is similar to the syntax accessing nested namespaces (only the dot punctuator is allowed, e.g.: OuterType.InnerType). ● The global:: keyword is often used, when code is automatically generated by tools. ● When is such code getting generated? ● Attention: namespaces are open! - So we can spread our namespace among several files, which is ok so far. But we should not “extend” namespaces of the .Net framework (like System) or namespaces from 3rd party vendors! The types we develop at Avid should be held in a namespace, being named following the coding conventions.
  • 4. 4 Namespaces in C# Code using 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. } } } ● Here namespaces are used to make two equally names not clash. - This downright a bad design. The idea of namespaces is not to resolve name conflicts, but to organize type to make them better searchable.
  • 5. 5 Assemblies ● 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. ● Types that are declared as internal are generally not accessible from other assemblies.
  • 6. 6 Referencing 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; ● This example shows how namespaces and assemblies relate.
  • 7. 7 Classical 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. ● Syntax errors are errors that are found by the compiler (e.g. typos in symbol names or keywords). ● Logic errors are errors in the code that are not found by the compiler but result in wrong behavior of our code. ● Run time errors are a kind of logic errors that result in an irrecoverable state of the program.
  • 8. 8 Classical Handling of Run Time Errors in C Code char* 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. 9 Exception Handling in C# Code try // 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!"); } ● However, if an Exception is thrown in the first line, the second line will not be reached at all. ● Exceptions in comparison to return statements: Sometimes, if a very significant thing happens, we want to pass control through multiple functions. - Then a return statement is not enough and Exceptions come into play. ● Error situations in ctors and many operators must be done with exceptions, because we don't have high flexibility on return types, however. ● In VB the possibilities with Catch clauses are more elaborate: ● An optional Exit Try can exit from a Try or Catch block, resuming the execution after the End Try block. ● An optional When clause, being specified after a Catch clause, can contribute additional criteria that must meet together with the Catch's exception type, so that the handler will execute.
  • 10. 10 Run 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. ● The term Exception has nothing to do with frequency they "happen". ● Exceptions may be thrown everywhere and every times, e.g. StackOverflowException, OutOfMemoryException or ExecutionEngineException.
  • 11. 11 Enums ● 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. ● What does that mean "typisized set of integral constants"? ● Constants that express instances of the same concept. E.g. the four directions would be represented with the constants North, East, South and West. Because we'll only use these constants' names in the code, we'll be never interested in their "real" integral values. A suitable name of that "integral set" type would be "Direction". ● What are flags? ● The idea is that a single value can represent a combination of values (so called flags).
  • 12. 12 public 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 Types Heap Stack aString = 0x23452113 Stack sndString = 0x23452113 "ABC" ● A variable of a reference type represents a pointer to an object on the heap. The variable is only a shortcut to the "real" object in the heap.
  • 13. 13 Value 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<K, V>, 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. ● Value types are further divided into simple types, enum types, struct types, and nullable types. ● Reference types are further divided into class types, interface types, array types, and delegate types.
  • 14. 14 Value 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)! ● According local value objects: What is the "stack"? ● Reference types' overhead: they do carry a vtable cataloging the virtual methods' addresses and a sync block index needed for synchronization in multithreaded scenarios. ● Equality semantics: We can consider Reference types as "entity types" that have "identity continuation", whereas for value types we're not interested whether the value "green" is the same "green" as tomorrow. ● Concerning value types can't act as a base type, there is one exception: actually the type System.Enum derives from System.ValueType and System.Enum is the base type of all enum types. ● According value types in C#: What is a memory layout? ● It defines how the fields of a type are laid out in memory for an instance: sequence, alignment and padding. By default, structs are laid out sequentially (compatible with Win32 types) and classes are laid out automatically (the layout is automatically optimized for the type by the runtime). ● What is a "POD"? Plain Old Datatype
  • 15. 15 public 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 Types Heap (is uninvolved) Stack aPoint = {X = 1, Y = 2} Stack aPoint = {X = 1, Y = 2} sndPoint = {X = 1, Y = 4} ● A variable of a value type directly represents all the values of the fields of the object on the stack.
  • 16. 16 When 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. ● In C++, classes vs. structs are means to control accessibility of contained and inherited members. In C#, classes vs. structs are used to define different sorts of types, reference types and value types.