|
Thanks Griff.
I liked examples like these you've given me.
You wrote::
eagle = new Sparrow()
I thought you may have to use:
Bird eagle = new Sparrow()
.
Is it because you have already defined the eagle as a bird (Bird eagle = new Eagle();) that you don't need to do this?
Brian
|
|
|
|
|
If you do this:
private void DoSummat()
{
Bird eagle = new Eagle();
...
eagle = new Sparrow();
...
} You are just reusing the existing variable, like you do without worrying about classes:
int i = 1;
while (true)
{
Console.WriteLine(i);
i = i + 1;
} If you tried to create a new variable with the same name in the same scope:
private void DoSummat()
{
Bird eagle = new Eagle();
...
Bird eagle = new Sparrow();
...
} You would get an error as it already exists - just the same as you can't do this:
for (int i = 0; i < 10; i++)
{
Console.Write($"{i} :");
for (int i = 100; i < 110; i++)
{
Console.Write(i);
}
Console.WriteLine();
} You can't have two loop guards with the same name!
Or even this:
private void DoSummat(int x)
{
int x = 666;
...
} Because the parameter and variable are the same name.
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!
|
|
|
|
|
Now I've had some coffee, I'll add some thoughts for you.
If you allowed
fox = new animal(); without the type specifier, what problems might it cause?
Well, consider this:
public animal fox { get; set; }
public void DoSummat(animal a)
{
fox = a;
...
} Is the fox assignment inside DoSummat meant to create a new local instance which is scoped to just the method, or to affect the public property? How could you tell? How could I tell when I tried to fix a problem in 100,000 lines of code next year?
Worse, is fox supposed to be an animal , or a mammal , or a Canidae ? Does it make a difference? Yes - it does. Because bird is a type of animal but a fox doesn't have a FlapWings method! So if you allow implicit typing, then the compiler has to assume the lowest common type in the hierarchy and that could mean that methods you expect to work just don't exist for that actual instance.
And consider this:
public void DoSummat(animal a)
{
fox = a;
...
fax = b;
...
} Isfax a new variable, or did I mistype fox ? How would you tell?
C# is a strongly typed language: which means that there is no implicit declarations, no automatic type changing (except where data won't be lost such as int to double for example), and heavy duty type checking - which makes your code more robust because it finds problems at compile time, instead of hoping you tested all the possible routes through the code and didn't leave a "bad type" in there.
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 do like the way you need to define all variables in C# unlike Python where you could start by attaching a string to a variable then later in the code attach a number to the same variable. Python accepts this without giving any errors
It can cause lots of problems in getting a program to work correctly.
Speaking of languages I wonder what happened to Visual Basic which was once a popular language. It's no longer in the top 10 popular languages.
I suspect C# would be popular with people who have learnt C and C++ as it's simlar in some ways to those languages.
I decided to learn C# as I wanted a language to create programs in Windows (that were exe) that had a console and would run fast. Also I prefer compiled code to scripting code.
Brian
|
|
|
|
|
Some good caffeine in there !
«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
|
|
|
|
|
Heavy duty wake up juice!
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: without the type specifier, what problems might it cause?
Hmmm...I can see the compiler struggling a bit with the following....
var fox = new;
|
|
|
|
|
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
|
|
|
|