Why don't we get an exception when we contact enum, which doesn't exist? [duplicate]
This question already has an answer here:
Is it possible to cast integer to enum? [duplicate]
6 answers
It is very interesting to me why this code will not give an exception and 12356 will be output to the console?
What is the meaning of this behavior enum?
enum Dummy { D1 = 123, D2 = 1234, D3 }
class Program
{
static void Main(string args)
{
Dummy d = (Dummy)12356;
Console.WriteLine((int)d);
Console.ReadKey();
}
}
Is this a bug or a feature?
c# .net enums
marked as duplicate by Crowcoder, Martin Liversage
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
Is it possible to cast integer to enum? [duplicate]
6 answers
It is very interesting to me why this code will not give an exception and 12356 will be output to the console?
What is the meaning of this behavior enum?
enum Dummy { D1 = 123, D2 = 1234, D3 }
class Program
{
static void Main(string args)
{
Dummy d = (Dummy)12356;
Console.WriteLine((int)d);
Console.ReadKey();
}
}
Is this a bug or a feature?
c# .net enums
marked as duplicate by Crowcoder, Martin Liversage
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I don't think it's a duplicate. This question asks why casting a random integer to an enum is allowed, as opposed to whether it is possible.
– GSerg
Nov 23 '18 at 11:50
That topic does not answer my question, so it is not a duplicate.
– Mikhail
Nov 23 '18 at 11:54
I agree with GSerg, although I have an interest in this question getting reopened, so I won't vote myself.
– Patrick Hofman
Nov 23 '18 at 11:59
It is consistent with the way other casts work in C#. Like from int to byte and that int stores a negative value or 256 or more. Mental model is "I wrote this code intentionally, don't bother double-checking that I got it right". These kind of checks are quite expensive, especially so for an enum. The only exception is casts on objects, they have to be checked since not doing will cause nasty undebuggable memory corruption problems.
– Hans Passant
Nov 23 '18 at 12:08
The direct duplicate is probably not the direct answer. But that answer is itself a duplicate. The duplicate marked by Patrick Hofman probably contains the answer you're looking for: Cast int to enum in C#. See atlaste's answer there (it could use an update, though).
– Jimi
Nov 23 '18 at 12:12
|
show 1 more comment
This question already has an answer here:
Is it possible to cast integer to enum? [duplicate]
6 answers
It is very interesting to me why this code will not give an exception and 12356 will be output to the console?
What is the meaning of this behavior enum?
enum Dummy { D1 = 123, D2 = 1234, D3 }
class Program
{
static void Main(string args)
{
Dummy d = (Dummy)12356;
Console.WriteLine((int)d);
Console.ReadKey();
}
}
Is this a bug or a feature?
c# .net enums
This question already has an answer here:
Is it possible to cast integer to enum? [duplicate]
6 answers
It is very interesting to me why this code will not give an exception and 12356 will be output to the console?
What is the meaning of this behavior enum?
enum Dummy { D1 = 123, D2 = 1234, D3 }
class Program
{
static void Main(string args)
{
Dummy d = (Dummy)12356;
Console.WriteLine((int)d);
Console.ReadKey();
}
}
Is this a bug or a feature?
This question already has an answer here:
Is it possible to cast integer to enum? [duplicate]
6 answers
c# .net enums
c# .net enums
edited Nov 23 '18 at 11:46
Patrick Hofman
126k18171229
126k18171229
asked Nov 23 '18 at 11:39
MikhailMikhail
31
31
marked as duplicate by Crowcoder, Martin Liversage
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Crowcoder, Martin Liversage
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I don't think it's a duplicate. This question asks why casting a random integer to an enum is allowed, as opposed to whether it is possible.
– GSerg
Nov 23 '18 at 11:50
That topic does not answer my question, so it is not a duplicate.
– Mikhail
Nov 23 '18 at 11:54
I agree with GSerg, although I have an interest in this question getting reopened, so I won't vote myself.
– Patrick Hofman
Nov 23 '18 at 11:59
It is consistent with the way other casts work in C#. Like from int to byte and that int stores a negative value or 256 or more. Mental model is "I wrote this code intentionally, don't bother double-checking that I got it right". These kind of checks are quite expensive, especially so for an enum. The only exception is casts on objects, they have to be checked since not doing will cause nasty undebuggable memory corruption problems.
– Hans Passant
Nov 23 '18 at 12:08
The direct duplicate is probably not the direct answer. But that answer is itself a duplicate. The duplicate marked by Patrick Hofman probably contains the answer you're looking for: Cast int to enum in C#. See atlaste's answer there (it could use an update, though).
– Jimi
Nov 23 '18 at 12:12
|
show 1 more comment
I don't think it's a duplicate. This question asks why casting a random integer to an enum is allowed, as opposed to whether it is possible.
– GSerg
Nov 23 '18 at 11:50
That topic does not answer my question, so it is not a duplicate.
– Mikhail
Nov 23 '18 at 11:54
I agree with GSerg, although I have an interest in this question getting reopened, so I won't vote myself.
– Patrick Hofman
Nov 23 '18 at 11:59
It is consistent with the way other casts work in C#. Like from int to byte and that int stores a negative value or 256 or more. Mental model is "I wrote this code intentionally, don't bother double-checking that I got it right". These kind of checks are quite expensive, especially so for an enum. The only exception is casts on objects, they have to be checked since not doing will cause nasty undebuggable memory corruption problems.
– Hans Passant
Nov 23 '18 at 12:08
The direct duplicate is probably not the direct answer. But that answer is itself a duplicate. The duplicate marked by Patrick Hofman probably contains the answer you're looking for: Cast int to enum in C#. See atlaste's answer there (it could use an update, though).
– Jimi
Nov 23 '18 at 12:12
I don't think it's a duplicate. This question asks why casting a random integer to an enum is allowed, as opposed to whether it is possible.
– GSerg
Nov 23 '18 at 11:50
I don't think it's a duplicate. This question asks why casting a random integer to an enum is allowed, as opposed to whether it is possible.
– GSerg
Nov 23 '18 at 11:50
That topic does not answer my question, so it is not a duplicate.
– Mikhail
Nov 23 '18 at 11:54
That topic does not answer my question, so it is not a duplicate.
– Mikhail
Nov 23 '18 at 11:54
I agree with GSerg, although I have an interest in this question getting reopened, so I won't vote myself.
– Patrick Hofman
Nov 23 '18 at 11:59
I agree with GSerg, although I have an interest in this question getting reopened, so I won't vote myself.
– Patrick Hofman
Nov 23 '18 at 11:59
It is consistent with the way other casts work in C#. Like from int to byte and that int stores a negative value or 256 or more. Mental model is "I wrote this code intentionally, don't bother double-checking that I got it right". These kind of checks are quite expensive, especially so for an enum. The only exception is casts on objects, they have to be checked since not doing will cause nasty undebuggable memory corruption problems.
– Hans Passant
Nov 23 '18 at 12:08
It is consistent with the way other casts work in C#. Like from int to byte and that int stores a negative value or 256 or more. Mental model is "I wrote this code intentionally, don't bother double-checking that I got it right". These kind of checks are quite expensive, especially so for an enum. The only exception is casts on objects, they have to be checked since not doing will cause nasty undebuggable memory corruption problems.
– Hans Passant
Nov 23 '18 at 12:08
The direct duplicate is probably not the direct answer. But that answer is itself a duplicate. The duplicate marked by Patrick Hofman probably contains the answer you're looking for: Cast int to enum in C#. See atlaste's answer there (it could use an update, though).
– Jimi
Nov 23 '18 at 12:12
The direct duplicate is probably not the direct answer. But that answer is itself a duplicate. The duplicate marked by Patrick Hofman probably contains the answer you're looking for: Cast int to enum in C#. See atlaste's answer there (it could use an update, though).
– Jimi
Nov 23 '18 at 12:12
|
show 1 more comment
2 Answers
2
active
oldest
votes
Is this a bug or a feature?
A feature, although it might bring you in curious circumstances when the code doesn't work as expected.
You could for example combine enum values, for example:
enum Dummy { D1 = 1, D2 = 2, D3 = 4 }
Dummy v = Dummy.D1 | Dummy.D2;
The backing value here is 3.
You can check for either of the values like this:
bool isD1 = (v & Dummy.D1) == Dummy.D1;
You could check your enum for single value validness like this:
Dummy v = Dummy.D1;
bool isValid = Enum.IsDefined(typeof(Dummy), v);
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
add a comment |
This is by design but discouraged, as specified in the documentation:
It's possible to assign any arbitrary integer value to meetingDay. For
example, this line of code does not produce an error: meetingDay =
(Day) 42. However, you should not do this because the implicit
expectation is that an enum variable will only hold one of the values
defined by the enum. To assign an arbitrary value to a variable of an
enumeration type is to introduce a high risk for errors.
You can always check if a given value is defined for an enum type by using IsDefined
:
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123)); //True
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123456)); //False
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this a bug or a feature?
A feature, although it might bring you in curious circumstances when the code doesn't work as expected.
You could for example combine enum values, for example:
enum Dummy { D1 = 1, D2 = 2, D3 = 4 }
Dummy v = Dummy.D1 | Dummy.D2;
The backing value here is 3.
You can check for either of the values like this:
bool isD1 = (v & Dummy.D1) == Dummy.D1;
You could check your enum for single value validness like this:
Dummy v = Dummy.D1;
bool isValid = Enum.IsDefined(typeof(Dummy), v);
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
add a comment |
Is this a bug or a feature?
A feature, although it might bring you in curious circumstances when the code doesn't work as expected.
You could for example combine enum values, for example:
enum Dummy { D1 = 1, D2 = 2, D3 = 4 }
Dummy v = Dummy.D1 | Dummy.D2;
The backing value here is 3.
You can check for either of the values like this:
bool isD1 = (v & Dummy.D1) == Dummy.D1;
You could check your enum for single value validness like this:
Dummy v = Dummy.D1;
bool isValid = Enum.IsDefined(typeof(Dummy), v);
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
add a comment |
Is this a bug or a feature?
A feature, although it might bring you in curious circumstances when the code doesn't work as expected.
You could for example combine enum values, for example:
enum Dummy { D1 = 1, D2 = 2, D3 = 4 }
Dummy v = Dummy.D1 | Dummy.D2;
The backing value here is 3.
You can check for either of the values like this:
bool isD1 = (v & Dummy.D1) == Dummy.D1;
You could check your enum for single value validness like this:
Dummy v = Dummy.D1;
bool isValid = Enum.IsDefined(typeof(Dummy), v);
Is this a bug or a feature?
A feature, although it might bring you in curious circumstances when the code doesn't work as expected.
You could for example combine enum values, for example:
enum Dummy { D1 = 1, D2 = 2, D3 = 4 }
Dummy v = Dummy.D1 | Dummy.D2;
The backing value here is 3.
You can check for either of the values like this:
bool isD1 = (v & Dummy.D1) == Dummy.D1;
You could check your enum for single value validness like this:
Dummy v = Dummy.D1;
bool isValid = Enum.IsDefined(typeof(Dummy), v);
answered Nov 23 '18 at 11:45
Patrick HofmanPatrick Hofman
126k18171229
126k18171229
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
add a comment |
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
This is a good guess, thanks. Unfortunately, I can not evaluate your answer :(
– Mikhail
Nov 23 '18 at 12:06
add a comment |
This is by design but discouraged, as specified in the documentation:
It's possible to assign any arbitrary integer value to meetingDay. For
example, this line of code does not produce an error: meetingDay =
(Day) 42. However, you should not do this because the implicit
expectation is that an enum variable will only hold one of the values
defined by the enum. To assign an arbitrary value to a variable of an
enumeration type is to introduce a high risk for errors.
You can always check if a given value is defined for an enum type by using IsDefined
:
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123)); //True
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123456)); //False
add a comment |
This is by design but discouraged, as specified in the documentation:
It's possible to assign any arbitrary integer value to meetingDay. For
example, this line of code does not produce an error: meetingDay =
(Day) 42. However, you should not do this because the implicit
expectation is that an enum variable will only hold one of the values
defined by the enum. To assign an arbitrary value to a variable of an
enumeration type is to introduce a high risk for errors.
You can always check if a given value is defined for an enum type by using IsDefined
:
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123)); //True
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123456)); //False
add a comment |
This is by design but discouraged, as specified in the documentation:
It's possible to assign any arbitrary integer value to meetingDay. For
example, this line of code does not produce an error: meetingDay =
(Day) 42. However, you should not do this because the implicit
expectation is that an enum variable will only hold one of the values
defined by the enum. To assign an arbitrary value to a variable of an
enumeration type is to introduce a high risk for errors.
You can always check if a given value is defined for an enum type by using IsDefined
:
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123)); //True
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123456)); //False
This is by design but discouraged, as specified in the documentation:
It's possible to assign any arbitrary integer value to meetingDay. For
example, this line of code does not produce an error: meetingDay =
(Day) 42. However, you should not do this because the implicit
expectation is that an enum variable will only hold one of the values
defined by the enum. To assign an arbitrary value to a variable of an
enumeration type is to introduce a high risk for errors.
You can always check if a given value is defined for an enum type by using IsDefined
:
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123)); //True
Console.WriteLine(Enum.IsDefined(typeof(Dummy), 123456)); //False
answered Nov 23 '18 at 11:48
KonamimanKonamiman
42.7k1598127
42.7k1598127
add a comment |
add a comment |
I don't think it's a duplicate. This question asks why casting a random integer to an enum is allowed, as opposed to whether it is possible.
– GSerg
Nov 23 '18 at 11:50
That topic does not answer my question, so it is not a duplicate.
– Mikhail
Nov 23 '18 at 11:54
I agree with GSerg, although I have an interest in this question getting reopened, so I won't vote myself.
– Patrick Hofman
Nov 23 '18 at 11:59
It is consistent with the way other casts work in C#. Like from int to byte and that int stores a negative value or 256 or more. Mental model is "I wrote this code intentionally, don't bother double-checking that I got it right". These kind of checks are quite expensive, especially so for an enum. The only exception is casts on objects, they have to be checked since not doing will cause nasty undebuggable memory corruption problems.
– Hans Passant
Nov 23 '18 at 12:08
The direct duplicate is probably not the direct answer. But that answer is itself a duplicate. The duplicate marked by Patrick Hofman probably contains the answer you're looking for: Cast int to enum in C#. See atlaste's answer there (it could use an update, though).
– Jimi
Nov 23 '18 at 12:12