SlideShare a Scribd company logo
1 of 20
Review of C#2 Features – Part I 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● Review of C#2 Features – Part I 
– Separate Getter/Setter Property Access Modifiers 
– Static Classes 
– Dissection of the .Net 1 Type System 
– Generic Types 
● Sources: 
– Jeffrey Richter, Applied Microsoft .Net Framework Programming 
– Jon Skeet, CSharp in Depth
3 
Let's take a look at Properties in C#1 
private int _age; 
public int Age // Public property Age. 
{ 
● So far, so good. What if we want the setter to be only privately accessible? 
– The setter could be removed, but we don't want to loose CheckAge()'s call! 
– A special private setter function could be written, but this not the .Net way! 
● Couldn't we simply make the setter private? 
– No! That is exactly the missed feature in C#1. 
– We can do so in C#2! 
get { return _age; } 
set 
{ 
if (CheckAge(_age)) 
{ 
_age = value; 
} 
} 
}
4 
Separate getter/setter Property Access in C#2 
private int _age; 
public int Age // Public property Age with private setter. 
{ 
get { return _age; } 
private set 
{ 
} 
} 
● Just to confirm that we get this right: 
if (CheckAge(_age)) 
{ 
_age = value; 
} 
– The property's access modifier (public in this case) is applied to the getter/setter. 
– For either the getter or the setter we can declare an access type, being more restricted than the property's one. 
– This feature is available for static properties and indexers as well. 
– It is not available for event adder/remover!
5 
Static Classes 
● Often we have to create utility functions, which aren't methods in a sense. 
– We can't associate them with a specific type. 
● To manage such functions we'll write a new type with following traits: 
– All its functions are static (except a private constructor (ctor)). 
– The type derives directly from Object. 
– There’s no state at all. 
– There are no visible ctors. 
– The type is sealed if the developer remembers to do so. 
● But we have to remember well all these traits, else our type may be misused. 
● C#2 adds static classes to the language, which force all these traits.
6 
Static Classes in Action 
public static class MyUtilities // A static class. 
{ 
// Does input match against rexToMatch? 
public static bool StringMatches(string input, string rexToMatch) 
{ 
return Regex.IsMatch(input, rexToMatch); 
} 
} 
● Important features of static classes: 
– Acts implicitly abstract and sealed. 
– Can neither derive from any type, nor implement any interface. 
– Can't include non-static members, incl. ctors. 
– Can't include any operators. 
– Can have other static members (methods, fields and properties) as well. 
– Can have private static members as well.
7 
The Type System in .Net 1 (presented in C#1) 
● Explicitly typed – i.e. typenames must be explicitly written at their declaration: 
double sum = 5.2; // Type name "double" must be explicitly written for the variable sum. 
● Safely typed – i.e. unrelated types can't fool the compiler. 
– Cross casting and other pointer stuff is generally illegal. 
● Statically typed – i.e. variables' types are determined at compile time: 
int count = 42; // count is of type int, we can only perform int operations on it! 
● Sorry? But the same variable can hold different sub type objects at run time! 
– Indeed! We can define variables that may hold e.g. any derived type: 
Object o = new Form(); // Type Form is derived from type Object. 
– .Net differs the static (Object) and the dynamic type (Form) of an instance. 
– The idea of static/dynamic types is used for run time polymorphism in .Net.
8 
Lack in the .Net 1 Type System in Action 
<GenericTypes> 
Presents the lack in the .Net 1 type system with object-based 
collections.
9 
Lack in the .Net 1 Type System (esp. Collections) 
● Run time polymorphism (i.e. LSP) is the key of some .Net features: 
– 1. Cosmic hierarchy: Object is the base type of all .Net types. 
– 2. .Net 1 Collections: Hold items of static type Object, their dynamic types can vary. 
● In fact Collections must be able to hold any type in order to be versatile. 
– .Net 1 "resorted" to run time polymorphism (i.e. LSP) to get this versatility. 
– So in a .Net 1 Collection, objects of any dynamic type can be stored. 
● Object-based Collections works great, as long as... 
– we'll only store objects of dynamic types that dependent code awaits, 
– we'll only cast down to dynamic types that dependent code put in, 
– we'll never put mixed unrelated dynamic types into the same Collection, 
– or we finally use run time type checks all over, 
– well as we don't fall into a type problem during run time.
10 
What are the Problems when using Object for Versatility? 
● Valuetype objects will be boxed implicitly, which can be very time consuming. 
● We have to know about the contained dynamic types. 
– Whenever Object is used in an interface, some convention must be around. 
– This convention describes the dynamic type, we're really dealing with. 
– The contributed programers have just to obey the convention... ;-) 
● We have to cast down or unbox to the type we await. 
– The cast is needed to access the interface of a static type (e.g. Form.Focus()). 
– These casts indicate that the programer knows more than the compiler! 
– The compiler wears "cast contact lenses"; it can't type check, it trusts the programmer! 
– These casts are type checked at run time, so they are consuming run time! 
● Type errors will happen at run time, not at compile time.
11 
Fixing the Lacks in the Collection Type System 
<GenericTypes> 
Presents how to make code type safe with generic 
collections.
12 
Generics to the Rescue 
● What could we do to stay typesafe? 
– Use arrays as collections if possible, they're typesafe. 
– Resort to specialized typesafe collections, e.g. StringCollection. 
● .Net 2 introduced generic types to get rid of the presented type problems. 
– Generic types are types that leave a part of type information open. 
– Generics are an outstanding feature in .Net 2. 
● Generics leave type information open? How should this help me out? 
– The open infos are accessible by type parameters. (generic type and open type) 
– As programmers we can fill the type parameters by concrete type arguments. 
– By setting type arguments of a generic we create a type. (constructed type) 
– => Net effect: The type argument will be of static type, so the formerly open type information can be checked by the compiler. 
– Typesafety means that types are checked by the compiler.
13 
Generic Collection Types 
● .Net 1 Collection counterparts live in namespace System.Collections.Generic. 
● The generic counterpart of type ArrayList is List<T>, T is the type parameter. 
– List<T>'s interface is set to open type T in parameter- and return-types. 
public class List<T> 
{ // "sort of" unbound generic type 
public void Add(T item) { /* pass */ } 
} 
– The type parameter T acts as a placeholder for the actual type argument. 
● When a type is constructed, T is replaced by a type argument. 
– List<string>'s interface is set to static type string in parameter- and return- types. 
public class List<string> 
{ // constructed type 
public void Add(string item) { /* pass */ } 
}
14 
Constructed Types in .Net 2 
● Similarities between C++' constructed types (templates) and those in .Net 2: 
– Generic instances can be directly created from the constructed type in .Net 2. 
IList<string> names = new List<string>(); 
– Also we can define a "typedef" as closed generic type alias: 
using IntList = System.Collections.Generic.List<int>; // IntList as alias of List<int>. 
● Differences between C++' constructed (template) types and those in .Net 2: 
– We can't create explicit or partial generic specializations in .Net 2. 
– We can't apply default type arguments or objects as arguments in .Net 2. 
– List<string> refers the same constructed type in every context in .Net 2. 
● E.g. in C++ std::list<std::string> can be a different type in different executables! 
● In opposite to Java the type argument won't be erased at run time. 
– In .Net 2, the type List<string> exists at run time, not only at compile time.
15 
Using and creating new Generic Types 
● Use present generic types: There's a rich set of existing generics to be used. 
– Generic Collections (List<T>, Dictionary<T, U> etc.) 
– Generic Delegates (Action<T>, Predicate<T> etc.) 
– We should prefer these generic types over non-generic ones. 
● The type argument of a generic type, can also be a constructed generic type: 
List<List<string>> lls; // This is a list of string-lists. 
● Creating new generic types: Following .Net types can be defined generically. 
– interfaces, classes, structs, delegates and methods (including type inference). 
● Implementation of generic and non-generic interfaces may conflict. 
– If we need to implement IEnumerable<T> we also have to implement IEnumerable. 
– Conflicts must be fixed with explicit interface implementation and redirection.
16 
Type Constraints in Action 
<GenericConstraints> 
Coding a generic type with constraints.
17 
Implementing Generic Types – Part I 
● The open type argument can't be arbitrary, if we call methods on it. 
// Invalid! We can only call methods of the static type. If T is unconstrained, t's static type is Object! 
int Foo<T>(T t ) { 
return t.Count; 
} 
● This is a sort of tradeoff, but we have to constrain the static type. 
// Fine (a type constraint)! 
int Foo<T>(T t) where T : ICollection { 
return t.Count; 
} 
● Type parameters can be constrained in different ways, also overloads are allowed: 
class C<T> where T : Form {} // T must be derived from type Form (type constraint). 
class C<T, U> where T : struct where U : struct {} // T and U must be value types. 
class D<T> where T : class, new() {} // T must be a reference type with a dctor. 
● For .Net's Collection implementations the static type is generally transparent. 
– They hold any types of objects, but will only call Object's methods on them. 
– (Exceptions: e.g. equivalence-based associative collections, which require IComparable to be implemented.)
18 
Implementing Generic Types – Part II 
● To get the default value of a type parameter use the default(T) expression. 
– The default value is null for reference types. 
– The default value is 0 or false for value types (or their fields). 
● We can't call operators on "open" types' instances. 
– Only the interface of type arguments can be checked. 
– In .Net static methods/properties/fields/delegates don't belong to this interface. 
– Consequence: we can't use (mathematical) operators on "open" types!
19 
Operators can't be called on open Types' Instances 
<GenericConstraints> 
The prove: operators can't be applied.
20 
Thank you!

