SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
1 
New C#3 Features (LINQ) – Part II 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● New C#3 Features (LINQ) – Part II 
– Implicit Typing 
– Lambda Expressions 
– Extension Methods 
● Sources: 
– Jon Skeet, CSharp in Depth 
– Bill Wagner, Effective C# 
– Bill Wagner, More Effective C#
3 
Local Type Inference Examples 
<TypeInferenceExamples> 
Presents local type inference.
4 
Implicit Typing of local Variables 
// Initialization of a local 
// variable in C#1/C#2: 
string someData = "a string"; 
● Explicit typing of local variables is no longer required! 
– Explicit type names can be replaced by the contextual keyword var on initializations. 
● But implicitly typed variables are still statically and strongly typed! 
– They are statically and immutably typed after initialization. 
– Their static type is inferred by the compiler, it analyzes the initializer expression. 
– Only the interface of the static type can be used on them. 
– So they do not function like the type "Variant" in COM or VB 6 or var in JavaScript! 
– (Implicitly typed constants are not available.) 
// New in C#3: declare someData as 
// implicitly typed local variable: 
var someData = "a string"; 
● The keyword var is similar to C++11's "additional 
meaning" of the auto keyword.
5 
Details about implicit Typing 
● The initializer expression needs not to be a constant or literal. 
– In C#3 there exist situations, in which the static type is unknown or anonymous. 
– These anonymous types are an important feature of LINQ in C#3! 
● The compiler is not able to infer all types. 
– It must be an initialization expression (inferred is the type of the expression right from =). 
– It must be only one variable being declared. 
– The compiler cannot infer the type of anonymous functions or method groups (as they are typeless expressions). 
– The initializer must not be a null literal w/o cast (also a typeless expression). 
● Type inference to constant symbols (a syntax likeconst instead of var i.e. "const inference"), is not supported. 
– (It wouldn't be very useful, because C# only allows compile time constants of primitive types.) 
● Problems with the var keyword in practical usage: 
– It can make code more difficult to read; declaration and usage should be near. 
– Its usability is much depending on the code editor's quality (e.g. IntelliSense).
6 
Pros and Cons of implicit Typing 
● Pros 
– The compiler can often infer the most efficient type (esp. for LINQ results). 
– Implicitly typed variables must be initialized, which may reduce coding errors. 
– It can improve readability, when the inferred type is obvious. 
– Less "using-namespace-directives" are needed. 
– Code can be easier modified. 
● Cons 
– Can be misunderstood by VB/COM/JavaScript developers. (No duck typing here!) 
– If the right hand side expression is complex, developers can't infer the type ;). 
– There are situations, when you are simply forced to use them. 
– Sometimes inference is the best solution, but it is not obvious why this the case. 
● When in doubt mind that code is more often read than written! 
● Another con: The var keyword hurts the interface 
segregation principle (after SOLID). - It enforces 
a style, where on the left hand side of the 
assignment a non-interface type, but a very 
concrete type is inferred. 
● A further con: confusion. Confusingly the var 
keyword is another way to express implicitly 
typed variables in C# (other ways: the let clause, 
the properties of anonymous types and 
arguments of generic methods).
7 
Lambda Expressions first Example 
<LambdaExpressionExamples> 
fShows how lambda expressions can express Delegate 
instances.
8 
Lambda Expressions as anonymous Functions 
// Classical usage of an anonymous 
// method to instantiate a Delegate: 
Predicate<int> isEven = delegate(int i) 
{ 
return 0 == (i % 2); 
}; 
// New in C#3: usage of a lambda 
// expression to instantiate a Delegate: 
Predicate<int> isEven = i => 0 == (i % 2); 
● Lambda expressions (lambdas) are an evolution of anonymous methods. 
– Anonymous methods and lambdas can be generalized to anonymous functions. 
– (Both allow to pass code as argument to another method.) 
– You can use expression lambdas and statement lambdas. 
– (Anonymous iterator blocks are not supported.) 
– You can generate expression trees from lambdas. 
– As a block of code lambdas can be debugged! 
– During run time the "names" of these anonymous functions are obfuscated to unspeakable compiler-generated symbols. 
● The operator => is called "lambda operator" or 
"big arrow" or "rocket" (taken from Ruby's 
"hashrocket" operator) and can be read as "goes 
to". 
● In ECMAScript's/JavaScript's function literals (var 
f = function(x) { return x * x; }) are equivalent to 
C#'s lambdas. - So in other words, JavaScript has 
lambdas for ages. 
● Anonymous functions can not be iterator blocks. 
It was counted to be too much effort for the C# 
compiler team. 
● Only top level methods can be iterator blocks. 
● Anonymous iterator blocks are supported in 
VB11! 
● The unspeakable symbols are then visible in 
stack-traces and in reverse-engineered code.
9 
Lambdas in everyday Usage 
<LambdaExpressionExamples> 
Shows some everyday usage examples of lambda expressions.
10 
Lambdas as Delegate Instances for everyday usage 
● Whenever anonymous methods have been used, lambdas can be used now. 
– The compiler converts lambdas to Delegate types by type inference respectively. 
– So the utility methods on Collections (e.g. FindAll()) can deal with lambdas as well. 
– Functions can have other functions as parameter or result => higher-order functions. 
– The types Predicate<T>/Action/<T> and the new type Func<T, R> can be used. 
● Lambdas can also be used for event handler code. 
● If lambdas refer local or other variables, these variables are gettingcaptured. 
– (I.e. lambdas lexically bind the enclosing scope's locals and the this reference.) 
– Captured variables can be modified within the capturing lambda! 
– The lifetime of captured locals is extended! Captured references can't be garbage collected (gc'd), as long as the Delegate 
instance (backing the lambda) wasn't gc'd! 
● Partial application and currying are not directly supported but can be simulated. 
● From within anonymous methods you can refer the enclosing 
scope's locals and also the this reference in instance methods of 
reference types. So in anonymous methods locals and the this 
reference are bound lexically. (In opposite to dynamic binding 
(binding variables where a function is called and not where it is 
declared) as, e.g., in JavaScript.) 
● The details of garbage collection of captured variables have been 
discussed in the C#2 review. 
● Partial application and currying: 
● Partial application: Bind a function to variable or constant 
arguments, so that a new function will be returned that accepts 
less arguments than the original function. - It can be simulated 
by creating a new Delegate instance from a call of the bound 
function with a constant argument. 
● Currying (named after the american mathematician Haskell 
Brooks Curry)/Schönfinkeln (named after the Russian 
mathematician Moses Ilyich Schönfinkel): Slash a function 
having multiple parameters into a function having only one 
parameter, but returning a chain of functions accepting the 
remaining parameters of the original function. - It can be 
simulated by cascading anonymous functions collecting the 
parameters and finally calling the original function. - In short a 
function having some parameters is converted into a function, 
which accepts less parameters by binding arguments to 
parameters.
11 
Expression Tree Example 
<LambdaExpressionExamples> 
Shows the basic usage of expression trees.
12 
Lambda Expressions for Expression Trees 
● Expression trees allow to treat code as analyzable and synthesizable data. 
– It's not as hidden as IL code. 
– This is a way of meta programming. 
● .Net 3.5 has a built-in support to compile lambdas into expression trees. 
– Mind to use the namespace System.Linq.Expressions! 
● This is the cornerstone of LINQ to SQL to handle code as a tree of objects. 
– 1. Lambdas are parsed to expression trees. 
– 2. Query providers try to translate the expression tree to SQL (e.g. T-SQL). 
– 3. The SQL is transmitted to the database engine that executes the SQL. 
● Expression trees can also be used to solve other problems. 
– E.g. you can code own query providers to exploit expression trees. 
● Expression trees can be created from lambda 
expressions, but not from anonymous methods. 
● Notice that we have used the type Expression 
instead of the delegate types Action or Func. 
Expression objects can be compiled, serialized 
and passed to remote processes. These features 
are important as they allow expression trees to be 
used with IQueryable<T>, i.e. against remote 
databases.
13 
Extension Methods Example 
f<ExtensionMethodsExamples> 
Shows the evolution from static utility methods to extension 
methods.
14 
Extension Methods 
● Sometimes you'd like to extend the set of methods a type provides. 
– But you can't do so because the type can't be inherited or is too abstract (interface). 
– Or changing a type (e.g. an interface) would break the code of present implementors. 
– Or inheritance is inappropriate: neither behavioral changes nor new fields are needed. 
– And you just have to use that type's public interface to create the extension. 
● When these kinds of desires arise you'll often end up with static utility methods. 
● The problem with static methods is that they don't belong to the extended type. 
– Neither C# nor Visual Studio will help you here. 
– You'll have to call and recall static methods like global functions with the "dot notation". 
● Promote static utility methods to extension methods to use them like members. 
– This will solve the discussed problems. 
– With extension methods the type looks like it was extended, but it was not modified.
15 
Pervasive Extension Methods – Example 
f<ExtensionMethodsExamples> 
Shows the pervasiveness of extension methods.
16 
The Implementation Site of Extension Methods 
● Extension methods must be defined in top level static classes. 
– The leftmost parameter must be of the being-extended type, having the this keyword as prefix. 
– The defining class is sometimes called "sponsor class". 
● Any .Net type can be extended. 
– E.g. sealed, "concrete" and abstract types (including interfaces). 
– Unbound and constructed generic types. 
– If an ext. method adds a new overload the respective method group will be extended. 
● The extension methods itself: 
– The public members of the extended type can be accessed via the "this parameter". 
– Other parameters than the "this parameter" can be used also. 
– Multiple overloads can be defined as well. 
– A result can be returned to the caller or not. 
– There are no extension properties (indexers), constructors, events or operators. 
● Top level static classes means not in nested static classes. 
● You can extend multiple different types in the same 
static class (as it is done here), but this should be 
considered a bad practice. You should create one static 
class per type you are about to extend. 
● In C# you can only extend types, not specific objects (but 
in Ruby both is possible). 
● A method group is the set of all methods found by a 
member lookup of a certain method name in a certain 
context. E.g. all overloads of a method being effectively 
present due to inheritance from multiple types or extension 
methods are part of that method's method group. Ctors do 
not build a method group! 
● Extension methods can only access public members of 
the extended type; i.e. they are no "friends" as known from 
C++! 
● If the "this-parameter" is null, then an 
ArgumentNullException should be thrown, because the 
"this-parameter" could also be passed explicitly (prefix 
notation).
17 
The Call Site of Extension Methods 
● The namespace of the static class defining the extension methods must be used. 
– That static class could be in the same namespace as the calling entity. 
– If not, a using directive to the namespace of that class is needed! 
● Calling extension methods. 
– They can be used with the period/pointer operator (i.e. "infix") on all instances directly. 
– (Infix: an instance of the extended type won't be passed as first argument.) 
– (The name of the static class defining the extension methods is never used then.) 
● Extension methods are pervasive on related types. 
– This means that they add candidates for method name resolution. 
– 1. Extension methods of interfaces are available for the implementing types as well. 
– 2. Extension methods of base types will extend sub types as well. 
● The name of the class in which the extension 
method is defined is irrelevant! 
● When there exist extension methods of equal 
signature in different namespaces, these 
extension methods may clash if their 
namespaces are used in the same code file. 
● The infix notation of extension methods allows to 
call them in a chained manner, then they are 
written in the order they are being executed. 
● If the "this-parameter" is null, then an 
ArgumentNullException should be thrown, 
because the "this- parameter" could also be 
passed explicitly (prefix notation).
18 
Name Resolution and Binding of Extension Methods 
● Name resolution of extension methods with the same logical signature. 
– (The namespace of the class defining the ext. methods must be visible!) 
– They are compile time (i.e. statically) bound. 
– 1. Extension methods on interfaces overrule implemented instance methods. 
– 2. Extension methods on classes overrule, when they have a better matching signature. 
– 3. Extension methods on classes can't overrule instance methods of identical signature. 
– 4. Extension methods on classes can't overrule instance methods having an implicit conversion. 
– 5. Extension methods on classes can't overrule generic instance methods (best match). 
● Binding to generic types. 
– Extension methods can be defined for an unbound generic type. (IList<T>) 
– Extension methods can be defined for a constructed generic type. (IList<int>) 
– Extension methods of IEnumerable<T>s are an easy way to extend LINQ.
19 
Extension Methods as Type Augmentation 
● This slide describes how extension methods could be applied to augment types. 
● 1. Define the minimal interface as .Net interface. 
– Or leave the present interfaces and classes in your API as they are. 
● 2. Create augmentation methods as extension methods on that interface. 
● 3. Now programmers can decide to opt in the extension methods. 
– They just need to add a using directive to make the static class visible. 
– Then the extension methods are visible as well. 
● (4. Eventually the extension methods could be promoted to instance methods.) 
● Notice that the visibility of extension methods is 
controlled by using respective namespaces. 
● Indeed extension methods allow extending a 
type's interface w/o breaking the code of present 
consumers. Java 8 introduced so called defender 
or default methods, which can be fully 
implemented (i.e. with a function body) in an 
interface definition; default methods do not break 
an interface's contract, but can be overridden in 
implementing types. Default methods make use 
of the template method design pattern.
20 
Final Words on Extension Methods 
● Extension methods aren't a new concept. 
– Similar to Obj-C's categories and slightly similar to mixins in Ruby, CLOS, D etc. 
● Underneath the compile time custom attribute "ExtensionAttribute" is applied. 
– E.g. in VB System.Runtime.CompilerServices.ExtensionAttribute must be used. 
– From .Net 2 code, extension methods can be called with the old prefix period notation. 
● Problems with extension methods in practical usage: 
– Extension methods can not be found by reflection on the extended type. 
– Its pervasiveness on name resolution can lead to unexpected results. 
– A type seems to have a varying interface, this is confusing esp. for beginners. 
– Programmers can't tell instance methods from extension methods by the bare code. 
– Its usability is much depending on the code editor's quality (e.g. IntelliSense). 
– => Can be critical in usage, because code is more often read than written! 
● The idea of a mixin is to define a part of a new 
type by including a bundle of predefined functions. 
And this bundle is called mixin. 
● Ruby has also so-called open classes, but this 
can not be simulated with C#'s extension 
methods.
21 
Thank you!

Weitere ähnliche Inhalte

Was ist angesagt?

The GO programming language
The GO programming languageThe GO programming language
The GO programming languageMarco Sabatini
 
(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
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dartImran Qasim
 
JDT embraces lambda expressions
JDT embraces lambda expressionsJDT embraces lambda expressions
JDT embraces lambda expressionsEclipse Day India
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming languageAbhishek Soni
 
ParaSail
ParaSail  ParaSail
ParaSail AdaCore
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_iNico Ludwig
 
Learn C# Programming - Variables & Constants
Learn C# Programming - Variables & ConstantsLearn C# Programming - Variables & Constants
Learn C# Programming - Variables & ConstantsEng Teong Cheah
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageRakesh Roshan
 
[Apostila] programação arduíno brian w. evans
[Apostila] programação arduíno   brian w. evans[Apostila] programação arduíno   brian w. evans
[Apostila] programação arduíno brian w. evansWeb-Desegner
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxEng Teong Cheah
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_outputAnil Dutt
 
Introduction to c
Introduction to cIntroduction to c
Introduction to cAjeet Kumar
 

Was ist angesagt? (19)

The GO programming language
The GO programming languageThe GO programming language
The GO programming language
 
(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
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dart
 
JDT embraces lambda expressions
JDT embraces lambda expressionsJDT embraces lambda expressions
JDT embraces lambda expressions
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
Pc module1
Pc module1Pc module1
Pc module1
 
ParaSail
ParaSail  ParaSail
ParaSail
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Unit 5 cspc
Unit 5 cspcUnit 5 cspc
Unit 5 cspc
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
Learn C# Programming - Variables & Constants
Learn C# Programming - Variables & ConstantsLearn C# Programming - Variables & Constants
Learn C# Programming - Variables & Constants
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
[Apostila] programação arduíno brian w. evans
[Apostila] programação arduíno   brian w. evans[Apostila] programação arduíno   brian w. evans
[Apostila] programação arduíno brian w. evans
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 

Andere mochten auch

Nueva promoción de policías nacionales.
Nueva promoción de policías nacionales.Nueva promoción de policías nacionales.
Nueva promoción de policías nacionales.fernandoatienzagarcia
 
Chic 潮流品牌展 《参展商手册》
Chic 潮流品牌展 《参展商手册》Chic 潮流品牌展 《参展商手册》
Chic 潮流品牌展 《参展商手册》vikki557788
 
Inspiring quotes that will change your life
Inspiring quotes that will change your lifeInspiring quotes that will change your life
Inspiring quotes that will change your lifeAbdul Quasim Khan
 
Top 8 group coordinator resume samples
Top 8 group coordinator resume samplesTop 8 group coordinator resume samples
Top 8 group coordinator resume sampleslombardnick09
 
Penulisan surat dinas yang benar
Penulisan surat dinas yang benarPenulisan surat dinas yang benar
Penulisan surat dinas yang benarIsnaini Shaleh
 
¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO?
¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO? ¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO?
¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO? Anyac Análisis Y Acción
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentationricardoaliaga
 
Marktversagen in Folge von Informationsmängeln
Marktversagen in Folge von InformationsmängelnMarktversagen in Folge von Informationsmängeln
Marktversagen in Folge von InformationsmängelnPhilippxx
 
Certifate of distinction in teaching- Harvard U.
Certifate of distinction in teaching- Harvard U.Certifate of distinction in teaching- Harvard U.
Certifate of distinction in teaching- Harvard U.Leonor Figueroa-Feher
 
профессиональный стандарт педагог
профессиональный стандарт педагогпрофессиональный стандарт педагог
профессиональный стандарт педагогТатьяна Фролова
 
Cure and more 2 study details
Cure and more 2 study detailsCure and more 2 study details
Cure and more 2 study detailsMahmoud Ellithy
 
Ricardo aliaga cueva
Ricardo aliaga cuevaRicardo aliaga cueva
Ricardo aliaga cuevaricardoaliaga
 
Modelamiento de albañileria confinad adef
Modelamiento de albañileria confinad adefModelamiento de albañileria confinad adef
Modelamiento de albañileria confinad adefManuel Jara
 
Top 8 inside sales coordinator resume samples
Top 8 inside sales coordinator resume samplesTop 8 inside sales coordinator resume samples
Top 8 inside sales coordinator resume sampleslombardnick09
 
Internship Final Report_eClerx
Internship Final Report_eClerxInternship Final Report_eClerx
Internship Final Report_eClerxShrey Chaurasia
 
4 клас 20 урок. Безпека в інтернеті.
4 клас 20 урок. Безпека в інтернеті.4 клас 20 урок. Безпека в інтернеті.
4 клас 20 урок. Безпека в інтернеті.StAlKeRoV
 
How I reduced my aht by 50 seconds
How I reduced my aht by 50 secondsHow I reduced my aht by 50 seconds
How I reduced my aht by 50 secondsSankha Chatterjee
 
Stereotyping
StereotypingStereotyping
Stereotypingddoggart
 

Andere mochten auch (20)

Nueva promoción de policías nacionales.
Nueva promoción de policías nacionales.Nueva promoción de policías nacionales.
Nueva promoción de policías nacionales.
 
Chic 潮流品牌展 《参展商手册》
Chic 潮流品牌展 《参展商手册》Chic 潮流品牌展 《参展商手册》
Chic 潮流品牌展 《参展商手册》
 
Inspiring quotes that will change your life
Inspiring quotes that will change your lifeInspiring quotes that will change your life
Inspiring quotes that will change your life
 
Km
KmKm
Km
 
Top 8 group coordinator resume samples
Top 8 group coordinator resume samplesTop 8 group coordinator resume samples
Top 8 group coordinator resume samples
 
Barker_Michael_4.4
Barker_Michael_4.4Barker_Michael_4.4
Barker_Michael_4.4
 
Penulisan surat dinas yang benar
Penulisan surat dinas yang benarPenulisan surat dinas yang benar
Penulisan surat dinas yang benar
 
¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO?
¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO? ¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO?
¿QUE PUEDEN HACER LAS REDES SOCIALES POR TU NEGOCIO?
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Marktversagen in Folge von Informationsmängeln
Marktversagen in Folge von InformationsmängelnMarktversagen in Folge von Informationsmängeln
Marktversagen in Folge von Informationsmängeln
 
Certifate of distinction in teaching- Harvard U.
Certifate of distinction in teaching- Harvard U.Certifate of distinction in teaching- Harvard U.
Certifate of distinction in teaching- Harvard U.
 
профессиональный стандарт педагог
профессиональный стандарт педагогпрофессиональный стандарт педагог
профессиональный стандарт педагог
 
Cure and more 2 study details
Cure and more 2 study detailsCure and more 2 study details
Cure and more 2 study details
 
Ricardo aliaga cueva
Ricardo aliaga cuevaRicardo aliaga cueva
Ricardo aliaga cueva
 
Modelamiento de albañileria confinad adef
Modelamiento de albañileria confinad adefModelamiento de albañileria confinad adef
Modelamiento de albañileria confinad adef
 
Top 8 inside sales coordinator resume samples
Top 8 inside sales coordinator resume samplesTop 8 inside sales coordinator resume samples
Top 8 inside sales coordinator resume samples
 
Internship Final Report_eClerx
Internship Final Report_eClerxInternship Final Report_eClerx
Internship Final Report_eClerx
 
4 клас 20 урок. Безпека в інтернеті.
4 клас 20 урок. Безпека в інтернеті.4 клас 20 урок. Безпека в інтернеті.
4 клас 20 урок. Безпека в інтернеті.
 
How I reduced my aht by 50 seconds
How I reduced my aht by 50 secondsHow I reduced my aht by 50 seconds
How I reduced my aht by 50 seconds
 
Stereotyping
StereotypingStereotyping
Stereotyping
 

Ähnlich wie New c sharp3_features_(linq)_part_ii

(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_iNico 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
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_iNico 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
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_viNico Ludwig
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APIPrabu U
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional ProgrammingAapo Kyrölä
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Featurestechfreak
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programmingNico Ludwig
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
Coding style of Linux Kernel
Coding style of Linux KernelCoding style of Linux Kernel
Coding style of Linux KernelPeter Chang
 
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
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScriptbinDebug WorkSpace
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionSimplilearn
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextUnfold UI
 

Ähnlich wie New c sharp3_features_(linq)_part_ii (20)

(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i
 
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
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i
 
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
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_vi
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional Programming
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
Coding style of Linux Kernel
Coding style of Linux KernelCoding style of Linux Kernel
Coding style of Linux Kernel
 
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
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScript
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-Expression
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 

Mehr von 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 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_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
 
(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
 
(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
 
(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_iiNico Ludwig
 
(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
 

Mehr von Nico Ludwig (18)

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 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_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
 
(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
 
(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
 
(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii
 
(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
 

Kürzlich hochgeladen

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Kürzlich hochgeladen (20)

DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

New c sharp3_features_(linq)_part_ii

  • 1. 1 New C#3 Features (LINQ) – Part II Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● New C#3 Features (LINQ) – Part II – Implicit Typing – Lambda Expressions – Extension Methods ● Sources: – Jon Skeet, CSharp in Depth – Bill Wagner, Effective C# – Bill Wagner, More Effective C#
  • 3. 3 Local Type Inference Examples <TypeInferenceExamples> Presents local type inference.
  • 4. 4 Implicit Typing of local Variables // Initialization of a local // variable in C#1/C#2: string someData = "a string"; ● Explicit typing of local variables is no longer required! – Explicit type names can be replaced by the contextual keyword var on initializations. ● But implicitly typed variables are still statically and strongly typed! – They are statically and immutably typed after initialization. – Their static type is inferred by the compiler, it analyzes the initializer expression. – Only the interface of the static type can be used on them. – So they do not function like the type "Variant" in COM or VB 6 or var in JavaScript! – (Implicitly typed constants are not available.) // New in C#3: declare someData as // implicitly typed local variable: var someData = "a string"; ● The keyword var is similar to C++11's "additional meaning" of the auto keyword.
  • 5. 5 Details about implicit Typing ● The initializer expression needs not to be a constant or literal. – In C#3 there exist situations, in which the static type is unknown or anonymous. – These anonymous types are an important feature of LINQ in C#3! ● The compiler is not able to infer all types. – It must be an initialization expression (inferred is the type of the expression right from =). – It must be only one variable being declared. – The compiler cannot infer the type of anonymous functions or method groups (as they are typeless expressions). – The initializer must not be a null literal w/o cast (also a typeless expression). ● Type inference to constant symbols (a syntax likeconst instead of var i.e. "const inference"), is not supported. – (It wouldn't be very useful, because C# only allows compile time constants of primitive types.) ● Problems with the var keyword in practical usage: – It can make code more difficult to read; declaration and usage should be near. – Its usability is much depending on the code editor's quality (e.g. IntelliSense).
  • 6. 6 Pros and Cons of implicit Typing ● Pros – The compiler can often infer the most efficient type (esp. for LINQ results). – Implicitly typed variables must be initialized, which may reduce coding errors. – It can improve readability, when the inferred type is obvious. – Less "using-namespace-directives" are needed. – Code can be easier modified. ● Cons – Can be misunderstood by VB/COM/JavaScript developers. (No duck typing here!) – If the right hand side expression is complex, developers can't infer the type ;). – There are situations, when you are simply forced to use them. – Sometimes inference is the best solution, but it is not obvious why this the case. ● When in doubt mind that code is more often read than written! ● Another con: The var keyword hurts the interface segregation principle (after SOLID). - It enforces a style, where on the left hand side of the assignment a non-interface type, but a very concrete type is inferred. ● A further con: confusion. Confusingly the var keyword is another way to express implicitly typed variables in C# (other ways: the let clause, the properties of anonymous types and arguments of generic methods).
  • 7. 7 Lambda Expressions first Example <LambdaExpressionExamples> fShows how lambda expressions can express Delegate instances.
  • 8. 8 Lambda Expressions as anonymous Functions // Classical usage of an anonymous // method to instantiate a Delegate: Predicate<int> isEven = delegate(int i) { return 0 == (i % 2); }; // New in C#3: usage of a lambda // expression to instantiate a Delegate: Predicate<int> isEven = i => 0 == (i % 2); ● Lambda expressions (lambdas) are an evolution of anonymous methods. – Anonymous methods and lambdas can be generalized to anonymous functions. – (Both allow to pass code as argument to another method.) – You can use expression lambdas and statement lambdas. – (Anonymous iterator blocks are not supported.) – You can generate expression trees from lambdas. – As a block of code lambdas can be debugged! – During run time the "names" of these anonymous functions are obfuscated to unspeakable compiler-generated symbols. ● The operator => is called "lambda operator" or "big arrow" or "rocket" (taken from Ruby's "hashrocket" operator) and can be read as "goes to". ● In ECMAScript's/JavaScript's function literals (var f = function(x) { return x * x; }) are equivalent to C#'s lambdas. - So in other words, JavaScript has lambdas for ages. ● Anonymous functions can not be iterator blocks. It was counted to be too much effort for the C# compiler team. ● Only top level methods can be iterator blocks. ● Anonymous iterator blocks are supported in VB11! ● The unspeakable symbols are then visible in stack-traces and in reverse-engineered code.
  • 9. 9 Lambdas in everyday Usage <LambdaExpressionExamples> Shows some everyday usage examples of lambda expressions.
  • 10. 10 Lambdas as Delegate Instances for everyday usage ● Whenever anonymous methods have been used, lambdas can be used now. – The compiler converts lambdas to Delegate types by type inference respectively. – So the utility methods on Collections (e.g. FindAll()) can deal with lambdas as well. – Functions can have other functions as parameter or result => higher-order functions. – The types Predicate<T>/Action/<T> and the new type Func<T, R> can be used. ● Lambdas can also be used for event handler code. ● If lambdas refer local or other variables, these variables are gettingcaptured. – (I.e. lambdas lexically bind the enclosing scope's locals and the this reference.) – Captured variables can be modified within the capturing lambda! – The lifetime of captured locals is extended! Captured references can't be garbage collected (gc'd), as long as the Delegate instance (backing the lambda) wasn't gc'd! ● Partial application and currying are not directly supported but can be simulated. ● From within anonymous methods you can refer the enclosing scope's locals and also the this reference in instance methods of reference types. So in anonymous methods locals and the this reference are bound lexically. (In opposite to dynamic binding (binding variables where a function is called and not where it is declared) as, e.g., in JavaScript.) ● The details of garbage collection of captured variables have been discussed in the C#2 review. ● Partial application and currying: ● Partial application: Bind a function to variable or constant arguments, so that a new function will be returned that accepts less arguments than the original function. - It can be simulated by creating a new Delegate instance from a call of the bound function with a constant argument. ● Currying (named after the american mathematician Haskell Brooks Curry)/Schönfinkeln (named after the Russian mathematician Moses Ilyich Schönfinkel): Slash a function having multiple parameters into a function having only one parameter, but returning a chain of functions accepting the remaining parameters of the original function. - It can be simulated by cascading anonymous functions collecting the parameters and finally calling the original function. - In short a function having some parameters is converted into a function, which accepts less parameters by binding arguments to parameters.
  • 11. 11 Expression Tree Example <LambdaExpressionExamples> Shows the basic usage of expression trees.
  • 12. 12 Lambda Expressions for Expression Trees ● Expression trees allow to treat code as analyzable and synthesizable data. – It's not as hidden as IL code. – This is a way of meta programming. ● .Net 3.5 has a built-in support to compile lambdas into expression trees. – Mind to use the namespace System.Linq.Expressions! ● This is the cornerstone of LINQ to SQL to handle code as a tree of objects. – 1. Lambdas are parsed to expression trees. – 2. Query providers try to translate the expression tree to SQL (e.g. T-SQL). – 3. The SQL is transmitted to the database engine that executes the SQL. ● Expression trees can also be used to solve other problems. – E.g. you can code own query providers to exploit expression trees. ● Expression trees can be created from lambda expressions, but not from anonymous methods. ● Notice that we have used the type Expression instead of the delegate types Action or Func. Expression objects can be compiled, serialized and passed to remote processes. These features are important as they allow expression trees to be used with IQueryable<T>, i.e. against remote databases.
  • 13. 13 Extension Methods Example f<ExtensionMethodsExamples> Shows the evolution from static utility methods to extension methods.
  • 14. 14 Extension Methods ● Sometimes you'd like to extend the set of methods a type provides. – But you can't do so because the type can't be inherited or is too abstract (interface). – Or changing a type (e.g. an interface) would break the code of present implementors. – Or inheritance is inappropriate: neither behavioral changes nor new fields are needed. – And you just have to use that type's public interface to create the extension. ● When these kinds of desires arise you'll often end up with static utility methods. ● The problem with static methods is that they don't belong to the extended type. – Neither C# nor Visual Studio will help you here. – You'll have to call and recall static methods like global functions with the "dot notation". ● Promote static utility methods to extension methods to use them like members. – This will solve the discussed problems. – With extension methods the type looks like it was extended, but it was not modified.
  • 15. 15 Pervasive Extension Methods – Example f<ExtensionMethodsExamples> Shows the pervasiveness of extension methods.
  • 16. 16 The Implementation Site of Extension Methods ● Extension methods must be defined in top level static classes. – The leftmost parameter must be of the being-extended type, having the this keyword as prefix. – The defining class is sometimes called "sponsor class". ● Any .Net type can be extended. – E.g. sealed, "concrete" and abstract types (including interfaces). – Unbound and constructed generic types. – If an ext. method adds a new overload the respective method group will be extended. ● The extension methods itself: – The public members of the extended type can be accessed via the "this parameter". – Other parameters than the "this parameter" can be used also. – Multiple overloads can be defined as well. – A result can be returned to the caller or not. – There are no extension properties (indexers), constructors, events or operators. ● Top level static classes means not in nested static classes. ● You can extend multiple different types in the same static class (as it is done here), but this should be considered a bad practice. You should create one static class per type you are about to extend. ● In C# you can only extend types, not specific objects (but in Ruby both is possible). ● A method group is the set of all methods found by a member lookup of a certain method name in a certain context. E.g. all overloads of a method being effectively present due to inheritance from multiple types or extension methods are part of that method's method group. Ctors do not build a method group! ● Extension methods can only access public members of the extended type; i.e. they are no "friends" as known from C++! ● If the "this-parameter" is null, then an ArgumentNullException should be thrown, because the "this-parameter" could also be passed explicitly (prefix notation).
  • 17. 17 The Call Site of Extension Methods ● The namespace of the static class defining the extension methods must be used. – That static class could be in the same namespace as the calling entity. – If not, a using directive to the namespace of that class is needed! ● Calling extension methods. – They can be used with the period/pointer operator (i.e. "infix") on all instances directly. – (Infix: an instance of the extended type won't be passed as first argument.) – (The name of the static class defining the extension methods is never used then.) ● Extension methods are pervasive on related types. – This means that they add candidates for method name resolution. – 1. Extension methods of interfaces are available for the implementing types as well. – 2. Extension methods of base types will extend sub types as well. ● The name of the class in which the extension method is defined is irrelevant! ● When there exist extension methods of equal signature in different namespaces, these extension methods may clash if their namespaces are used in the same code file. ● The infix notation of extension methods allows to call them in a chained manner, then they are written in the order they are being executed. ● If the "this-parameter" is null, then an ArgumentNullException should be thrown, because the "this- parameter" could also be passed explicitly (prefix notation).
  • 18. 18 Name Resolution and Binding of Extension Methods ● Name resolution of extension methods with the same logical signature. – (The namespace of the class defining the ext. methods must be visible!) – They are compile time (i.e. statically) bound. – 1. Extension methods on interfaces overrule implemented instance methods. – 2. Extension methods on classes overrule, when they have a better matching signature. – 3. Extension methods on classes can't overrule instance methods of identical signature. – 4. Extension methods on classes can't overrule instance methods having an implicit conversion. – 5. Extension methods on classes can't overrule generic instance methods (best match). ● Binding to generic types. – Extension methods can be defined for an unbound generic type. (IList<T>) – Extension methods can be defined for a constructed generic type. (IList<int>) – Extension methods of IEnumerable<T>s are an easy way to extend LINQ.
  • 19. 19 Extension Methods as Type Augmentation ● This slide describes how extension methods could be applied to augment types. ● 1. Define the minimal interface as .Net interface. – Or leave the present interfaces and classes in your API as they are. ● 2. Create augmentation methods as extension methods on that interface. ● 3. Now programmers can decide to opt in the extension methods. – They just need to add a using directive to make the static class visible. – Then the extension methods are visible as well. ● (4. Eventually the extension methods could be promoted to instance methods.) ● Notice that the visibility of extension methods is controlled by using respective namespaces. ● Indeed extension methods allow extending a type's interface w/o breaking the code of present consumers. Java 8 introduced so called defender or default methods, which can be fully implemented (i.e. with a function body) in an interface definition; default methods do not break an interface's contract, but can be overridden in implementing types. Default methods make use of the template method design pattern.
  • 20. 20 Final Words on Extension Methods ● Extension methods aren't a new concept. – Similar to Obj-C's categories and slightly similar to mixins in Ruby, CLOS, D etc. ● Underneath the compile time custom attribute "ExtensionAttribute" is applied. – E.g. in VB System.Runtime.CompilerServices.ExtensionAttribute must be used. – From .Net 2 code, extension methods can be called with the old prefix period notation. ● Problems with extension methods in practical usage: – Extension methods can not be found by reflection on the extended type. – Its pervasiveness on name resolution can lead to unexpected results. – A type seems to have a varying interface, this is confusing esp. for beginners. – Programmers can't tell instance methods from extension methods by the bare code. – Its usability is much depending on the code editor's quality (e.g. IntelliSense). – => Can be critical in usage, because code is more often read than written! ● The idea of a mixin is to define a part of a new type by including a bundle of predefined functions. And this bundle is called mixin. ● Ruby has also so-called open classes, but this can not be simulated with C#'s extension methods.