SlideShare a Scribd company logo
1 
(7) Introduction of C# Basics – Advanced Features 
– Part II 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● (7) Introduction of C# – Advanced Features – Part II 
– Object-based and generic Collections 
– Delegates and Events 
– Custom Attributes 
– Reflection
3 
Collections 
● Collections are types that represent a set of other objects (elements or items). 
– Certain Collections differ in the way they manage access to their items. 
– Roughly we tell sequential from associative Collections. 
– Examples from namespace System.Collections: ArrayList, Hashtable, Queue or Stack. 
● .Net collections manage references to objects, not the objects itself. 
– Value type objects are being boxed into objects, when they are added. 
● object-based Collections are not typesafe! 
– One can easily introduce run time errors.
4 
Type Problems in Collections 
// A collection of Objects (the static type of those is System.Object). 
ArrayList aList = new ArrayList(); 
// One can add any type of Object into this collection. 
aList.Add(new Bus()); 
// If we want to access the contained object's interface, we have probably 
// to cast down to the dynamic type's interface. 
object anObject = aList[0]; 
VintageCar aVintageCar = (VintageCar)anObject; 
//... but we can fail here, e.g. if the actual dynamic type differs from the 
// assumed dynamic type. -> Bus is a System.Object, but not a VintageCar! 
// This cast will end in an InvalidCastException! 
// We'll never reach this line: 
double estimatedPrize = aVintageCar.EstimatedPrize;
5 
Generic Collections to the Rescue 
● Generic collections allow to specify the type of the managed items. 
– They live in the namespace System.Collections.Generic. 
– A type name is used as argument for a generic type. 
– The type names are specified in angle brackets: List<int> -> a list of int. 
● Arrays act also like generic collections, but employ another syntax. 
● Benefits compared to object-typed Collections: 
– With generic types the compiler can check typesafety. 
– Downcasting is not required. 
● It is also possible to create own generic types: 
– Generic classes, interfaces etc. 
– The range of allowed generic types can be controlled with constraints.
6 
Generic Collections in Action 
// A collection of VintageCars (static type). 
List<VintageCar> aList = new List<VintageCar>(42); 
// We can add any type of VintageCar into this collection. 
aList.Add(new VintageCar()); 
// We can not add Cars, this is a too common type. 
aList.Add(new Car()); // Compile time error 
// We can not add Buses, this type is no kind of VintageCar. 
aList.Add(new Bus()); // Compile time error 
// If we want to access the contained object's interface, 
// downcasting is not required, the assignment can not fail. 
VintageCar aVintageCar = aList[0]; 
double estimatedPrize = aVintageCar.EstimatedPrize;
7 
Delegates 
● Often there exist root-algorithms that call other sub-algorithms. 
– The root-algorithm is always identical. 
– Only the sub-algorithms are a matter of change. 
– Example: The root-algorithm "Sort" has a fix structure, but it calls compare-algorithms to compare objects. For each object type 
the compare-algorithm is probably different. 
● The idea of root- and sub-algorithms make up some kind of "template". 
– Root-algorithms such as "Sort" have a fix structure, the sub-algorithms vary. 
– For sub-algorithms, interfaces must be defined that fit into that "template". 
● .Net introduces the delegate concept to describe the interface of sub-algorithms in a type save manner. 
– In C/C++ we have a similar the concept with function pointers.
8 
Example of Almost identical Algorithms 
public void SortInts(List<int> elements) { 
for (int x = 0; x < elements.Count - 1; ++x) { 
for (int y = 0; y < elements.Count - 1 - x; ++y) { 
if (elements[y] > elements[y + 1]) 
{ 
Swap(y, y + 1, elements); 
} 
} 
} 
} 
public void SortStrings(List<string> elements) { 
for (int x = 0; x < elements.Count - 1; ++x) { 
for (int y = 0; y < elements.Count - 1 - x; ++y) { 
if (0 < string.Compare(elements[y], elements[y + 1])) 
{ 
Swap(y, y + 1, elements); 
} 
} 
} 
}
9 
Steps to improve the SortXXX Methods 
● The comparison expression is the only difference in the implementations! 
● Extract this sub-algorithm as method and define its signature as delegate: 
public delegate int Compare<T>(T left, T right); 
● Sort() gets passed a parameter of delegate type that represents a Compare() method implementation. 
● Within Sort() the passed delegate is invoked and its returned value is analyzed. 
– I.e. we'll pass another method as argument to the method Sort(). 
● In effect, the method Sort() is much more useful: it can sort any kinds of objects! 
– The programmer has to define a suitable Compare() method. 
– The Compare() method's identifier must be passed as argument.
10 
The same Algorithm with Delegates 
// Definition of the signature of delegate methods: 
public delegate int Compare<T>(T left, T right); 
// Definition of the "template" algorithm: 
public void Sort<T>(List<T> elements, Compare<T> cmp) 
{ 
for (int x = 0; x < elements.Count - 1; ++x) 
{ 
for (int y = 0; y < elements.Count - 1 - x; ++y) 
{ 
// Here the delegate is called: 
if (0 < cmp(elements[y], elements[y + 1])) 
{ 
Swap(y, y + 1, elements); 
} 
} 
} 
}
11 
Delegates in Action 
public static void Main(string[] args) { 
List<string> sList = new List<string>(2); 
sList.Add("World"); 
sList.Add("Hello"); 
List<int> iList = new List<int>(2); 
iList.Add(2); 
iList.Add(17); 
// Here the method identifiers are passed as arguments, the Compare- 
// operation is delegated to these methods. 
Sort<string>(sList, CompareStrings); 
Sort<int>(iList, CompareInts); 
} 
// Two methods with the signature of the delegate Compare: 
public int CompareStrings(string s1, string s2) { 
return string.Compare(s1, s2); 
} 
public int CompareInts(int i1, int i2) { 
return i1.CompareTo(i2); 
}
12 
Events 
● Sometimes it is useful to get notified, when an object's state is updated. 
– E.g. in Cars a notification when the Tank is almost empty is useful. 
● Such notifications are implemented as events in the .Net framework. 
– Syntactically events are special fields within a type that can refer to functions. 
● Observers register to events: 
– Registering to an event means to register a delegate instance to an event. 
– E.g. the Driver object can register to Car's event TankEmpty to get notified. 
– More than one observer can register to the event TankEmpty (multicasting). 
● Observed objects can raise events: 
– The event's type is a delegate that will be invoked from within the object. 
– On raising the event, arguments can be passed as well.
13 
Class Car with the Event TankEmpty 
// Define the delegate type for the event handler: 
public delegate void TankEmptyEventHandler(string info); 
public class Car { 
// Define the event TankEmpty like a field in Car: 
public event TankEmptyEventHandler TankEmpty; 
public void EngineRunning() { 
if (_tankRunningEmpty && null != TankEmpty) { 
// Raise the event: 
TankEmpty("Tank less than 5L!"); 
} 
} 
}
14 
Events in Action 
// Two methods that may act as handler for TankEmpty event instances of 
// TankEmptyEventHandler. 
public static void ReportToConsole(string info) { 
public static void Main(string[] args) { 
Car car = new Car(); // Object car is being observed here. 
// Register the two handlers (use operator -= to unregister). When the 
// event is raised these two handlers (the observers) are being called. 
car.TankEmpty += ReportToConsole; 
car.TankEmpty += DashBoardSignal; 
while (true) { 
car.EngineRunning(); 
} 
} 
Console.WriteLine(info); 
} 
public static void DashBoardSignal(string info) { 
TankEmptySignal.Set(true); 
}
15 
Custom Attributes 
● In C# .Net types and members can be annotated with C# attributes. 
– E.g.: private, sealed or readonly. 
● Custom attributes allow to add more metadata for a .Net element into IL. 
– Declarative nature: A type's behavior is modified without modifying the type itself. 
– Consumption: During run time this metadata can be read and interpreted via reflection. 
● There exist many predefined run time and compile time custom attributes. 
– Examples: FlagsAttribute, ConditionalAttribute, TestAttribute 
– How to annotate: Custom attributes can be placed via direct or targeted syntax. 
● Programmers can also define custom attributes. 
– Custom attributes have to derive from System.Attribute.
16 
Compile Time Attribute “Conditional” in Action 
public class Debug 
{ 
// The method WriteLine is annotated with ConditionalAttribute("DEBUG"): 
[System.Diagnostics.Conditional("DEBUG")] 
public static void WriteLine(string message) { /*pass */ } 
} 
private void F() 
{ 
// Due to the compile time ConditionalAttribute("DEBUG"), this method 
// call is only compiled in the "DEBUG" configuration: 
System.Diagnostics.Debug.WriteLine("Hello out there!"); 
}
17 
Reflection 
● The .Net framework allows to analyze types during run time via reflection. 
● Reflection is needed: 
– If code must handle unknown types (members, hierarchies etc.). 
● This was addressed with dynamic typing in .Net 4 as well. 
– If code has to decide upon type features. 
● E.g., if code has to analyze custom attributes. 
● Reflection is accessible via the type Type and the namespace System.Reflection. 
● Reflection should be used sparingly, because it is rather expensive. 
– Better use polymorphism and design patterns.
18 
Reflecting Type "String" 
public void Main() { 
// Getting the type of String to start reflection: 
Type theStringType = typeof(string); 
// Reflecting all custom attributes (also the inherited ones) of the type string: 
object[] customAttributes = theStringType.GetCustomAttributes(true); 
for (int i = 0; i < customAttributes.Length; ++i) { 
Attribute attribute = (Attribute)customAttributes[i]; 
Console.WriteLine(attribute); 
} 
// Reflecting all methods of the type String: 
object[] methods = theStringType.GetMethods(); 
for (int i = 0; i < methods.Length; ++i) { 
System.Reflection.MethodInfo methodInfo = (System.Reflection.MethodInfo)methods[i]; 
Console.WriteLine(methodInfo); 
} 
}
19 
C# Features not Covered in this Course 
● Operator overloading and user defined indexers. 
● Generics in depth. 
● Nullable types. 
● Iterators and yielding. 
● LINQ and Lambda expressions. 
● Dynamic typing. 
● Programming of: (Web)Services, (Web)Applications with GUI and Concurrent code.
20 
Thank you!

More Related Content

PDF
(7) c sharp introduction_advanvced_features_part_ii
PDF
Review of c_sharp2_features_part_iii
PPTX
Storage Classes and Functions
PPTX
Storage Class Specifiers in C++
PDF
Lambda Expressions in Java
PPT
Savitch ch 18
ODP
(4) collections algorithms
PPTX
Oop2011 actor presentation_stal
(7) c sharp introduction_advanvced_features_part_ii
Review of c_sharp2_features_part_iii
Storage Classes and Functions
Storage Class Specifiers in C++
Lambda Expressions in Java
Savitch ch 18
(4) collections algorithms
Oop2011 actor presentation_stal

What's hot (20)

PDF
(4) collections algorithms
PPT
Savitch Ch 18
PPTX
Qcon2011 functions rockpresentation_f_sharp
DOC
Storage classess of C progamming
PPTX
Qcon2011 functions rockpresentation_scala
PPTX
Oop2010 Scala Presentation Stal
PDF
Programming with Lambda Expressions in Java
PPTX
Storage classes in C
PPT
ParaSail
PPTX
C sharp part 001
PDF
Java 8 - Project Lambda
PPTX
Operators & Casts
PPTX
Storage class in C Language
PPT
Functions in c
PPTX
Storage classes in C
PPTX
Storage class
PPTX
Java 8 lambda
PPTX
Storage class in c
PPTX
11 lec 11 storage class
PPT
Managed Compiler
(4) collections algorithms
Savitch Ch 18
Qcon2011 functions rockpresentation_f_sharp
Storage classess of C progamming
Qcon2011 functions rockpresentation_scala
Oop2010 Scala Presentation Stal
Programming with Lambda Expressions in Java
Storage classes in C
ParaSail
C sharp part 001
Java 8 - Project Lambda
Operators & Casts
Storage class in C Language
Functions in c
Storage classes in C
Storage class
Java 8 lambda
Storage class in c
11 lec 11 storage class
Managed Compiler
Ad

