Jump to content

C# function varibales

MisterWhite
Go to solution Solved by Nineshadow,

Guess is not initalized.

Try

string guess = "";

i didn't have this error until i put all code in the function, so never thought it could be the case

 

IntelliSense should even warn you about that.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

We don't have that in C++ D:

 

I know, sorry I should't have put such an edge to the statement.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

I'd suggest not using a void, it's bad practice for this situation

 

 

public static void Main (string[] args){int m;Console.Write(Domath(m));}public static int Domath(int _var)// this place{return _var=2*3;} 
Link to comment
Share on other sites

Link to post
Share on other sites

In any event it's better to use the constant string.Empty.

Only if you find it more readable, which is a matter of preference.

Link to comment
Share on other sites

Link to post
Share on other sites

Only if you find it more readable, which is a matter of preference.

Not really since the definition of what an empty string means in the context of the language may change (not very likely granted). However it's still a feature... and anyway what's more readable than an explicit 'Empty'.

In any event most of the basic data types have such constants defined. Ignore them at your peril - it's your reputation at the end of the day.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Not really since the definition of what an empty string means in the context of the language may change (not very likely granted). However it's still a feature... and in any event what's more readable than an explicit 'Empty'.

In any event most of the basic data types have such constants defined. Ignore them at your peril - it's your reputation at the end of the day.

"Not very likely" is a significant understatement. As for what's more readable, like I said, that's a matter of preference. Some people prefer double quotes, others prefer Empty.
Link to comment
Share on other sites

Link to post
Share on other sites

"Not very likely" is a significant understatement. As for what's more readable, like I said, that's a matter of preference. Some people prefer double quotes, others prefer Empty.

 

But it is still possible and I have been around in the industry long enough to see it happen - Doubles and Reals in Pascal/Delphi for instance. Using the language provided constants will at least enforce that in the future when things do change that you should have consistent behaviour. Would you also manually type out -2,147,483,648 to 2,147,483,647 for the bounds of an int in C# if you had to?

 

As I said, it's your career at the end of the day so ignore them at your own peril.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

But it is still possible and I have been around in the industry long enough to see it happen - Doubles and Reals in Pascal/Delphi for instance. Using the language provided constants will at least enforce that in the future when things do change that you should have consistent behaviour. Would you also manually type out -2,147,483,648 to 2,147,483,647 for the bounds of an int in C# if you had to?

 

As I said, it's your career at the end of the day so ignore them at your own peril.

 

No, I wouldn't do that with int, but it's a good thing I never mentioned int or any type other than string. String.Empty is a bit different from Int32.MinValue or Int32.MaxValue which is why you must use "" in certain situations anyway.

I cannot for the life of me think of a situation where Microsoft would change the language/framework such that String.Empty was not equivalent to ""; this would be a significant (and nonsensical) breaking change no matter which one you use in your code. When I search this topic on Google, the overwhelming consensus appears to be that it makes no practical difference as I originally said. They few people who do have concerns are mostly worried about the performance difference between the two (which is virtually non-existent) or that String.Empty more clearly expresses developer intent (which I can sort of understand but don't necessarily agree with). I'm not seeing anyone worried about the prospect of "" suddenly changing to mean something other than a zero length string. But I suppose we'll have to agree to disagree.

Apologies to the OP for derailing the thread.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×