Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

Yet Another Way for Using the "using" Keyword

There are two uses for the C# using keyword:

  1. The using statement:

    Defines a scope, outside of which an object or objects will be disposed.

    using (TransactionScope transactionScope = new TransactionScope())

    {

        // ...

    }

  2. The using directive:

    The using directive has two uses:

    1. To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:

      using System.Text;

    2. To create an alias for a namespace or a type:

      using Text = System.Text;

      using SB = System.Text.StringBuilder;

At the last MVP Global Summit, Mads dared the C# MVPs to came up with another way for using the using keyword.

Taking Mads on his dare, here is my proposal: use it on LINQ.

There is some kind of non-determinism (from the developer's point of view) in the code generated from LINQ queries.

Let's take, for example, this class:

public class MyList<T> : List<T>

{

    public IEnumerable<T> Select<TResult>(Func<T, TResult> selector)

    {

        return null;

    }

}

Let's take an instance of this class and reference it with two variable of different types:

MyList<string> ml = new MyList<string>() { "one", "two", "three" };

IList<string> il = ml;

The way the C# compiler expands queries depends on the variable being used.

This query:

var qm = fromin ml

         select s.ToUpper();

will expand into:

IEnumerable<string> qm = ml.Select<string>(delegate(string s)

    {

        return s.ToUpper();

    });

And this query:

var qi = fromin il

         select s.ToUpper();

will expand into:

IEnumerable<string> qi = System.Linq.Enumerable.Select<string, string>(il, delegate(string s)

    {

        return s.ToUpper();

    });

What if I wanted to explicitly use System.Ling.Enumerable.Select on ml but using the query syntax?

A nice way to do it would be:

var qm = fromin ml

         select using(System.Linq.Enumerable) s.ToUpper();

Just like table hints on TSQL.

Published Sun, Mar 25 2007 21:58 by Paulo Morgado

Comments

# More On Another Way For Using The “using” Keyword@ Sunday, August 10, 2008 7:47 PM

In the past I presented another possible use for the using keyword: as hints on LINQ . I’ve been giving

Paulo Morgado

# More On Another Way For Using The “using” Keyword@ Sunday, August 10, 2008 7:48 PM

In the past I presented another possible use for the using keyword: as hints on LINQ . I’ve been giving

Paulo Morgado

# More On Another Way For Using The “using” Keyword@ Sunday, August 10, 2008 8:46 PM

In the past I presented another possible use for the using keyword: as hints on LINQ . I’ve been giving

Community Blogs

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: