Search the web
Sign In
New User? Sign Up
altdotnet · Alt Dot.Net Discussions
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Linq question, can you cast a 'var' to a typed object?   Message List  
Reply | Forward Message #3380 of 23235 |
Re: Re: Linq question, can you cast a 'var' to a typed object?

Hello Ayende,

There ARE two rules for vars, and you CAN set severity for each of them.
First rule is the conservative one - use var when type is explicitly visible
on the right side of initialization expression. This covers cases like this:
var dict = new Dictionary<A,B>();
var list = (IList<A>)obj;
var service = serviceProvider.GetService<ISomeService>();

Second rule suggests var whenever type on the right side is the same as variable
type, e.g. replacing with var will not change types.

First rule is suggestion by default, the second is hint. If you don't like
the second, hit Alt-Enter on the hint, select "Change severity" and select
"Do not show".

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


AR> Jimmy,
AR> I honestly think that in some directions, C# 2.0 was pushed as far
AR> as it
AR> could go.
AR> See NHQG for details, but be sure to bring the lang spec with you
AR> :-)
AR> R# 4 supports 3.0, the problem is that I think that it needs better
AR> granularity on the rules for var.
AR> It needs at least two, always use var where appropriate. Use var for
AR> new /
AR> const assignment.
AR> On 2/24/08, Jimmy Bogard
AR> <jimmy.bogard@...> wrote:
AR>
>> Count is valid:
>>
>> if you're importing System.Linq
>> if you import the System.Enumeration namespace
>> if you use the Count() method
>> If R# doesn't support 3.0 in an EAP at least, we can't begin to
>> assume
>> what will or will not be abused. I'm still explaining to folks how
>> generics
>> work in .NET, let alone lambdas.
>> var foo = GetFoo() is a special case. For the overwhelming most part
>> I see var foo = new FooBar();
>>
>> Didn't this happen with C# 2? Everyone assumed all the new features
>> would get abused. If anything people pushed the limits of the
>> features' usefulness to find the boundaries. I don't think we've
>> completely exhausted the usefulness of C# 2.0, what makes everyone
>> think we're already in the hole for 3.0?
>>
>> On Sat, Feb 23, 2008 at 8:30 PM, Chris Brandsma
>> <chris.brandsma@...>
>>
>> wrote:
>>
>>> No Count is valid if you have System.Linq in your usings.
>>>
>>> This is where Extension Methods will make things a bit confusing for
>>> people.
>>>
>>> On Sat, Feb 23, 2008 at 7:28 PM, Ayende Rahien
>>> <Ayende@...> wrote:
>>>
>>>> var shouldn't be smarter, it is string[]. and that doesn't have a
>>>> Count property.
>>>>
>>>> var foo = GetUsersByGroups();
>>>>
>>>> Now, can you tell me what this returns?
>>>>
>>>> On 2/24/08, Jay R. Wren
>>>> <jrwren@...> wrote:
>>>>
>>>>> I knew I'd stir stuff up.
>>>>>
>>>>> I'm sure this is going to be a greater style war than { after the
>>>>> if or on the next line.
>>>>>
>>>>> I'm familiar with GetFoo(). Heck, by name it had better return a
>>>>> Foo, so I know that foo is a Foo.
>>>>>
>>>>> One thing I noticed pretty quickly with var is that the compiler
>>>>> types the variable exactly as the return parameter of the
>>>>> signature of the
>>>>> method. For example,
>>>>> var s= "a b c ".Split(' ');
>>>>> var c = s.Count;
>>>>> If s is an IList<string> this compiles, but s isn't an
>>>>> IList<string>, even though it could be. s here will always be a
>>>>> string[]
>>>>> because Split returns string[]. string[] doesn't have a Count
>>>>> member, so
>>>>> this code does not compile.
>>>>> :( var could be smarter.
>>>>>
>>>>> --
>>>>> Jay
>>>>> On Sat, Feb 23, 2008 at 9:09 PM, Ayende Rahien
>>>>> <Ayende@...> wrote:
>>>>>
>>>>>> Hm, I am not sure that I agree.
>>>>>>
>>>>>> var foo = GetFoo();
>>>>>>
>>>>>> This isn't clear to me. I want to see the type itself in this
>>>>>> scenario.
>>>>>>
>>>>>> On 2/24/08, Jay R. Wren
>>>>>> <jrwren@...> wrote:
>>>>>>
>>>>>>> I take a day off and I miss a good discussion.
>>>>>>>
>>>>>>> I just want to comment that I plan on using var EVERYWHERE that
>>>>>>> I can possibly use it in C# 3.0.
>>>>>>>
>>>>>>> var makes me typing easier.
>>>>>>>
>>>>>>> That is all.
>>>>>>> --
>>>>>>> Jay
>>>>>>> On Fri, Feb 22, 2008 at 6:07 PM, Matthew Podwysocki <
>>>>>>> matthew.podwysocki@...>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Right, which I should have explained a little better...
>>>>>>>>
>>>>>>>> On Fri, Feb 22, 2008 at 6:06 PM, Dustin Campbell <
>>>>>>>> dustinc@...> wrote:
>>>>>>>>
>>>>>>>>>> (I'm not sure which F# uses).
>>>>>>>>>>
>>>>>>>>> F# is statically-typed and uses compile-time type inference,
>>>>>>>>> but to a
>>>>>>>>> greater degree than C# 3.0.
>>>>>>>>> Best Regards,
>>>>>>>>> Dustin Campbell
>>> --
>>> --------------------------------
>>> Christopher Brandsma
>>> http://www.ChrisBrandsma.com
>>> http://www.ElegantCode.com
>>> http://www.BoiseCodeCamp.com
>>> Boise Code Camp 2008 -- March 8, 2008






Sun Feb 24, 2008 10:45 am

ilya.ryzhenkov
Offline Offline
Send Email Send Email

Forward
Message #3380 of 23235 |
Expand Messages Author Sort by Date

Heartily agree Jeremy....
cls2deg
Offline Send Email
Feb 22, 2008
8:16 pm

Hello Ayende, There ARE two rules for vars, and you CAN set severity for each of them. First rule is the conservative one - use var when type is explicitly...
Ilya Ryzhenkov
ilya.ryzhenkov
Offline Send Email
Feb 24, 2008
10:45 am

Cool, this is great to know....
Ayende Rahien
Ayende@...
Send Email
Feb 24, 2008
11:06 am

Hello, I actually find this to be to invasive. Maybe I'm still to used to 2.0-style code, but I changed it, so that instead of reporting "you can put var here"...
Krzysztof Koźmic
xtoff_k
Offline Send Email
Feb 24, 2008
12:33 pm

Hello james, When we were working on var support in ReSharper 4 the team quickly splitted into two groups. One group, who had some background in functional...
Ilya Ryzhenkov
ilya.ryzhenkov
Offline Send Email
Feb 24, 2008
11:05 am

... name is somewhat wrong. It should be "currentElement". Note, that it is not like Hungarian Notion, we don't encode type in the name. We give "hint" to type...
Casey Charlton
caseycharlton69
Offline Send Email
Feb 24, 2008
11:39 am

Hello, You pretty much nailed it here. However doing what you described, requres discipline, and that's something not every programmer has. Some will still...
Krzysztof Koźmic
xtoff_k
Offline Send Email
Feb 24, 2008
12:40 pm

Actually, I don't think doing foreach (var v in myDictionary) is really a problem. What would cause a problem would be foreach (object o in myDictionary)...
Jay Chapman
jchapman202
Offline Send Email
Feb 24, 2008
1:16 pm

Hello, I was referring to what Ilya wrote about need to better name variables when you use var. Maybe i really shouldn't have used myDictionary let's say you...
Krzysztof Komic
xtoff_k
Offline Send Email
Feb 24, 2008
1:38 pm

Hello Casey, You can abuse any feature of any language. Why should we focus on "abusability" of language features, instead of defining good practices? It's...
Ilya Ryzhenkov
ilya.ryzhenkov
Offline Send Email
Feb 24, 2008
11:57 am

Thats what I would like P&P to be doing :) Fundamentally ... developers will use every combo of var, linq, lambdas, extension methods and every other new...
Casey Charlton
caseycharlton69
Offline Send Email
Feb 24, 2008
12:07 pm
 First  |  |  Next > Last 
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help