Similar to (7) c sharp introduction_advanvced_features_part_ii (20)

PPT
03 oo with-c-sharp
PDF
C# quick ref (bruce 2016)
PPT
Chapter 1 Presentation
PDF
Introduction to c#
PDF
Introduction To Csharp
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Introduction to csharp
PPTX
.Net Framework 2 fundamentals
PPTX
PPT
IntroToCSharpcode.ppt
PDF
Intake 38 5 1
PDF
1204csharp
PPT
C Language fundamentals hhhhhhhhhhhh.ppt
PPT
Constructor
PPTX
C# interview
PPT
Testing for share
PPT
C# programming
PDF
Review of c_sharp2_features_part_i
PPTX
5. c sharp language overview part ii
03 oo with-c-sharp
C# quick ref (bruce 2016)
Chapter 1 Presentation
Introduction to c#
Introduction To Csharp
Introduction to csharp
Introduction to csharp
Introduction to csharp
.Net Framework 2 fundamentals
IntroToCSharpcode.ppt
Intake 38 5 1
1204csharp
C Language fundamentals hhhhhhhhhhhh.ppt
Constructor
C# interview
Testing for share
C# programming
Review of c_sharp2_features_part_i
5. c sharp language overview part ii
Ad

More from Nico Ludwig (20)

PPTX
Grundkurs fuer excel_part_v
PPTX
Grundkurs fuer excel_part_iv
PPTX
Grundkurs fuer excel_part_iii
PPTX
Grundkurs fuer excel_part_ii
PPTX
Grundkurs fuer excel_part_i
PDF
(2) gui drawing
ODP
(2) gui drawing
PDF
(1) gui history_of_interactivity
ODP
(1) gui history_of_interactivity
PDF
New c sharp4_features_part_vi
PDF
New c sharp4_features_part_v
PDF
New c sharp4_features_part_iv
ODP
New c sharp4_features_part_iii
PDF
New c sharp4_features_part_ii
PDF
New c sharp4_features_part_i
PDF
New c sharp3_features_(linq)_part_v
PDF
New c sharp3_features_(linq)_part_iv
ODP
New c sharp3_features_(linq)_part_iv
PDF
New c sharp3_features_(linq)_part_iii
PDF
New c sharp3_features_(linq)_part_ii
Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_i
(2) gui drawing
(2) gui drawing
(1) gui history_of_interactivity
(1) gui history_of_interactivity
New c sharp4_features_part_vi
New c sharp4_features_part_v
New c sharp4_features_part_iv
New c sharp4_features_part_iii
New c sharp4_features_part_ii
New c sharp4_features_part_i
New c sharp3_features_(linq)_part_v
New 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_ii

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars

