cs30 new
DESCRIPTION
This is the presentation to explain what's new in C# 3.0 programming languageTRANSCRIPT
04/07/23 for intenal Knowledge Share only 2
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types– Lambda Expressions– Generic Delegates– Expression Trees
Are we missing any thing here ?????Are we missing any thing here ?????
04/07/23 for intenal Knowledge Share only 3
Pre-Requisites• Some Programming Exp • C• C ++• .NET BasicsApart of all ..
Imagination is more important than knowledge
.. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .... ....
04/07/23 for intenal Knowledge Share only 4
Ur acquaintance with C#
• Using it more than 2 Yrs• Started Recently• Am new to C#
• What C# is ..?
Orcas
What is ?
Specialisation doesn’t mean doing extraordinary thingsSpecialisation doesn’t mean doing extraordinary things
But doing ordinary things, extraordinarily well..But doing ordinary things, extraordinarily well..
04/07/23 for intenal Knowledge Share only 6
Visual Studio Code Name – Orcas
• Web– Multi version apps– CSS Design tools – Intellisense for ECMA Script– AJAX Library
• Applications Templates– WPF– WCF– WF
• Data Integration– EDM – Entity Data Manager (ADO.NET vNext)– Multi-tier Dataset– Reporting Project templates
demo
Visual Studio Code name Orcas
1) Ctrl+Tab2) MultiVersion3) ECMA Intellisupport4) DataIntegration(EDM)5) AJAX Support6) CSS
C# 3.0 Features
If we fail to plan, we are planning to fail
04/07/23 for intenal Knowledge Share only 9
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
04/07/23 for intenal Knowledge Share only 10
Implicitly typed local variables• Declared as ‘var’ – compiler will determine type while
initialization• Can only be used when declared and initialized in same
statement• Can’t be null• Can’t be class members• Used to store anonymous types as in LINQ based programming• Don’t confuse with “var” at JavaScript or VB, which actually
allows you to assign any type to a variable in your code• Multiple declarations should evaluate at Compile time
– var bVal = true;– var strVal = “MATUG”;– Var intNum = 9;– var obj = new { UGName = “Automated Test”, url =
“http://matug.net”, alturl=“http://groups.msn.com/matug/”}
04/07/23 for intenal Knowledge Share only 11
Implicitly typed arrays• Declared as ‘var’ – compiler will determine type
while initialization
• Collection type of elements is determined by compiler
– var fstArry = new[] {3,6,11,78,93}; Int32– var secArry = new[] {3,6,11.48,78,93}; Double– var thrArry = new[] {“Microsoft”, “MAQ”}; String– var secArry = new[] {3,6,11.48,”78”,93}; Won’t
compile due to irregularity in the elements
demo
Implicit Variables
1) Variables2) Multiple Declaration3) Typed Arrays4) Arrays Mismatch
04/07/23 for intenal Knowledge Share only 13
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
04/07/23 for intenal Knowledge Share only 14
Auto implemented properties• Compiler will create the private variables• Single line public property accessor• Simple object persistence mechanism
Old Styleprivate string strVar;public string EmployeeName{get{return strVar;}set{strVar = value;}}
C# 3.0public string EmployeeName{get; set;}
Would you like to know what happens behind the screen ?Would you like to know what happens behind the screen ?
04/07/23 for intenal Knowledge Share only 15
Auto implemented propertiesAssembler will give you the code similar to the below
namespace SampleLib { public class YesClass{ // Fields [CompilerGenerated] private string <>k__AutomaticallyGeneratedPropertyField0; // Methods public YesClass();public void DoNothing();// Propertiespublic string Reply { [CompilerGenerated] get; [CompilerGenerated] set; }}
} How do you know this ? …How do you know this ? … Use any Reflector ..Use any Reflector ..
demo
Property
1) Property2) Reflector
04/07/23 for intenal Knowledge Share only 17
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
04/07/23 for intenal Knowledge Share only 18
Object & Collection Initializers• Declaration and initialization at one step• Ease of construction and initialization
process• Sequence of members are separated by ‘,’• Should be an accessible field assigned with
= operator
demo
Object & Collections
1) Object from a class2) Collections from a List <>
04/07/23 for intenal Knowledge Share only 20
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
04/07/23 for intenal Knowledge Share only 21
Extension Methods• Static Methods• Invoked with instance method syntax• Declared by “this” keyword as modifier• Declared in “static” classes only
• Extension members of other kinds, such as properties, events, and operators, are being considered but are currently not supported.
demo
Extension Methods
1) Reverse the given string
04/07/23 for intenal Knowledge Share only 23
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
04/07/23 for intenal Knowledge Share only 24
Anonymous Types• No class code declaration• Direct initialization• Type is omitted at syntax• Not only the variables but objects can also be
anonymous
Old Stylepublic class Book{
…}
Book bkObj = new Book();
C# 3.0 Style
var bkObj = new { Title = “Harry Potter 7”, PubYr = 2007}
04/07/23 for intenal Knowledge Share only 25
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
Did I miss somethin
g???
04/07/23 for intenal Knowledge Share only 26
λ Lambda Expressions• Extension of anonymous methods• Advancement of Delegates• Is an inline expression / statement block
– with a concise syntax that can be used wherever a delegate or anonymous method is expected
– which can be used to pass arguments to method call or assign value to delegate
• Expression use “=>” operator– Left side denotes result– Right side contains statement block or
expression
Lambda Expressions// Data source.int[] Scores = { 90, 71, 82, 93, 75, 82 };
Write me some code that returns me the count for the numbers that are greater than 80 ?
// This query executes immediately because it returns a singleton int value, not an iterable sequence.int scoreQuery = Scores.Where(n => n > 80).Count();
Lambda Expressions// Data source.string[] Projects = { “eZCap”, “iMedsoft”,
“Patient Portal”, “LiveWellHRA”, “ETL”, “iMedLiveWell”};
Write me some code that returns me the projects that has “Med” in project name ?
IEnumerable<string> projsQry = from proj in projs where proj.Contains("Med") select proj;
foreach (string projName in projsQry) { Console.WriteLine(projName); }
04/07/23 for intenal Knowledge Share only 29
λ Lambda Expressions – conti ..• Improved type inference • Conversion for both delegate types as well
as expression trees• Expression trees ??
– Lambda expressions with an expression body can be converted to expression tree
• General rules– Must contain same number of parameters as the
delegate type– Each parameter and return value must be
implicitly convertible to its corresponding delegate parameter or delegate return type
demo
Lambda Expressions
1) Delegates2) Anonymous Methods3) Lambda Expressions
04/07/23 for intenal Knowledge Share only 31
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Methods– Partial Methods
04/07/23 for intenal Knowledge Share only 32
Generic Delegates (Func)• Allows to construct a method
with out declaring delegate• 5 predefined generic delegates• Last parameter is always
return type• You can extend them with
LINQ– Func<>
demo
Generic Delegates
1) Func<string, string>
04/07/23 for intenal Knowledge Share only 34
Agenda• What’s new in Orcas • C# 3.0
– Implicit Variables– Auto implemented properties– Object & Collection Initializers– Extension Methods– Anonymous Types / Tuples– Lambda Expressions– Generic Delegates (Func)– Expression Trees– Partial Methods
04/07/23 for intenal Knowledge Share only 35
Expression Trees• Don’t get confuse
System.LINQ.Expression with Windows.Forms
• Allow lamda expressions as data instead of code– Expression<>
demo
Expression Trees
1) Expression<Func<string, string>>
2) Expression.Compile
Q&A
04/07/23 for intenal Knowledge Share only 38
For further win/winvar WhoIsThereNextMonthWhoIsThereNextMonth = new {
Default = “Chakravarthy”, Perhaps = “Sarma & Siva”, AreYou=“NotSure”,
Topics=“What do you Want ??”, AnyThing=“Don’t hesitate, ASK ME”}
04/07/23 for intenal Knowledge Share only 39
Where to go for any technical• Post your message at
http://groups.msn.com/CSharpGroup• Drop me a line at
chakravarthy.dsk@inteq
• Visit our sessions on “KS” Knowledge Share with out fail
04/07/23 for intenal Knowledge Share only 40
Next ???