More Related Content

More from Nico 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_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_vNico 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_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_iiNico 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_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_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNico 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
 
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
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_iiNico Ludwig
 

More from Nico Ludwig (20)

(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_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_v
 
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_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
 
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_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_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
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
 
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
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise 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
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
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
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
[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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
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
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.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
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Review of c_sharp2_features_part_i

  • 1. Review of C#2 Features – Part I Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● Review of C#2 Features – Part I – Separate Getter/Setter Property Access Modifiers – Static Classes – Dissection of the .Net 1 Type System – Generic Types ● Sources: – Jeffrey Richter, Applied Microsoft .Net Framework Programming – Jon Skeet, CSharp in Depth
  • 3. 3 Let's take a look at Properties in C#1 private int _age; public int Age // Public property Age. { ● So far, so good. What if we want the setter to be only privately accessible? – The setter could be removed, but we don't want to loose CheckAge()'s call! – A special private setter function could be written, but this not the .Net way! ● Couldn't we simply make the setter private? – No! That is exactly the missed feature in C#1. – We can do so in C#2! get { return _age; } set { if (CheckAge(_age)) { _age = value; } } }
  • 4. 4 Separate getter/setter Property Access in C#2 private int _age; public int Age // Public property Age with private setter. { get { return _age; } private set { } } ● Just to confirm that we get this right: if (CheckAge(_age)) { _age = value; } – The property's access modifier (public in this case) is applied to the getter/setter. – For either the getter or the setter we can declare an access type, being more restricted than the property's one. – This feature is available for static properties and indexers as well. – It is not available for event adder/remover!
  • 5. 5 Static Classes ● Often we have to create utility functions, which aren't methods in a sense. – We can't associate them with a specific type. ● To manage such functions we'll write a new type with following traits: – All its functions are static (except a private constructor (ctor)). – The type derives directly from Object. – There’s no state at all. – There are no visible ctors. – The type is sealed if the developer remembers to do so. ● But we have to remember well all these traits, else our type may be misused. ● C#2 adds static classes to the language, which force all these traits.
  • 6. 6 Static Classes in Action public static class MyUtilities // A static class. { // Does input match against rexToMatch? public static bool StringMatches(string input, string rexToMatch) { return Regex.IsMatch(input, rexToMatch); } } ● Important features of static classes: – Acts implicitly abstract and sealed. – Can neither derive from any type, nor implement any interface. – Can't include non-static members, incl. ctors. – Can't include any operators. – Can have other static members (methods, fields and properties) as well. – Can have private static members as well.
  • 7. 7 The Type System in .Net 1 (presented in C#1) ● Explicitly typed – i.e. typenames must be explicitly written at their declaration: double sum = 5.2; // Type name "double" must be explicitly written for the variable sum. ● Safely typed – i.e. unrelated types can't fool the compiler. – Cross casting and other pointer stuff is generally illegal. ● Statically typed – i.e. variables' types are determined at compile time: int count = 42; // count is of type int, we can only perform int operations on it! ● Sorry? But the same variable can hold different sub type objects at run time! – Indeed! We can define variables that may hold e.g. any derived type: Object o = new Form(); // Type Form is derived from type Object. – .Net differs the static (Object) and the dynamic type (Form) of an instance. – The idea of static/dynamic types is used for run time polymorphism in .Net.
  • 8. 8 Lack in the .Net 1 Type System in Action <GenericTypes> Presents the lack in the .Net 1 type system with object-based collections.
  • 9. 9 Lack in the .Net 1 Type System (esp. Collections) ● Run time polymorphism (i.e. LSP) is the key of some .Net features: – 1. Cosmic hierarchy: Object is the base type of all .Net types. – 2. .Net 1 Collections: Hold items of static type Object, their dynamic types can vary. ● In fact Collections must be able to hold any type in order to be versatile. – .Net 1 "resorted" to run time polymorphism (i.e. LSP) to get this versatility. – So in a .Net 1 Collection, objects of any dynamic type can be stored. ● Object-based Collections works great, as long as... – we'll only store objects of dynamic types that dependent code awaits, – we'll only cast down to dynamic types that dependent code put in, – we'll never put mixed unrelated dynamic types into the same Collection, – or we finally use run time type checks all over, – well as we don't fall into a type problem during run time.
  • 10. 10 What are the Problems when using Object for Versatility? ● Valuetype objects will be boxed implicitly, which can be very time consuming. ● We have to know about the contained dynamic types. – Whenever Object is used in an interface, some convention must be around. – This convention describes the dynamic type, we're really dealing with. – The contributed programers have just to obey the convention... ;-) ● We have to cast down or unbox to the type we await. – The cast is needed to access the interface of a static type (e.g. Form.Focus()). – These casts indicate that the programer knows more than the compiler! – The compiler wears "cast contact lenses"; it can't type check, it trusts the programmer! – These casts are type checked at run time, so they are consuming run time! ● Type errors will happen at run time, not at compile time.
  • 11. 11 Fixing the Lacks in the Collection Type System <GenericTypes> Presents how to make code type safe with generic collections.
  • 12. 12 Generics to the Rescue ● What could we do to stay typesafe? – Use arrays as collections if possible, they're typesafe. – Resort to specialized typesafe collections, e.g. StringCollection. ● .Net 2 introduced generic types to get rid of the presented type problems. – Generic types are types that leave a part of type information open. – Generics are an outstanding feature in .Net 2. ● Generics leave type information open? How should this help me out? – The open infos are accessible by type parameters. (generic type and open type) – As programmers we can fill the type parameters by concrete type arguments. – By setting type arguments of a generic we create a type. (constructed type) – => Net effect: The type argument will be of static type, so the formerly open type information can be checked by the compiler. – Typesafety means that types are checked by the compiler.
  • 13. 13 Generic Collection Types ● .Net 1 Collection counterparts live in namespace System.Collections.Generic. ● The generic counterpart of type ArrayList is List<T>, T is the type parameter. – List<T>'s interface is set to open type T in parameter- and return-types. public class List<T> { // "sort of" unbound generic type public void Add(T item) { /* pass */ } } – The type parameter T acts as a placeholder for the actual type argument. ● When a type is constructed, T is replaced by a type argument. – List<string>'s interface is set to static type string in parameter- and return- types. public class List<string> { // constructed type public void Add(string item) { /* pass */ } }
  • 14. 14 Constructed Types in .Net 2 ● Similarities between C++' constructed types (templates) and those in .Net 2: – Generic instances can be directly created from the constructed type in .Net 2. IList<string> names = new List<string>(); – Also we can define a "typedef" as closed generic type alias: using IntList = System.Collections.Generic.List<int>; // IntList as alias of List<int>. ● Differences between C++' constructed (template) types and those in .Net 2: – We can't create explicit or partial generic specializations in .Net 2. – We can't apply default type arguments or objects as arguments in .Net 2. – List<string> refers the same constructed type in every context in .Net 2. ● E.g. in C++ std::list<std::string> can be a different type in different executables! ● In opposite to Java the type argument won't be erased at run time. – In .Net 2, the type List<string> exists at run time, not only at compile time.
  • 15. 15 Using and creating new Generic Types ● Use present generic types: There's a rich set of existing generics to be used. – Generic Collections (List<T>, Dictionary<T, U> etc.) – Generic Delegates (Action<T>, Predicate<T> etc.) – We should prefer these generic types over non-generic ones. ● The type argument of a generic type, can also be a constructed generic type: List<List<string>> lls; // This is a list of string-lists. ● Creating new generic types: Following .Net types can be defined generically. – interfaces, classes, structs, delegates and methods (including type inference). ● Implementation of generic and non-generic interfaces may conflict. – If we need to implement IEnumerable<T> we also have to implement IEnumerable. – Conflicts must be fixed with explicit interface implementation and redirection.
  • 16. 16 Type Constraints in Action <GenericConstraints> Coding a generic type with constraints.
  • 17. 17 Implementing Generic Types – Part I ● The open type argument can't be arbitrary, if we call methods on it. // Invalid! We can only call methods of the static type. If T is unconstrained, t's static type is Object! int Foo<T>(T t ) { return t.Count; } ● This is a sort of tradeoff, but we have to constrain the static type. // Fine (a type constraint)! int Foo<T>(T t) where T : ICollection { return t.Count; } ● Type parameters can be constrained in different ways, also overloads are allowed: class C<T> where T : Form {} // T must be derived from type Form (type constraint). class C<T, U> where T : struct where U : struct {} // T and U must be value types. class D<T> where T : class, new() {} // T must be a reference type with a dctor. ● For .Net's Collection implementations the static type is generally transparent. – They hold any types of objects, but will only call Object's methods on them. – (Exceptions: e.g. equivalence-based associative collections, which require IComparable to be implemented.)
  • 18. 18 Implementing Generic Types – Part II ● To get the default value of a type parameter use the default(T) expression. – The default value is null for reference types. – The default value is 0 or false for value types (or their fields). ● We can't call operators on "open" types' instances. – Only the interface of type arguments can be checked. – In .Net static methods/properties/fields/delegates don't belong to this interface. – Consequence: we can't use (mathematical) operators on "open" types!
  • 19. 19 Operators can't be called on open Types' Instances <GenericConstraints> The prove: operators can't be applied.

Editor's Notes

  1. In VB the behavior of static classes is available as Modules. static nested class in Java nothing to do with static classes in C#.
  2. Before we talk about generic types, let&amp;apos;s review the type system of .Net 1. Pointer tricks can be done in unsafe contexts in C#.
  3. Some definitions of strong typing wouldn&amp;apos;t consider C# being a strongly typed language, because it allows type conversions (e.g. casts) to happen. In strongly typed languages type conversions are not present, but sophisticated type inference makes the type system easy to use (e.g. Haskell).
  4. Object based collections are sometimes called &amp;quot;weakly typed collections&amp;quot;.
  5. Another perception of generic types: They define a structure and some or all data types in that structure may change. The most important details of unbound, open and closed types are discussed in the attached examples.
  6. We should use these generic collections on a regular basis, if we start a new project. If we want to replace old style collections by their generic counterparts in existing code, be sure to have unit tests in place testing this refactored code. The problem is that some of the generic types have a slightly different behavior than their old style counterparts. (E.g. if we try to get a value for an unknown key in a Hashtable (with the indexer), null will be returned, but if we try the same (indexer) on a Dictionary&amp;lt;T, U&amp;gt; a KeyNotFoundException will be thrown.) As a matter of fact arrays have been updated to be based on generics in .Net 2 as well.
  7. Here we discuss the difference of .Net&amp;apos;s approach to generic types compared to C++ and Java. C++ templates are something like a sophisticated macro mechanism (before template where in avail in C++, macros were used to mimic their behavior). C++ creates a new type per template instance at compile time, specialized types result from this procedure. Notice that we have to write the fully qualified namespace of the generic type to be aliased. This is valid for .Net 2 at run time: typeof(List&amp;lt;int&amp;gt;) != typeof(List&amp;lt;string&amp;gt;). In Java&amp;apos;s run time it would result to: List.class == List.class due to the type erasure, the argument type was erased (type erasure) and the raw type List remained. Constructed types are created at run time (i.e. They are not present in the IL code), when they are needed. Only the &amp;quot;unbound generic type&amp;quot; (not an official term for type declarations) exists at compile time. In opposite to C++ (template instances) and Java (type erasure), .Net generics remain generic types at run time. In C# we can&amp;apos;t use the type void as generic type argument (this is allowed in C++ and Java (as java.lang.Void)).
  8. We can not use ref/out parameter types for generic (esp. delegate) types.
  9. C++ does also provide a way to constraint templates: static_asserts. C#&amp;apos;s constraints are also similar to &amp;quot;concepts&amp;quot;, a feature planned for future C++ standards. In Java, generics can be constrained with so called &amp;quot;bounds&amp;quot;. In C# we have four kinds of constraints: Reference type constraints and value type constraints that must be the first being declared. Ctor type constraints must be the last ones. In between we have the conversion type constraints (identity, reference and boxing conversion, no user conversion); a type argument must then fulfill all conversion type constraints. In conversion type constraints we can only have one class (as our type can only inherit one class), but multiple interfaces a type argument needs to implement/inherit. We can not use type constraints for value types, sealed types or for following &amp;quot;special&amp;quot; types: System.Object, System.Enum, System.ValueType or System.Delegate.
  10. The operators != and == can be used if the type is constrained to be a reference type. But then only the references will be compared. If the types are further constrained to derive from a special reference type the overloads of == and != of exactly that derived type will be called. It is possible to use various operators with the advent of .Net 4/C#4 and dynamic typing. Also, we can not define generic ctors (C++ allows the definition of template ctors for type-conversion scenarios).