(7) c sharp introduction_advanvced_features_part_ii

  • 1. 1 (7) Introduction of C# Basics – Advanced Features – Part II Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● (7) Introduction of C# – Advanced Features – Part II – Object-based and generic Collections – Delegates and Events – Custom Attributes – Reflection
  • 3. 3 Collections ● Collections are types that represent a set of other objects (elements or items). – Certain Collections differ in the way they manage access to their items. – Roughly we tell sequential from associative Collections. – Examples from namespace System.Collections: ArrayList, Hashtable, Queue or Stack. ● .Net collections manage references to objects, not the objects itself. – Value type objects are being boxed into objects, when they are added. ● object-based Collections are not typesafe! – One can easily introduce run time errors.
  • 4. 4 Type Problems in Collections // A collection of Objects (the static type of those is System.Object). ArrayList aList = new ArrayList(); // One can add any type of Object into this collection. aList.Add(new Bus()); // If we want to access the contained object's interface, we have probably // to cast down to the dynamic type's interface. object anObject = aList[0]; VintageCar aVintageCar = (VintageCar)anObject; //... but we can fail here, e.g. if the actual dynamic type differs from the // assumed dynamic type. -> Bus is a System.Object, but not a VintageCar! // This cast will end in an InvalidCastException! // We'll never reach this line: double estimatedPrize = aVintageCar.EstimatedPrize;
  • 5. 5 Generic Collections to the Rescue ● Generic collections allow to specify the type of the managed items. – They live in the namespace System.Collections.Generic. – A type name is used as argument for a generic type. – The type names are specified in angle brackets: List<int> -> a list of int. ● Arrays act also like generic collections, but employ another syntax. ● Benefits compared to object-typed Collections: – With generic types the compiler can check typesafety. – Downcasting is not required. ● It is also possible to create own generic types: – Generic classes, interfaces etc. – The range of allowed generic types can be controlled with constraints.
  • 6. 6 Generic Collections in Action // A collection of VintageCars (static type). List<VintageCar> aList = new List<VintageCar>(42); // We can add any type of VintageCar into this collection. aList.Add(new VintageCar()); // We can not add Cars, this is a too common type. aList.Add(new Car()); // Compile time error // We can not add Buses, this type is no kind of VintageCar. aList.Add(new Bus()); // Compile time error // If we want to access the contained object's interface, // downcasting is not required, the assignment can not fail. VintageCar aVintageCar = aList[0]; double estimatedPrize = aVintageCar.EstimatedPrize;
  • 7. 7 Delegates ● Often there exist root-algorithms that call other sub-algorithms. – The root-algorithm is always identical. – Only the sub-algorithms are a matter of change. – Example: The root-algorithm "Sort" has a fix structure, but it calls compare-algorithms to compare objects. For each object type the compare-algorithm is probably different. ● The idea of root- and sub-algorithms make up some kind of "template". – Root-algorithms such as "Sort" have a fix structure, the sub-algorithms vary. – For sub-algorithms, interfaces must be defined that fit into that "template". ● .Net introduces the delegate concept to describe the interface of sub-algorithms in a type save manner. – In C/C++ we have a similar the concept with function pointers.
  • 8. 8 Example of Almost identical Algorithms public void SortInts(List<int> elements) { for (int x = 0; x < elements.Count - 1; ++x) { for (int y = 0; y < elements.Count - 1 - x; ++y) { if (elements[y] > elements[y + 1]) { Swap(y, y + 1, elements); } } } } public void SortStrings(List<string> elements) { for (int x = 0; x < elements.Count - 1; ++x) { for (int y = 0; y < elements.Count - 1 - x; ++y) { if (0 < string.Compare(elements[y], elements[y + 1])) { Swap(y, y + 1, elements); } } } }
  • 9. 9 Steps to improve the SortXXX Methods ● The comparison expression is the only difference in the implementations! ● Extract this sub-algorithm as method and define its signature as delegate: public delegate int Compare<T>(T left, T right); ● Sort() gets passed a parameter of delegate type that represents a Compare() method implementation. ● Within Sort() the passed delegate is invoked and its returned value is analyzed. – I.e. we'll pass another method as argument to the method Sort(). ● In effect, the method Sort() is much more useful: it can sort any kinds of objects! – The programmer has to define a suitable Compare() method. – The Compare() method's identifier must be passed as argument.
  • 10. 10 The same Algorithm with Delegates // Definition of the signature of delegate methods: public delegate int Compare<T>(T left, T right); // Definition of the "template" algorithm: public void Sort<T>(List<T> elements, Compare<T> cmp) { for (int x = 0; x < elements.Count - 1; ++x) { for (int y = 0; y < elements.Count - 1 - x; ++y) { // Here the delegate is called: if (0 < cmp(elements[y], elements[y + 1])) { Swap(y, y + 1, elements); } } } }
  • 11. 11 Delegates in Action public static void Main(string[] args) { List<string> sList = new List<string>(2); sList.Add("World"); sList.Add("Hello"); List<int> iList = new List<int>(2); iList.Add(2); iList.Add(17); // Here the method identifiers are passed as arguments, the Compare- // operation is delegated to these methods. Sort<string>(sList, CompareStrings); Sort<int>(iList, CompareInts); } // Two methods with the signature of the delegate Compare: public int CompareStrings(string s1, string s2) { return string.Compare(s1, s2); } public int CompareInts(int i1, int i2) { return i1.CompareTo(i2); }
  • 12. 12 Events ● Sometimes it is useful to get notified, when an object's state is updated. – E.g. in Cars a notification when the Tank is almost empty is useful. ● Such notifications are implemented as events in the .Net framework. – Syntactically events are special fields within a type that can refer to functions. ● Observers register to events: – Registering to an event means to register a delegate instance to an event. – E.g. the Driver object can register to Car's event TankEmpty to get notified. – More than one observer can register to the event TankEmpty (multicasting). ● Observed objects can raise events: – The event's type is a delegate that will be invoked from within the object. – On raising the event, arguments can be passed as well.
  • 13. 13 Class Car with the Event TankEmpty // Define the delegate type for the event handler: public delegate void TankEmptyEventHandler(string info); public class Car { // Define the event TankEmpty like a field in Car: public event TankEmptyEventHandler TankEmpty; public void EngineRunning() { if (_tankRunningEmpty && null != TankEmpty) { // Raise the event: TankEmpty("Tank less than 5L!"); } } }
  • 14. 14 Events in Action // Two methods that may act as handler for TankEmpty event instances of // TankEmptyEventHandler. public static void ReportToConsole(string info) { public static void Main(string[] args) { Car car = new Car(); // Object car is being observed here. // Register the two handlers (use operator -= to unregister). When the // event is raised these two handlers (the observers) are being called. car.TankEmpty += ReportToConsole; car.TankEmpty += DashBoardSignal; while (true) { car.EngineRunning(); } } Console.WriteLine(info); } public static void DashBoardSignal(string info) { TankEmptySignal.Set(true); }
  • 15. 15 Custom Attributes ● In C# .Net types and members can be annotated with C# attributes. – E.g.: private, sealed or readonly. ● Custom attributes allow to add more metadata for a .Net element into IL. – Declarative nature: A type's behavior is modified without modifying the type itself. – Consumption: During run time this metadata can be read and interpreted via reflection. ● There exist many predefined run time and compile time custom attributes. – Examples: FlagsAttribute, ConditionalAttribute, TestAttribute – How to annotate: Custom attributes can be placed via direct or targeted syntax. ● Programmers can also define custom attributes. – Custom attributes have to derive from System.Attribute.
  • 16. 16 Compile Time Attribute “Conditional” in Action public class Debug { // The method WriteLine is annotated with ConditionalAttribute("DEBUG"): [System.Diagnostics.Conditional("DEBUG")] public static void WriteLine(string message) { /*pass */ } } private void F() { // Due to the compile time ConditionalAttribute("DEBUG"), this method // call is only compiled in the "DEBUG" configuration: System.Diagnostics.Debug.WriteLine("Hello out there!"); }
  • 17. 17 Reflection ● The .Net framework allows to analyze types during run time via reflection. ● Reflection is needed: – If code must handle unknown types (members, hierarchies etc.). ● This was addressed with dynamic typing in .Net 4 as well. – If code has to decide upon type features. ● E.g., if code has to analyze custom attributes. ● Reflection is accessible via the type Type and the namespace System.Reflection. ● Reflection should be used sparingly, because it is rather expensive. – Better use polymorphism and design patterns.
  • 18. 18 Reflecting Type "String" public void Main() { // Getting the type of String to start reflection: Type theStringType = typeof(string); // Reflecting all custom attributes (also the inherited ones) of the type string: object[] customAttributes = theStringType.GetCustomAttributes(true); for (int i = 0; i < customAttributes.Length; ++i) { Attribute attribute = (Attribute)customAttributes[i]; Console.WriteLine(attribute); } // Reflecting all methods of the type String: object[] methods = theStringType.GetMethods(); for (int i = 0; i < methods.Length; ++i) { System.Reflection.MethodInfo methodInfo = (System.Reflection.MethodInfo)methods[i]; Console.WriteLine(methodInfo); } }
  • 19. 19 C# Features not Covered in this Course ● Operator overloading and user defined indexers. ● Generics in depth. ● Nullable types. ● Iterators and yielding. ● LINQ and Lambda expressions. ● Dynamic typing. ● Programming of: (Web)Services, (Web)Applications with GUI and Concurrent code.

Editor's Notes

  • #6: Another perception of generic types/Collections: They define a structure and some or all data types in that structure may change.
  • #9: Mind that the differences between both algorithms are very small.
  • #10: The definition of a Delegate type looks a little bit like a typedef in C/C++. According Compare&amp;lt;T&amp;gt;(): What is the meaning of the T?
  • #13: Events are like interrupts. What are interrupts? Interrupts are interrupting &amp;quot;notifications&amp;quot; issued by the hardware, e.g. I/O. What are alternative techniques? Primarily &amp;quot;polling&amp;quot;. Polling means to use software to ask for new events periodically.
  • #15: The methods ReportToConsole() and DashBoardSignal() are the event handlers. Sometimes such methods are called &amp;quot;callback functions&amp;quot;. Callback functions implement what we call the &amp;quot;Hollywood principle&amp;quot;: &amp;quot;Don&amp;apos;t call us, we&amp;apos;ll call you!&amp;quot;, they are also an incarnation of the &amp;quot;Inversion of Control” paradigm.
  • #19: We want to analyze (reflect) the type System.String. (A good German word for reflection is &amp;quot;Selbstauskunft&amp;quot;.) Iterate over its custom attributes. Iterate over its methods.