I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

Weird C# compile possibility

Now this is something i really didn't think it would compile in C#.

 

private int _someVar = 0;
private void DoStuff1()
{            
    int _someVar = 0;
    _someVar = 6;
    // ... code that uses _someVar
}

private void DoStuff2()
{
    _someVar = 5;
    // ... code that uses _someVar
}

 

We should at least get a warning if you ask me... 

One learns something new every day.

Print | posted on Wednesday, May 23, 2007 9:52 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: Weird C# compile possibility

I dont get why you would think that would not work as you have declared the same variable for the class / form?
Its scope is valid across all methods of the class. The inner _someVar..within the actual function is local to the actual function, it takes precedence over the class variable _someVar.

5/23/2007 11:35 PM | Jon
Gravatar

# re: Weird C# compile possibility

i didn't say that it wouldn't work.
i know it works.
i just think that this kind of "typo" in variable naming should at least give a warning.
5/24/2007 9:05 AM | Mladen
Gravatar

# re: Weird C# compile possibility

I don't think a warning should appear there because this is perfectly legal and a warning would only clutter output.
5/24/2007 9:52 AM | Miha Markic
Gravatar

# re: Weird C# compile possibility

well i'm of the opnion that the IDE and the compiler should make our life simpler.
and that they should report ambigouous code.

i understand that this is completly legal code, but i've been debugging this scenario yesterday for 2 hours, and a nice little warning would be nice.

i disagree about cluttering output.
5/24/2007 12:06 PM | Mladen
Gravatar

# re: Weird C# compile possibility

Yeah, well, warnings should be warnings - compiler can't guess whether you are right or wrong dealing with those variables or fields. So, if you are doing something as you want you might get a ton of useles warnings which would clutter the output and one would miss the real warnings.
Perhaps IDE could have an option to color local variables differently then fields.
5/24/2007 2:08 PM | Miha Markic
Gravatar

# re: Weird C# compile possibility

that's not a bad idea at all, Miha.
5/24/2007 2:09 PM | Mladen
Gravatar

# re: Weird C# compile possibility

That would be a nice feature, local vs public to the class variables.
5/24/2007 2:25 PM | Jon
Gravatar

# re: Weird C# compile possibility

O one last thing, tell whoever wrote this to not start local variables with the underscore key. The hell kind of coding style is that!
5/24/2007 2:25 PM | Jon
Gravatar

# re: Weird C# compile possibility

it was a copy/paste accident :)
5/24/2007 2:26 PM | Mladen
Gravatar

# re: Weird C# compile possibility

Some orgs I have worked in have a policy of always prefacing fields with "this" to make clear they are fields and not locals. doen't work for statics obviously.

I like the colorizing thing better though.
5/29/2007 7:15 PM | Jesse
Gravatar

# re: Weird C# compile possibility

ReSharper provides a warning saying something like "Local variable hides instance variable" in this scenario.
6/9/2007 3:21 PM | Richard
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET