The Artima Developer Community
Sponsored Link

Java Buzz Forum
Implicit Local Variables and Lambda comes to C#

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
Implicit Local Variables and Lambda comes to C# Posted: Sep 13, 2005 7:18 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: Implicit Local Variables and Lambda comes to C#
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

Anders is a smart man. He has seen good ideas that are taking hold in the dynamic language space, and has pragmatically chosen those that can make sense in a statically typed world and applied them.

Specifically:

  • C# 3 has lambda. True closures.
  • C# 3 has implicitly typed local variables

Implicit variables

As Ben says, if your IDE knows to put in a (Cast), can't the compiler?

The C# 3 one can, and does:

var x = 7; var s = "This is a string."; var d = 99.99; var numbers = new int[] { 0, 1, 2, 3, 4, 5, 6 };

foreach (int n in numbers) Console.WriteLine(n);

var lowNums = from n in numbers where n < 5 select n;

Now, this doesn't mean that C# is a script language and you can put anything you want into these variables. The compiler works out the type, and statically binds it. If you try to assign an incorrect type later on you will get an exception.

Lambda

C# 2 offered good old delegate() which was a nice step forward. Now they have gone the whole hog though, and allow you to create lambda expressions.

A lambda expression is written as a parameter list, followed by the => token, followed by an expression.

// Explicit (int x) => x + 1

// Implicit. If there is one, then the parens can be ommited
x => x + 1

// Two, implicit
(x, y) => return x * y;

Some more examples:

var evenNumbers = list.FindAll(i => (i % 2) == 0);

// multiple parameters
var matches = fruit.FilterBy((string name, double price) => name == "banana" && price < 2.00);

Java 7 anyone?

C# 3.0 New features

  • Implicitly typed local variables, which permit the type of local variables to be inferred from the expressions used to initialize them.
  • Extension methods, which make it possible to extend existing types and constructed types with additional methods.
  • Lambda expressions, an evolution of anonymous methods that provides improved type inference and conversions to both delegate types and expression trees.
  • Object initializers, which ease construction and initialization of objects.
  • Anonymous types, which are tuple types automatically inferred and created from object initializers.
  • Implicitly typed arrays, a form of array creation and initialization that infers the element type of the array from an array initializer.
  • Query expressions, which provide a language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery.
  • Expression trees, which permit lambda expressions to be represented as data (expression trees) instead of as code (delegates).

LINQ Project

LINQ Tech Preview

C# 3.0 Language Specification

Read: Implicit Local Variables and Lambda comes to C#

Topic: Reference vs. Learning: pick ONE Previous Topic   Next Topic Topic: [Sep 5, 2005 13:17 PDT] 12 Links

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use