zero int?
-
The declaration and initialisation could of course have been done in one line, but that makes little difference. From this I get the impression that the developer who wrote this is very 'modern'. He probably has no idea what the compiler will generate from those simple code lines and added the type cast just in case this assignment might be problematic. Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment. Bottom line: I see this as clumsy code, but not as a real horror. It does what it is supposed to and I see no potentially harmful side effects. However, I would also see it as an indicator that the developer should perhaps learn more about what happens under the hood, even if that's not 'modern'.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.I think, someone may well correct me, that the compiler will ignore the second line as it is repeating the prior step because .net initialises all numbers to zero if no value is explicitly supplied.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
b10543748 wrote:
Int32 contador;
// contador = (Int32)0;ftfy The second line is pointless.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
I think, someone may well correct me, that the compiler will ignore the second line as it is repeating the prior step because .net initialises all numbers to zero if no value is explicitly supplied.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
No. You would get the 'use of an unassigned variable' error and it would not compile. Edit: To be precise: I made two assumptions: 1) This variable is declared inside a method, not as a class member. Initializing it separately leaves no other possibility than that. 2) If you declare a variable inside a method and don't initialize it, it's still ok for the compiler as long as you don't try to use it in the following code. Such an unused declaration would only trigger a compiler warning. The error would occur as soon as you tried to use the uninitialized variable (other than initializing it) in the following code.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
b10543748 wrote:
Int32 contador;
// contador = (Int32)0;ftfy The second line is pointless.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
A method to try it is written quickly. If the method only contains this declaration, it would work, but also be somewhat useless. Then add a single line that uses the variable and see what the compiler says :) The second line is not pointless at all, at least if you intend to use the variable. If not, why declare it in the first place?
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
A method to try it is written quickly. If the method only contains this declaration, it would work, but also be somewhat useless. Then add a single line that uses the variable and see what the compiler says :) The second line is not pointless at all, at least if you intend to use the variable. If not, why declare it in the first place?
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.Sorry, I was misreading it.
Int32
is an object type and so does need to be initialised in code; I was thinking of value typeint
s
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Sorry, I was misreading it.
Int32
is an object type and so does need to be initialised in code; I was thinking of value typeint
s
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Int32 is a struct, not a class, making it a value type and not a reference type. It's also the underlying type in the framework for int, as far as I know you can use them synonymously.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
Sorry, I was misreading it.
Int32
is an object type and so does need to be initialised in code; I was thinking of value typeint
s
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
It isn't, as noted, but it also wouldn't matter if it were. Local variables of reference types also "have to" be initialized. That is, they must satisfy the Definite Assignment rules. If it were a field in a struct/class or an array-element it wouldn't have to be initialized, but that is the case for any type; they're always set to default(type) in those cases.
-
Sorry, I was misreading it.
Int32
is an object type and so does need to be initialised in code; I was thinking of value typeint
s
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Int32 is a struct, not a class, making it a value type and not a reference type. It's also the underlying type in the framework for int, as far as I know you can use them synonymously.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.Well, actually, we don't know... it could be an Acme.Int32, the provided code doesn't specify. :rolleyes:
-
Well, actually, we don't know... it could be an Acme.Int32, the provided code doesn't specify. :rolleyes:
True. In that case it would be a horror. Using a class or a struct with the name of a type defined in the framework without fully qualifiying the type to point out that it is not the type you would normally assume it to be, is downright deceptive. To be able to do that you also have to leave out the system namespace where the type in question is located and only use the namespace where the 'Acme' type is located. Include both and you have no choice but to fully qualify the types in your code.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
True. In that case it would be a horror. Using a class or a struct with the name of a type defined in the framework without fully qualifiying the type to point out that it is not the type you would normally assume it to be, is downright deceptive. To be able to do that you also have to leave out the system namespace where the type in question is located and only use the namespace where the 'Acme' type is located. Include both and you have no choice but to fully qualify the types in your code.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.I have just made this code up to show how horrific you can make it Define my own type using a name defined in the framework
namespace Dummy
{
public class Int32
{
public Int32()
{
}public string Value { get; set; } }
}
Implement it somewhere
using system = Dummy; // note the lower case 's'
namespace AnotherDummy
{
public class Class1
{
public Class1()
{
string value = "Hello";system.Int32 x = new system.Int32(); x.Value = value; } }
}
-
I have just made this code up to show how horrific you can make it Define my own type using a name defined in the framework
namespace Dummy
{
public class Int32
{
public Int32()
{
}public string Value { get; set; } }
}
Implement it somewhere
using system = Dummy; // note the lower case 's'
namespace AnotherDummy
{
public class Class1
{
public Class1()
{
string value = "Hello";system.Int32 x = new system.Int32(); x.Value = value; } }
}
Long ago we did our best to write misleading code with help of the C preprocessor. Then Java came along and they celebrated the end of such malicious practices. C# has a preprocessor (as all directives beginning with '#' clearly show), but it is harder to use in a destructive way. Still, your example shows that it was not the preprocessor that was the root of all evil.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
I have just made this code up to show how horrific you can make it Define my own type using a name defined in the framework
namespace Dummy
{
public class Int32
{
public Int32()
{
}public string Value { get; set; } }
}
Implement it somewhere
using system = Dummy; // note the lower case 's'
namespace AnotherDummy
{
public class Class1
{
public Class1()
{
string value = "Hello";system.Int32 x = new system.Int32(); x.Value = value; } }
}
That's bad, but it can be worse:
namespace Dummy
{
public struct Int32
{
int val;private Int32(int v) { val = v; } public static implicit operator Int32(int x) { return new Int32(x - 3); } public static implicit operator int(Int32 x) { return x.val; } public static Int32 operator \*(Int32 a, Int32 b) { return new Int32(a.val \* (b.val + 1)); } // etc }
}
-
That's bad, but it can be worse:
namespace Dummy
{
public struct Int32
{
int val;private Int32(int v) { val = v; } public static implicit operator Int32(int x) { return new Int32(x - 3); } public static implicit operator int(Int32 x) { return x.val; } public static Int32 operator \*(Int32 a, Int32 b) { return new Int32(a.val \* (b.val + 1)); } // etc }
}
-
The declaration and initialisation could of course have been done in one line, but that makes little difference. From this I get the impression that the developer who wrote this is very 'modern'. He probably has no idea what the compiler will generate from those simple code lines and added the type cast just in case this assignment might be problematic. Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment. Bottom line: I see this as clumsy code, but not as a real horror. It does what it is supposed to and I see no potentially harmful side effects. However, I would also see it as an indicator that the developer should perhaps learn more about what happens under the hood, even if that's not 'modern'.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.CDP1802 wrote:
Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned.
That's definitely true of assigning 0 to a variable. But there's a related case that's problematic: sqlParams.Add(new SqlParameter("Quantity", 0)); Acutally assigns the "Quantity" parameter a value of null, because apparently this fits the definition for
SqlParameter(string parameterType, SqlDbType dbType)
better than it does for
SqlParamter(string parameterType, object value)
because 0 is a valid value for the enum SqlDbType and any match is a better match than object. To assign a value of 0, you have to do: sqlParams.Add(new SqlParameter("Quantity", Convert.ToInt32(0))); (as for why you would do this... well, I'd rather not go into it...) So maybe the original coder was confused by that very specific case? ...probably not...
-
The declaration and initialisation could of course have been done in one line, but that makes little difference. From this I get the impression that the developer who wrote this is very 'modern'. He probably has no idea what the compiler will generate from those simple code lines and added the type cast just in case this assignment might be problematic. Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment. Bottom line: I see this as clumsy code, but not as a real horror. It does what it is supposed to and I see no potentially harmful side effects. However, I would also see it as an indicator that the developer should perhaps learn more about what happens under the hood, even if that's not 'modern'.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment.
assuming you aren't needing to deal with the IEEE arithmetic concepts of +0 and -0
-
Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment.
assuming you aren't needing to deal with the IEEE arithmetic concepts of +0 and -0
-
Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment.
assuming you aren't needing to deal with the IEEE arithmetic concepts of +0 and -0
-
Ironically, assingning zero to a variable of a numeric type is the most unproblematic case of all, since it turns out to be one or more zero bytes, no matter if we are looking at an integer type, a floating point type, signed or unsigned. The compiler knows the size (in bytes) of the variable the value is assigned to and there are no special ways to represent the number 'zero'. Therefore no special type information is needed in the assignment.
assuming you aren't needing to deal with the IEEE arithmetic concepts of +0 and -0