cs30 new

40
C# 3.0 What’s new by Chakravarthy http://msmvps.com/blogs/chakravart [email protected] Project Le

Upload: dsk-chakravarthy

Post on 26-Aug-2014

1.414 views

Category:

Technology


1 download

DESCRIPTION

This is the presentation to explain what's new in C# 3.0 programming language

TRANSCRIPT

Page 1: Cs30 New

C# 3.0 What’s

newby Chakravarthy

http://msmvps.com/blogs/[email protected]

Project Lead

Page 2: Cs30 New

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 ?????

Page 3: Cs30 New

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

.. .. .. .. .. .. .. .. .. .. ..

.. .. .. .. .. .. .. .. .. .. .... ....

Page 4: Cs30 New

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 ..?

Page 5: Cs30 New

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..

Page 6: Cs30 New

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

Page 7: Cs30 New

demo

Visual Studio Code name Orcas

1) Ctrl+Tab2) MultiVersion3) ECMA Intellisupport4) DataIntegration(EDM)5) AJAX Support6) CSS

Page 8: Cs30 New

C# 3.0 Features

If we fail to plan, we are planning to fail

Page 9: Cs30 New

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

Page 10: Cs30 New

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/”}

Page 11: Cs30 New

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

Page 12: Cs30 New

demo

Implicit Variables

1) Variables2) Multiple Declaration3) Typed Arrays4) Arrays Mismatch

Page 13: Cs30 New

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

Page 14: Cs30 New

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 ?

Page 16: Cs30 New

demo

Property

1) Property2) Reflector

Page 17: Cs30 New

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

Page 18: Cs30 New

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

Page 19: Cs30 New

demo

Object & Collections

1) Object from a class2) Collections from a List <>

Page 20: Cs30 New

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

Page 21: Cs30 New

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.

Page 22: Cs30 New

demo

Extension Methods

1) Reverse the given string

Page 23: Cs30 New

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

Page 24: Cs30 New

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}

Page 25: Cs30 New

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???

Page 26: Cs30 New

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

Page 27: Cs30 New

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();

Page 28: Cs30 New

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); }

Page 29: Cs30 New

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

Page 30: Cs30 New

demo

Lambda Expressions

1) Delegates2) Anonymous Methods3) Lambda Expressions

Page 31: Cs30 New

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

Page 32: Cs30 New

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<>

Page 33: Cs30 New

demo

Generic Delegates

1) Func<string, string>

Page 34: Cs30 New

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

Page 35: Cs30 New

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<>

Page 36: Cs30 New

demo

Expression Trees

1) Expression<Func<string, string>>

2) Expression.Compile

Page 37: Cs30 New

Q&A

Page 38: Cs30 New

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”}

Page 39: Cs30 New

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

Page 40: Cs30 New

04/07/23 for intenal Knowledge Share only 40

Next ???