Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | mikehuguet |
View: | 953 times |
Download: | 2 times |
#28 BATON ROUGE
.NET FundamentalsMike Huguet
Solutions Architect for BRDNUG leader, http://brdnug.org MS Patterns & Practices SP Guidance
Advisor SQLSat28 Organizer
http://twitter.com/mhuguet http://geekswithblogs.com/mikehuguet
Mike Huguet
A.K.A – “Huggy” Bear What does he do anyway? Favorite restaurant is any $5 Chinese Buffet He only quotes movies from the 80’s
Alternate Intro
Introduce some of the “new” fundamentals Enhance knowledge of these fundamentals
Objectives
Face.Expression !=
Feature Timeline
2006 2007 2010
3.0 3.5 4.02.0
2008
3.5 SP1
Generics
• Extensions• Lambdas• LINQ
2005
Language specific implementation / syntax Compiler and IDE magic Increase productivity and readability /
reduce code
What’s in Common
?Generics
“Generics let you tailor a method, class, structure, or interface to the precise data type it acts upon.”
- MSDN
Generics
- MSDN
Native to the CLR Allow for better type safety increasing
performance (no box, unbox, casting)◦ ArrayList vs. List<T>
IDE Magic
What are Generics?
*200% perf gain value types (i.e.-string, int…)
*100% perf gain ref types (i.e.-MyClass) Used throughout .NET Make great containers
◦ i.e.-List<T> Activator.CreateInstance<T>()
Uses / Benefits
* - #’s from MSDN
C#◦ List<string> x = new List<string>();◦ String GetValueAsString<T>(T entity) {}
VB.NET◦ Dim x As New List(Of String)◦ Function GetValueAsString(of T)(ByRef entity As T) As String
Syntax
Demo
Derivation Constraints◦ class Repo<T> where T : BaseEntity◦ class Repo<T> where T : BaseEntity, IComparable<T>◦ class Repo<T, K> where T : BaseEntity
where K : IValidator
Reference/Value Type Constraints◦ class Repo<T> where T : class◦ class Repo<T> where T : struct
Constructor Constraints◦ class Repo<T> where T : class, new()
Constraints (C#)
Derivation Constraints◦ Class Repo(Of T As BaseEntity)◦ Class Repo(Of T As {BaseEntity, IComparable(Of T)})◦ Class Repo(Of T As BaseEntity, K As IValidator))
Reference/Value Type Constraints◦ Class Repo(Of T As Class)◦ Class Repo(Of T As Structure)
Constructor Constraints◦ Class Repo(Of T As New, Class)
Constraints (VB.NET)
?Extension Methods
“Extension methods enable you to ‘add’ methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.”
-MSDN
Allows adding methods to an existing type No recompile needed Can extend framework classes (including
sealed) Methods included in Intellisense
Extension Methods
Demo
?Lamda Expressions
“A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.”
-MSDN
Shorthand for a function A variable that points to function Anonymous function (no name) Returns a single value Explicit or inferred types
Return Parameter(s)
What is a Lambda Anyway?
C# Arg list => expression x => x + 1 (x, y) => x == y
VB.NET Function(x) x + 1 Function(x, y) x = y
Syntax Examples
Summary - Wherever a delegate type can be used
Event handlers LINQ Projection Extensions (Select, SelectMany) Find Extensions (Find, FindAll)
When Can We Use Them?
Demo
?Language Integrated Query (LINQ)“LINQ is a set of extensions to the .NET
Framework that encompass language-integrated query, set, and transform operations. .”
-MSDN
Core functionality provided by extension methods (method based)◦ Any()◦ Where()◦ OrderBy()◦ ThenBy()◦ Contains()◦ Join()◦ First()...
LINQ
Expression basedvar results = from process in Process.GetProcesses() where process.ProcessName.Contains("WINWORD") orderby process.MainWindowTitle descending select process;
Method based var results = Process.GetProcesses()
.Where(process=>process.ProcessName .Contains("WINWORD")) .OrderBy(x=>x.MainWindowTitle);
LINQ Options
Demo
LINQ
.NET Language – Integrated Query
C# VBOther
Languages
LINQ to Objects LINQ to
DatasetsLINQ to
SQLLINQ to Entities
LINQ to XML
Relational Data
XMLObjec
ts
LINQ enabled ADO.NET
LINQ enabled data sources
LINQ to … LINQ to Objects LINQ to XML (XLINQ) LINQ to DataSets LINQ to SQL (DLINQ) LINQ to Entities (EF) LINQ to SharePoint LINQ to REST LINQ to NHibernate LINQ to Twitter LINQ to SQL Saturday???
Feature Timeline
2006 2007 2010
3.0 3.5 4.02.0
2008
3.5 SP1
Generics
• Extensions• LINQ• Lambdas
2005
Key Takeaways Rich productivity improvements in the
framework and languages More to explore and learn LINQ is built on all of these You know that you want to roll your own
LINQ provider & “Extend it”