+9  A: 

It is redundant, but I would stay consistent. If you are always initializing fields, keep it that way regardless of the type. Also, as you pointed out in a comment, being required to think about default values for certain types is distracting, so another reason to initialize first.

Ed Swangren
*Every* type has a default value. For nullable types (whether nullable value types or reference types) the default value is the null value.
Jon Skeet
That's true, that's why I added in that addition to the first sentence. Setting a reference type to null would also be redundant.
Ed Swangren
Does anyone else find having to even think about what default value of each time is kind of gets in the way?
Sung Meister
OK, removed that altogether as it was misleading.
Ed Swangren
@Ed: I guess setting "null" for reference types looks redundant. But it sometimes helps to distinguish whether a type is a reference type or a value type.(like a structure, especially)
Sung Meister
@Jon: What about Structs? "DateTime bla; DoSomething(bla)" => "Compiler Error: Use of initialized Value bla"
Michael Stum
@Michael, datetime's default is 01/01/0001 12:00:00 AM. You have to new it up first... Just doing DateTime bla reserves space for it - not creating it. Structs won't compile unless all of their fields are initialized once the constructor finishes.
SnOrfus
+2  A: 

Personally, I like this since you're saying to the world "I specifically want this to be 0". This reduces confusion when working in a team.

Jon B
+4  A: 

If you find it more readable that way, that's fine. I don't think it'll affect the JITted code - and even if it does, the performance hit will be absolutely tiny.

If ReSharper is annoying you when it comes to this warning, just configure it not to treat it as a problem. I don't have ReSharper on this machine, but I think it's fairly easy to find the relevant options - let me know if you can't and I'll look for it when I'm on the right machine.

Jon Skeet
Great to hear that performance hit will be tiny. Let me look for the option to turn that warning off
Sung Meister
Would the performance hit be a constant 'one single computer tick'?
I wouldn't like to make any guarantees about the cost - but quite possibly :)
Jon Skeet
@Jon: Actually performance was not much of a concern. It was just that I wanted to know if it is better to be more explicit in code or not. But since I found another question already, I will be closing this one.Thanks for the post
Sung Meister
Downvoters: without comments, downvotes are relatively pointless.
Jon Skeet
A: 

I would say it's bad practice not to explicitly define the field. An uninitialized field should not be assumed to be the default value, it should be assumed as "null". In this case, you want it to explicitly be 0, and you should therefore declare it to be 0.

Resharper is a very strict coding style, and you shouldn't blindly listen to it, especially if it damages the readability of your code.

Furthermore, if someone were to need to port this to a different language (where perhaps the default value is undefined, rather than "0") then it would cause uninitialized variable compiler errors or, even worse, run-time errors.

I don't see what's wrong with relying on features which the language spec guarantees - and whenever you port code you need to take into account differences in behaviour. This would be a pretty simple one to spot compared with, say, the order of initialization between field initializers and (cont)
Jon Skeet
base class constructors. (For example, C# and Java differ here.) For a similar case, would you not assume the values of elements in a newly created array object either?
Jon Skeet
@Jon Skeet: I concure
Bobby Cannon
Perhaps this is more coding philosophy than anything else, but I would prefer exposing the methods you use. In relying on the default value, you are obfuscating the initialization.
A: 

If it like Java at all (on a cell phone... Hard to look up) then explicitly initializing it means you cannot mark the field as final (might be const in C#) which makes it harder to properly make an immutible class.

TofuBeer
Why would it be hard to make an immutable class with explicitly initialized field value? Wouldn't it be easier on the other hand?
Sung Meister
Looks like readonly is the same general idea as final in Java. So if you make a field that is not readonly you can change it - which means the object is not immutable.
TofuBeer
oh... and I am assuming the value is reset in the constructor...
TofuBeer
Yes, the value gets reset in the constructor. Well, I can expose it as a read-only property ("getter" method in Java) to outside world though.
Sung Meister
A: 

The general answer is like others have stated, if it makes more sense to you and your team then reset the Reshaper setting to a hint. The C# will initialize all values to their default value (0 for numbers, null for reference types) which is equivalent to the .NET 2.0 default operator:

int i = default(int);

EDIT: You wouldn't use the above in your code. It is equivalent to 'i = 0', however it is much less readable. I only used it to illustrate the point about the unnassigned variables are initialized with their default value.

CodeMonkeyKing
"default(int)" sounds great but I think "0" would be more readable, instead of having to think what default value of "int" is as you skim through code... But I think i will surely take advantage of your advice. Thank you
Sung Meister
I totally agree with you. I've included an edit to reflect your point.
CodeMonkeyKing
A: 

When I see unneeded initialization I begin to wonder a little bit whether the person writing the code really understood .NET...and what other problems might be lurking in this code.

Dan
The point I wanted make was a "readability". I don't think I should keep on thinking about what default value of each type is. I do not want a subtle bug creep in by assuming that a certain type has a certain default value - Sorry. I am not too fond of "clever" code but a fan of readable code.
Sung Meister
And if you know .NET, there's really not much "thinking" about the default value.
Dan
A: 

I guess it's a matter of taste, but as I've said elsewhere, I don't like redundancy.

Leaving off initializer for a field could either mean that the coder knew it would be initialized to default values, or that he knew it would not be initialized and didn't care, or else he didn't think about it and his code just happens to work. The latter two would be, shall we say, "educational opportunitites".

On the other hand, initializing a field to its default value says to me that the coder doesn't understand the platform, or doesn't trust it.

What about cases where the default value is never used? Cases where the field is initialized from all constructors, or where the field is always set before being read? In those cases, setting to the default value is not an indication of intent - only an indication of following a rule that makes no sense.

John Saunders
+1  A: 

Initializing the fields might imply a performance penalty:

iulianchira
Those links seem like a great article to read in the afternoon. Thanks.
Sung Meister
A: 

'And if you know .NET, there's really not much "thinking" about the default value. – Dan Mar 17 '09 at 11:53'

To Dan,

Do you really begin to wonder if someone understands .NET if they have to explicitly declare? I actually begin to wonder about people who questions other peoples coding ethic especially it is commonly a good practice and had no detriment in the final product. I explicitly declare everything because that is the way I code, that's the way I like to see it and, when I sell my code (not very often but I do), that is the way the customer likes to see it. I do understand .NET. Explicitly declaring is not a 'problem lurking in code'.

Robert