|
For local variables, it's not necessary to repeat the type name:
var - C# Reference | Microsoft Docs[^]
Implicitly typed local variables - C# Programming Guide | Microsoft Docs[^]
The following two lines produce identical IL:
Animal fox = new Animal();
var fox = new Animal();
NB: You can't use var for fields, property types, method parameters, or return types.
Why no var on fields? – Fabulous Adventures In Coding[^]
There is a suggestion which would allow fields declared as: Animal fox = new(); , but it hasn't been implemented yet:
csharplang/target-typed-new.md at master · dotnet/csharplang · GitHub[^]
NB2: Some people vehemently oppose any use of var beyond anonymous types. And it certainly can be overused - for example, var x = Foo(); would compile, but is not readable. But for a new expression, where the type is right next to the variable declaration, I don't see any problem with using var .
NB3: To clarify, based on the responses: using var for new expressions is fine; you should generally avoid it for anything else.
var x = new SomeType(); is fine.var x = SomeMethod(); is bad.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 16-May-19 9:45am.
|
|
|
|
|
Personally, I'd recommend that beginners avoid var until they get to Linq, just to make it more obvious to them exactly what they are doing.
But at least it's less horrendous than misused dynamic ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Use "var" to help "keep you coding" if you're not sure what type is returned. (Methods; LINQ).
THEN, after intelli-sense has resolved the type, you can then "mouse over the var" and convert it to an explicit type ("quick action" refactor).
Perfect for a student (and memory-poor me), IMO.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Until you get to Linq, you should have a good idea what type you are using - particularly when you are just starting. I think explicit typing helps beginners rather than confuses them when they suddenly find it "won't pass x to method y" and can't understand why not.
But hey! I'm not going to start a flame war about it!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: I'm not going to start a flame war Oh come on, we need a bit of excitement sometimes.
|
|
|
|
|
Reminds of "History" classes ... memorizing dates before they meant something.
Different strokes.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Yes Gerry I've found intelli-sense very useful. Great tool tool for beginners.
Brian
|
|
|
|
|
I agree with OriginalGriff. Telling newbies to use var probably means that they will never use proper types.
|
|
|
|
|
Which is why I added the note that it's best saved for new expressions.
Bad: var x = Foo();
Good: var x = new Bar();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Which is why ... In the forlorn hope that your advice will be properly listened to.
|
|
|
|
|
There's a first time for everything.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
var is a training wheel for newbs (and me).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
But they are going to read about it in books any way.
If it makes coding easier then I'm for it. I can learn the more later.
Brian
|
|
|
|
|
Brian_TheLion wrote: I can learn the more later. Learning things in the wrong order is a sure way to failure.
|
|
|
|
|
Yes that is true but it's good to know that 'var' exists.
I find that I'm best at learning programming by studying medium sized programs. I like to step thru a program to see how it works. I also try to write a simple C# program from what I have learnt.
I suspect the biggest thing that turns beginners away from learning C# is when their program is fulled with errors and they have no idea on how to fix the problem or where they went wrong as they have tried to build a complex program before fully learning C#. Jumping in the deep end as they say.
Things like l1.addExit(new Exit(Exit.Directions.North, l2)); takes a bit of getting use to but I'm keen to learn and are starting to understand it more.
Brian
|
|
|
|
|
Brian_TheLion wrote: I suspect the biggest thing that turns beginners away from learning C# is when their program is fulled with errors When I see a student whose code is full of errors (beyond typos), I see a student who is not being guided properly, or, a student who is "flailing" because they have not grounded themselves in language basics, or don't know how to study in a disciplined way.
Once you make some progress in getting over the initial learning curve with C#, I predict you will look back on VB and Python as the messes they are
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Hi Bill.
I hope your right.
You do get some lift of confidence when the program runs without errors.
With me it's a case of getting out of the habit of script coding like I did with Basic and QuickBasic.
Brian
|
|
|
|
|
Thanks Richard for the var info.
Brian
|
|
|
|
|
Thanks for the links and info Richard.
Brian
|
|
|
|
|
Currently I'm using version 2015 if Visual Studio and was wondering if anything happened to my computer and I had to reinstall Visual Studio then would I still be able to install version 2015. I remember that you run a small program that caused it to download and install itself (unless there is a way to download the complete file for storage)
I was told that there were some issues with version 2017 so I'm staying with version 2015 at the moment.
Brian
|
|
|
|
|
Yes, you can: Download Older Visual Studio Software | Visual Studio - Visual Studio[^] and I'd agree that 2017 wasn't good (but neither was 2015 which is why my "revert to" version is 2013)
The latest though - 2019 - seems pretty solid to me so far, and has some very nice bits and bobs.
BUT. If you have several version on your system, then must be installed "earliest first" or they will give problems (MS says so, and my experience in the past bears that out)
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've been using 2017 for quite a while now and have not come across any issues. Maybe I don't use it enough.
|
|
|
|
|
Maybe it was just the early versions (or an interaction with the other crap on my system) but when I originally installed it, "flaky" was a good word to describe it. Weird UI glitches, disappearing code, crash to desktop, slow as heck - that's why I gave up and went back to 2013.
Glad to hear it didn't affect everybody.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've found 2017 to be solid; 2019 keeps changing my text editor settings; so much so, I'm staying with 2017.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code.
I could type this
Code One
<pre>public string Name
{
get;
set;
}</pre>
or I could type
Code Two
<pre>public string Name
{
get
{
return name;
}
set
{
name = value;
}</pre>
or I could type
Code Three
<pre>public void SetLength (double len)
{
length = len
}</pre>
And sometimes Code One and Code Three are used.
I think that one of these codes are used if I make the Variables private instead of public.
Any help would be welcome thanks
Brian
|
|
|
|