c# var
-
Actually your example is quite interesting, as it isn't really a String but rather Uri. The correct way to declare it would have been
using System;
...
Uri url = new Uri("http://www.contoso.com/");WebRequest wr = WebRequest.Create(siteUri);
But as you might have figured, you could have just used this instead
var url = new Uri("http://www.contoso.com/");
var wr = WebRequest.Create(siteUri);
or just
var url = "http:// www. contoso.com/";
var request = WebRequest.Create (url);Member 2896020 wrote:
var url = "http:// www. contoso.com/"; var request = WebRequest.Create (url);
Which is essentially what it was. But url is a string. I don't see the value in typing var instead of string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
VB doesn't have
var
? :confused: The equivalent would be:Dim something = "my string"
Which looks fine to me and is still strongly typed, unless Options Strict is Off.
Mycroft Holmes wrote:
As for
var
content changing type and not having to refactorUsing
var
is strongly typed so I don't really get what you're saying. Are you thinking ofdynamic
orOption Strict Off
(which luckily DOESN'T have an equivalent in C# X| )?Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
I use c# and almost never use var, and the content changing comment was a response to some of the previous posts.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
I'd actually prefer it is you could do this:
Dictionary> complicatedDictionary = new *();
To me, that would be a more natural way of showing what the type of
complicatedDictionary
actually is. As it is, I findvar
is mainly over used by the lazy-and-don't-care script kiddies ..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
C# 9.0 will have it: [Welcome to C# 9.0 | .NET Blog](https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#target-typed-new-expressions)
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
The problem is that it sometimes becomes a style guideline in teams. I have seen that happening before, which is just stupid to me. "Oh, but you can just hover over it and you will see". This mindset can be a real nightmare on code reviews. I hate the laziness that motivated most var uses I have seen. People don't even know why var was created (to support anonymous types). There are though, as some people have already mentioned, some situations where var makes the code cleaner. But I actually never seen that kind of diligence, so wherever I could I abolished the used of var for non anonymous types. To make matters worse, stupid resharper (that some people like to use) has the default setting that everything should be var. So it pretty much converts the new-comers to the var mindset from the start.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
I'd actually prefer it is you could do this:
Dictionary> complicatedDictionary = new *();
To me, that would be a more natural way of showing what the type of
complicatedDictionary
actually is. As it is, I findvar
is mainly over used by the lazy-and-don't-care script kiddies ..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
Dictionary> complicatedDictionary = new ();
Is on the planned list for C# 9.0.
-
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
In my experience, the need to use var arises from wanting to hide the sometimes lengthy names of collection iterators in foreach declarations. Especially when Linq is used to get the collection, the declaration can become messy. But you now how it goes: first it's to shorten lengthy iterator names, then lengthy class names, then pretty much everywhere because it looks more consistent for some people. I can support the use in foreach declarations, but everywhere else it's more of an annoyance. I never understood the desire to hide stuff like this. It's like an unspoken commitment to never refactor whatever is hidden. On a related note: Regions. Why?
-
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
I really like the short notation. When I need to know what type the variable is, I hover the mouse cursor over it and it will show the actual type. And so long as I am not paid by the character, I will use the shortest notation I can use.:cool:
-
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
There is no advantage. If you just write a scribble, then always using var is Ok. But if you write production-level code, var is sloppy style and thus an absolute no-go. Before IntelliSense was able to automatically convert the var's in your source code to the correct type, var was even an evidence of a programmer not knowing what type they're using. Only exception: Use var to avoid writing the same type twice in the same line. Bad
List list = new List();
Good
var list = new List();
Because then, when you want to change the type, let's say from List to HashSet, you only have to change it once. IntelliSense also suggests this, at least in VS 2017.