.Net 3.5           -Prabhakaran
.Net 3.5 Framework
What is new in 3.5 C#?   Implicitly Typed Local Variables   Automatic Properties   Object Initializer   Collection Ini...
Implicitly Typed Local VariablesExamples:  var i = 5;  var s = "Hello";  var d = 1.0;  var numbers = new int[] {1, 2, 3}; ...
Automatic Properties   public class Person {         private string _firstName;         private string _lastName;        ...
   For example, using automatic properties I can now re-write the code    above to just be:     public class Person {   ...
Object Initializer Person person = new Person(); person.FirstName = "Scott"; person.LastName = "Guthrie"; person.Age = 32;...
Collection InitializersList<Person> people = new List<Person>();  people.Add( new Person { FirstName = "Scott", LastName =...
Extension MethodsExtension methods allow developers to add new methods to the public contract of anexisting CLR type, with...
Lambda Expressions
Query Syntax   Query syntax is a convenient declarative shorthand for expressing queries    using the standard LINQ query...
Anonymous Types
C# 3.5 Features
C# 3.5 Features
Upcoming SlideShare
Loading in …5
×

C# 3.5 Features

4,593 views

Published on

C# 3.5 Features

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,593
On SlideShare
0
From Embeds
0
Number of Embeds
380
Actions
Shares
0
Downloads
62
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C# 3.5 Features

  1. 1. .Net 3.5 -Prabhakaran
  2. 2. .Net 3.5 Framework
  3. 3. What is new in 3.5 C#? Implicitly Typed Local Variables Automatic Properties Object Initializer Collection Initializers Extension Methods Lambda Expressions Query Syntax Anonymous Types Expression Trees
  4. 4. Implicitly Typed Local VariablesExamples: var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary<int,Order>();Are equivalent to: int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary<int,Order> orders = new Dictionary<int,Order>();Errors: var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, collection initializer not permitted var z = null; // Error, null type not permitted
  5. 5. Automatic Properties public class Person { private string _firstName; private string _lastName; private int _age; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public int Age { get { return _age; } set { _age = value; } } }
  6. 6.  For example, using automatic properties I can now re-write the code above to just be: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } Or If I want to be really terse, I can collapse the whitespace even further like so: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
  7. 7. Object Initializer Person person = new Person(); person.FirstName = "Scott"; person.LastName = "Guthrie"; person.Age = 32;3.5 Person person = new Person { FirstName="Scott", LastName="Guthrie", Age=32 }; Person person = new Person { FirstName = "Scott", LastName = "Guthrie" Age = 32, Address = new Address { Street = "One Microsoft Way", City = "Redmond", State = "WA", Zip = 98052 } };
  8. 8. Collection InitializersList<Person> people = new List<Person>(); people.Add( new Person { FirstName = "Scott", LastName = "Guthrie", Age = 32 } ); people.Add( new Person { FirstName = "Bill", LastName = "Gates", Age= 50 } ); people.Add( new Person { FirstName = "Susanne", LastName = "Guthrie", Age = 32 } );3.5List<Person> people = new List<Person> { new Person { FirstName = "Scott", LastName = "Guthrie", Age = 32 }, new Person { FirstName = "Bill", LastName = "Gates", Age = 50 }, new Person { FirstName = "Susanne", LastName = "Guthrie", Age = 32} };
  9. 9. Extension MethodsExtension methods allow developers to add new methods to the public contract of anexisting CLR type, without having to sub-class it or recompile the original type.Example:string email = Request.QueryString["email"];if ( EmailValidator.IsValid(email) ) {}3.5string email = Request.QueryString["email"];if ( email.IsValidEmailAddress() ) {}public static class ScottGuExtensions{ public static bool IsValidEmailAddress(this string s) { Regex regex = new Regex(@"^[w-.]+@([w-]+.)+[w-]{2,4}$"); return regex.IsMatch(s); }}
  10. 10. Lambda Expressions
  11. 11. Query Syntax Query syntax is a convenient declarative shorthand for expressing queries using the standard LINQ query operators.
  12. 12. Anonymous Types

×