1 / 16

Sprint Technologies Pvt. Ltd C# 3.0 Features

Sprint Technologies Pvt. Ltd C# 3.0 Features. Features. Implicitly Typed Local Variables Implicitly Typed Arrays Auto Implemented Properties Object Initializers Collection Initializers Anonymous Types Extension Methods Lambda Expressions. Implicitly Typed Local Variables.

eagan
Download Presentation

Sprint Technologies Pvt. Ltd C# 3.0 Features

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sprint Technologies Pvt. Ltd C# 3.0 Features

  2. Features • Implicitly Typed Local Variables • Implicitly Typed Arrays • Auto Implemented Properties • Object Initializers • Collection Initializers • Anonymous Types • Extension Methods • Lambda Expressions

  3. Implicitly Typed Local Variables • To declare variable ,type is implicitly inferred from the expression used to initialize the variable • A new keyword called "var" Eg:var age = 30; initializes age to 30 & automatically gives it type of integer. age is a strongly typed variable • Need to be declared & initialized in the same statement. • Can't be initialized to null. • Can't be used as class members. • Used to declare anonymous types as part of a LINQ expression.

  4. Implicitly Typed Arrays • use the var keyword for declaring arrays var numbers = new[] { 1, 2, 3, 4, 5}; var names = new[] { "Dave", Doug, "Jim" }; • Compiler infers type of array elements at compile-time • By examining the values from initialization expression

  5. Auto Implemented Properties • Class is placeholder with getters & setters • Hold property values without additional logic • get & set properties are repetitive In C# 3.0: • On declaring a property, compiler automatically creates a private, anonymous field • Available to property's get & set accessors only • Auto implemented properties must declare get & set • To create a read-only property, modify scope of set accessor to be private.

  6. Auto Implemented Properties Example Normally: public class Person{ string _firstName; string _lastName; public string FirstName { get{return _firstName;} set{_firstName = value;} } public string LastName { get{return _lastName;} set{_lastName = value;} }public string FullName { get{return FirstName + " " + LastName;} } } in C# 3.0: public class Person{ public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return FirstName + " " + LastName; } private set {;} } }

  7. Object Initializers • create object & set its properties in a single line of code Person object & its properties appropriate values Eg:Person obj = new Person { ID = 1, FirstName = "Bill", LastName = "Gates" }; MessageBox.Show(obj.FullName); directly assign values to properties of Person object • reduce the need to have a specific constructor for every variation of argument variation • Used to write LINQ based select projection queries that transform & create a new object for results of the query.

  8. Collection Initializers • to populate a collection: List<string> names = new List<string>(); names.Add("David"); names.Add("Tim"); names.Add("Doug"); • can shorten line of code to a single line List<string> names = new List<string> {"David", "Tim", "Doug"};

  9. Anonymous Types • can create a type on-the-fly at compile time var obj=new{ FirstName="Bill",LastName="Gates"}; type assigned to obj is determined by compiler • hence,can't pass an anonymous type to another method • can be used within method they are declared. • new type has public properties & backing fields defined for members initialized during construction • use Object Initializer to specify properties • placeholders for quickly defining entity types

  10. Extension Methods • new static methods to existing classes • class containing extension method should be static. • invoked as they are instance methods. • first parameter passed specifies type on which they operate & it is preceded by "this" • can't access private variables of type extending from the method • Instance methods with same signature take precedence

  11. Extension Methods • add a method "IsValidZipCode" to string class to validate zip code namespace StringExtensions { public static class CustomStringExtension { public static bool IsValidZipCode(this string input) { Regex regEx = new Regex(@"^\d{6}$"); return regEx.IsMatch(input); } } } • specify type to which extn method should be added as first parameter • IsValidZipCode method to be added to string class • Inside extn method, access all public properties/methods/events of actual string instance that method is being called

  12. Extension Methods to invoke it from the client application, • import the namespace , CustomStringExtension is located using StringExtensions; • declare a variable of type string & invoke IsValidZipCode() private void btnTestExtensionMethod_Click(object sender, EventArgs e) { string zip = "85226"; if (zip.IsValidZipCode()) MessageBox.Show("Valid Zipcode format"); else MessageBox.Show("Invalid Zipcode format"); }

  13. Lambda Expressions • functions declared in context of expressions than a class member • An inline expression / a statement block • used to pass arguments to a method / assign value to delegate • Use lambda operator => • left side of the operator denotes results • the right side contains expression to be evaluated.

  14. Lambda Expressions • syntax: (parameter-list) => expression; • expression - any C# expression or a block of code. • can use a lambda expression in place of a delegate. Eg: age => age + 1 takes one argument, age,& returns age + 1 as the result

  15. Lambda Expressions sample lambda expressions & corresponding delegates: //Explicitly typed parameter (Person obj) => MessageBox.Show(obj.FirstName.ToUpper()); //Implicitly typed parameter (obj) => obj.FirstName == "Bill"; //Explicitly typed parameter (int a, int b) => a + b //Implicitly typed parameter (x, y) => { return x + y; } • can infer parameter type from signature of delegate it is assigned to.

  16. Thank You

More Related