c# var
-
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!
I kind of find it easier to read with var first. But this is more secure, since you have to know what you are going to need and the you create the new instance in the lazy mode. Best of both options, I guess
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
-
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!
OriginalGriff wrote:
that would be a more natural way of showing what the type
Yup. And as I replied on the Insider News, what I really want is:
var foo = new();
In most cases, the compiler should be able to figure out whatfoo
is by inspecting its usage in the code! :laugh:Latest Articles:
Abusing Extension Methods, Null Continuation, and Null Coalescence Operators -
Ah, the use of var. I know that some people use var to do code alignment on variable names; the idea being that it's easier to scan the name if it lines up.
private void DoSomething()
{
var apiEndpoint = new Uri("http://someapi");
var retryCount = 10;
var longClassName = new ThisIsAReallyLongNameThatWouldReallyScrewUpTheClassNameDeclaration();
// body of the method.
}Pete O'Hanlon wrote:
the idea being that it's easier to scan the name if it lines up.
:thumbsup::thumbsup: That's my main argument (readability) and why I like it. On the other hand, incoming Option #3[^] is good too. It is a bit less readable, but still "forces" you to by type strong but combined with the lazyness / don't break the lines with long names too.
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
-
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.
is it lazy. Sure is. Does it work. Unfortunately yes and Fortunately yes. AS mentioned already when refactoring code and say a variable changes from int to long or even to a string it makes sure the code continues to work. I don't like it. but that does not mean I have never used it. I have and I hate myself for it. But I remember one time I had to write a bit of code. The co-workers insisted that the variable would always be an integer. ALWAYS they said. having some experience I knew probably gonna change. About a year later system requirements changed. I had used var and the code still worked. sooooooo tldr; lazy yes, flexible yes, hate myself yes.
To err is human to really mess up you need a computer
-
it is good for a lot of applications but can be missused very easily. it's main purpose is code readability.
MyMostExcellentBaseClassOfAllClassesInTheWholeWorld myClass = new MyMostExcellentBaseClassOfAllClassesInTheWholeWorld();
or
var myClass = new MyMostExcellentBaseClassOfAllClassesInTheWholeWorld();
It is really good when working with Linq, etc. as well.
Slacker007 wrote:
it's main purpose is code readability.
Its main purpose is actually anonymous types.
-
Slacker007 wrote:
it's main purpose is code readability.
Its main purpose is actually anonymous types.
I guess it could be used for both, but most people use it for implicit, which by nature adds to code readability for most. Edit: implicit and anonymous. but when I use it, I am not thinking "anonymous" or "Implicit" etc, I am thinking code readability. var - C# Reference | Microsoft Docs[^] c# - Implicitly Typed vs Anonymous Type - Stack Overflow[^]
-
is it lazy. Sure is. Does it work. Unfortunately yes and Fortunately yes. AS mentioned already when refactoring code and say a variable changes from int to long or even to a string it makes sure the code continues to work. I don't like it. but that does not mean I have never used it. I have and I hate myself for it. But I remember one time I had to write a bit of code. The co-workers insisted that the variable would always be an integer. ALWAYS they said. having some experience I knew probably gonna change. About a year later system requirements changed. I had used var and the code still worked. sooooooo tldr; lazy yes, flexible yes, hate myself yes.
To err is human to really mess up you need a computer
The problem with that is that you are working to what should have been a breaking change. And that's important, because if you were assuming int division as a result of the original type spec for example, then a breaking change means you know it's just failed and can adapt to it.
var
in that case means you don't know, and output can be subtly wrong and unnoticed until it's a real problem. I'd say var should be there for temporary storage of Linq results, and nowhere else ..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
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.
That's just pure laziness.
-
ZurdoDev wrote:
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.
I felt the same way initially, and I agree, for something simple like a native type, I rarely use var. However:
var complicatedDictionary = new Dictionary>();
and worse, I like definitely like it.Latest Articles:
Abusing Extension Methods, Null Continuation, and Null Coalescence Operators+1 For native types, I don't see the point in using var. I also subscribe to the idea of using: var x = new SomeMoreComplexType(); I don't mind: var x = SomeFunction(); ...if *I* don't particularly case about the type returned, for example, if I only use it to forward to some other function and I'm not looking at any of its members myself.
-
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.
ZurdoDev wrote:
var url = "http://someapi";
Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Is it a string? Could it be a URI[^]
Uri siteUri = new Uri("http://www.contoso.com/");
Director of Transmogrification Services Shinobi of Query Language Master of Yoda Conditional
-
ZurdoDev wrote:
var url = "http://someapi";
Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Is it a string? Could it be a URI[^]
Uri siteUri = new Uri("http://www.contoso.com/");
Director of Transmogrification Services Shinobi of Query Language Master of Yoda Conditional
-
I use
auto
, the C++ equivalent, whenever it will deduce the correct type. - It forces you to initialize the variable. - If the variable is initialized by calling a function, and that function's return type changes, the code might not even be affected. - Some type names are long or complicated, and I hate spilling lines. Some argue that it makes the code harder to understand because the reader has to figure out the type. My counterargument is that the reader doesn't understand how the code fits into the system if this is the case.Robust Services Core | Software Techniques for Lemmings | Articles
You are going on a dangerous path. It leads you to the days when an insignificant dot was significant:
auto i = 2;
auto j = 3;
auto k = i/j;
auto f = 2./j;
// f != k
// any similarity with FORTRAN is intentionalBesides "auto" has four letters and "int" has three; it's not energy efficient :)
Mircea
-
You are going on a dangerous path. It leads you to the days when an insignificant dot was significant:
auto i = 2;
auto j = 3;
auto k = i/j;
auto f = 2./j;
// f != k
// any similarity with FORTRAN is intentionalBesides "auto" has four letters and "int" has three; it's not energy efficient :)
Mircea
Not very dangerous unless one ignores compiler warnings. Then again, I don't use floating point very often. The main one I have to watch for is
for(auto i = 0...
which makes
i
anint
when what might actually be called for is asize_t
.Robust Services Core | Software Techniques for Lemmings | Articles
-
Yes, it was 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.
-
But what if you meant it as a URI? I think SQL would call this ambiguous
Director of Transmogrification Services Shinobi of Query Language Master of Yoda Conditional
MadMyche wrote:
But what if you meant it as a URI?
Then you do new Uri(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.
When I moved from VB (where purists dump on var from a great height) to c# where explicit types were mandatory I lost all faith in
var
, it became the one thing in VB that I loathed. Now that it has phoenixed in c# I refuse to use it. As forvar
content changing type and not having to refactor, what a croc of shit, you should HAVE to refactor if you change type or at least check your usage of the variable.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!
-
The problem with that is that you are working to what should have been a breaking change. And that's important, because if you were assuming int division as a result of the original type spec for example, then a breaking change means you know it's just failed and can adapt to it.
var
in that case means you don't know, and output can be subtly wrong and unnoticed until it's a real problem. I'd say var should be there for temporary storage of Linq results, and nowhere else ..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
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 use
var
almost exclusively these days. I started out using it sometimes, but then I started using it more and more. To me, it just seems nice to have all myvars
lined up nicely :D Some people thing thevar
keyword isn't strongly typed, but it is (we havedynamic
for that). It's not like JavaScript'svar
keyword. It's only absolutely necessary to usevar
when working with anonymous types (still strongly typed!).Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
When I moved from VB (where purists dump on var from a great height) to c# where explicit types were mandatory I lost all faith in
var
, it became the one thing in VB that I loathed. Now that it has phoenixed in c# I refuse to use it. As forvar
content changing type and not having to refactor, what a croc of shit, you should HAVE to refactor if you change type or at least check your usage of the variable.Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
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