Click here to Skip to main content
15,919,774 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: C# - just making an observation Pin
Albert Holguin1-Dec-14 9:32
professionalAlbert Holguin1-Dec-14 9:32 
GeneralRe: C# - just making an observation Pin
Albert Holguin1-Dec-14 9:31
professionalAlbert Holguin1-Dec-14 9:31 
GeneralRe: C# - just making an observation Pin
Dan Neely2-Dec-14 3:13
Dan Neely2-Dec-14 3:13 
GeneralRe: C# - just making an observation Pin
Marc Clifton1-Dec-14 16:18
mvaMarc Clifton1-Dec-14 16:18 
GeneralRe: C# - just making an observation Pin
BillWoodruff1-Dec-14 20:44
professionalBillWoodruff1-Dec-14 20:44 
GeneralRe: C# - just making an observation Pin
OriginalGriff1-Dec-14 8:07
mveOriginalGriff1-Dec-14 8:07 
GeneralRe: C# - just making an observation Pin
BillWoodruff1-Dec-14 20:44
professionalBillWoodruff1-Dec-14 20:44 
GeneralRe: C# - just making an observation Pin
OriginalGriff1-Dec-14 22:43
mveOriginalGriff1-Dec-14 22:43 
var is necessary - it's hard to see how Linq could work without it - and yes, it's still strongly typed. But...it's lazy, and it promotes code that is harder to read - for the sake of saving a couple of seconds when you write the code.

For example, what is this code doing:
C#
foreach (var item in myList)
   {
   ...
   }
What type is item? I don't know - so I have to go elsewhere, find the definition of myList and work it out from there.
And then I find that myList is declared as
C#
var myList = myUserControlInADifferentAssembly.GetAll(userInput);
Which means I have to hunt that down as well.
If instead the original coder had spent a couple of seconds writing:
C#
foreach (UserProfile item in myList)
   {
   ...
   }
My concentration wouldn't be broken and I could keep on with the flow of the code.

I've seen people write this:
C#
var i = 0;
Which is spectacularly pointless!

There is also the fun that if a var is an IEnumerable returned by Linq methods, it isn't obvious that using it twice in successive lines of code will re-evaluate the whole enumerable again! At least if you have to write the type in full, you get a clue that probably what you should be doing is converting it to a List to evaluate the IEnumerable! You don't get that from var because "the system just handles it for you" - in the same way that untyped Dim works in VB, and that always comes back to bite people! Laugh | :laugh:

For example:
C#
private string Showme(string s)
    {
    Console.WriteLine(s);
    return s;
    }

If you do this:
C#
List<string> list = new List<string>() { "hello", "there", "Paul" };
var enumerable = list.Where(s => s != "c").Select(s => Showme(s));
Console.WriteLine("111111");
string s1 = string.Join(";", enumerable);
Console.WriteLine("222222");
string s2 = string.Join(";", enumerable);
Console.WriteLine("333333");

It isn't obvious that you get this:
111111
hello
there
Paul
222222
hello
there
Paul
333333

But as a List it is evaluated once:
C#
List<string> list = new List<string>() { "hello", "there", "Paul" };
var enumerable = list.Where(s => s != "c").Select(s => Showme(s)).ToList();
Console.WriteLine("111111");
string s1 = string.Join(";", enumerable);
Console.WriteLine("222222");
string s2 = string.Join(";", enumerable);
Console.WriteLine("333333");

hello
there
Paul
111111
222222
333333
And spelling it out makes that clearer:
C#
List<string> list = new List<string>() { "hello", "there", "Paul" };
List<string> enumerable = list.Where(s => s != "c").Select(s => Showme(s)).ToList();
Console.WriteLine("111111");
string s1 = string.Join(";", enumerable);
Console.WriteLine("222222");
string s2 = string.Join(";", enumerable);
Console.WriteLine("333333");


To my mind, it promotes laziness, which leads to lazy, unthinking code which is harder to maintain.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

GeneralRe: C# - just making an observation Pin
JMK-NI1-Dec-14 8:22
professionalJMK-NI1-Dec-14 8:22 
GeneralRe: C# - just making an observation Pin
Ravi Bhavnani1-Dec-14 8:24
professionalRavi Bhavnani1-Dec-14 8:24 
GeneralRe: C# - just making an observation Pin
Nemanja Trifunovic1-Dec-14 8:37
Nemanja Trifunovic1-Dec-14 8:37 
GeneralRe: C# - just making an observation Pin
Jeremy Falcon1-Dec-14 8:44
professionalJeremy Falcon1-Dec-14 8:44 
GeneralRe: C# - just making an observation Pin
Mycroft Holmes1-Dec-14 13:27
professionalMycroft Holmes1-Dec-14 13:27 
GeneralRe: C# - just making an observation Pin
Jeremy Falcon1-Dec-14 13:58
professionalJeremy Falcon1-Dec-14 13:58 
GeneralRe: C# - just making an observation Pin
Mycroft Holmes1-Dec-14 16:47
professionalMycroft Holmes1-Dec-14 16:47 
GeneralRe: C# - just making an observation Pin
Jeremy Falcon2-Dec-14 4:33
professionalJeremy Falcon2-Dec-14 4:33 
GeneralRe: C# - just making an observation Pin
Marc Clifton1-Dec-14 16:10
mvaMarc Clifton1-Dec-14 16:10 
GeneralRe: C# - just making an observation Pin
Jeremy Falcon1-Dec-14 16:41
professionalJeremy Falcon1-Dec-14 16:41 
GeneralRe: C# - just making an observation Pin
Ian Shlasko1-Dec-14 8:44
Ian Shlasko1-Dec-14 8:44 
GeneralRe: C# - just making an observation Pin
Jeremy Falcon1-Dec-14 8:47
professionalJeremy Falcon1-Dec-14 8:47 
GeneralRe: C# - just making an observation Pin
Ian Shlasko1-Dec-14 8:52
Ian Shlasko1-Dec-14 8:52 
GeneralRe: C# - just making an observation Pin
OriginalGriff1-Dec-14 9:07
mveOriginalGriff1-Dec-14 9:07 
GeneralRe: C# - just making an observation Pin
Albert Holguin1-Dec-14 8:53
professionalAlbert Holguin1-Dec-14 8:53 
GeneralRe: C# - just making an observation Pin
Ian Shlasko2-Dec-14 1:31
Ian Shlasko2-Dec-14 1:31 
GeneralRe: C# - just making an observation Pin
Albert Holguin2-Dec-14 4:06
professionalAlbert Holguin2-Dec-14 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.