Is there any way to give c# enums a “range” of allowed values?
Say I have an enum like the following, where I want the numbering to start at 1000.
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
Assigning each Fruit
a number is tedious and hard to read, not to mention awkward if I have 20+ Fruit
s and might want to reorder them or add/remove a Fruit
. Is there any kind of syntax like the following?
public enum Fruits : uint where value >= 1000
{
Apple,
Orange,
Strawberry,
Banana,
Blueberry,
}
c# enums
|
show 1 more comment
Say I have an enum like the following, where I want the numbering to start at 1000.
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
Assigning each Fruit
a number is tedious and hard to read, not to mention awkward if I have 20+ Fruit
s and might want to reorder them or add/remove a Fruit
. Is there any kind of syntax like the following?
public enum Fruits : uint where value >= 1000
{
Apple,
Orange,
Strawberry,
Banana,
Blueberry,
}
c# enums
7
Set1000
only to first fruit, others fruits will have value incremented by 1
– Fabio
Nov 23 '18 at 1:28
2
Just define the value for your first element (Apple), and you are set. All further elements get values that will be incremented by one. The documentation forenum
will tell you this, too: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…. Or here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… Documentation is an amazing thing... ;-)
– elgonzo
Nov 23 '18 at 1:28
1
Note that even if you assign values, technically you can pass any integer as anenum
; it just won't correspond to your defined values. That's becasue internally the enum is just compiled to a native type (usuallyint
). Meaning if you have a function that accepts aFruit
parameter, you could pass any value to it; it doesn't have to be one of your defined values.
– D Stanley
Nov 23 '18 at 1:42
as soon as you start to store values from that enum somewhere (files, a database, ...) and need to maintain a certain degree of version compatibility, you will find that explicitely numbering them and never change a number once assigned is what you want.
– dlatikay
Nov 23 '18 at 2:23
Standard trick to get a range that's useful for validation is to add an extra member, Last = Blueberry. And perhaps you want First = Apple in this case.
– Hans Passant
Nov 23 '18 at 2:31
|
show 1 more comment
Say I have an enum like the following, where I want the numbering to start at 1000.
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
Assigning each Fruit
a number is tedious and hard to read, not to mention awkward if I have 20+ Fruit
s and might want to reorder them or add/remove a Fruit
. Is there any kind of syntax like the following?
public enum Fruits : uint where value >= 1000
{
Apple,
Orange,
Strawberry,
Banana,
Blueberry,
}
c# enums
Say I have an enum like the following, where I want the numbering to start at 1000.
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
Assigning each Fruit
a number is tedious and hard to read, not to mention awkward if I have 20+ Fruit
s and might want to reorder them or add/remove a Fruit
. Is there any kind of syntax like the following?
public enum Fruits : uint where value >= 1000
{
Apple,
Orange,
Strawberry,
Banana,
Blueberry,
}
c# enums
c# enums
asked Nov 23 '18 at 1:25
IamsodarncoolIamsodarncool
11416
11416
7
Set1000
only to first fruit, others fruits will have value incremented by 1
– Fabio
Nov 23 '18 at 1:28
2
Just define the value for your first element (Apple), and you are set. All further elements get values that will be incremented by one. The documentation forenum
will tell you this, too: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…. Or here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… Documentation is an amazing thing... ;-)
– elgonzo
Nov 23 '18 at 1:28
1
Note that even if you assign values, technically you can pass any integer as anenum
; it just won't correspond to your defined values. That's becasue internally the enum is just compiled to a native type (usuallyint
). Meaning if you have a function that accepts aFruit
parameter, you could pass any value to it; it doesn't have to be one of your defined values.
– D Stanley
Nov 23 '18 at 1:42
as soon as you start to store values from that enum somewhere (files, a database, ...) and need to maintain a certain degree of version compatibility, you will find that explicitely numbering them and never change a number once assigned is what you want.
– dlatikay
Nov 23 '18 at 2:23
Standard trick to get a range that's useful for validation is to add an extra member, Last = Blueberry. And perhaps you want First = Apple in this case.
– Hans Passant
Nov 23 '18 at 2:31
|
show 1 more comment
7
Set1000
only to first fruit, others fruits will have value incremented by 1
– Fabio
Nov 23 '18 at 1:28
2
Just define the value for your first element (Apple), and you are set. All further elements get values that will be incremented by one. The documentation forenum
will tell you this, too: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…. Or here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… Documentation is an amazing thing... ;-)
– elgonzo
Nov 23 '18 at 1:28
1
Note that even if you assign values, technically you can pass any integer as anenum
; it just won't correspond to your defined values. That's becasue internally the enum is just compiled to a native type (usuallyint
). Meaning if you have a function that accepts aFruit
parameter, you could pass any value to it; it doesn't have to be one of your defined values.
– D Stanley
Nov 23 '18 at 1:42
as soon as you start to store values from that enum somewhere (files, a database, ...) and need to maintain a certain degree of version compatibility, you will find that explicitely numbering them and never change a number once assigned is what you want.
– dlatikay
Nov 23 '18 at 2:23
Standard trick to get a range that's useful for validation is to add an extra member, Last = Blueberry. And perhaps you want First = Apple in this case.
– Hans Passant
Nov 23 '18 at 2:31
7
7
Set
1000
only to first fruit, others fruits will have value incremented by 1– Fabio
Nov 23 '18 at 1:28
Set
1000
only to first fruit, others fruits will have value incremented by 1– Fabio
Nov 23 '18 at 1:28
2
2
Just define the value for your first element (Apple), and you are set. All further elements get values that will be incremented by one. The documentation for
enum
will tell you this, too: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…. Or here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… Documentation is an amazing thing... ;-)– elgonzo
Nov 23 '18 at 1:28
Just define the value for your first element (Apple), and you are set. All further elements get values that will be incremented by one. The documentation for
enum
will tell you this, too: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…. Or here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… Documentation is an amazing thing... ;-)– elgonzo
Nov 23 '18 at 1:28
1
1
Note that even if you assign values, technically you can pass any integer as an
enum
; it just won't correspond to your defined values. That's becasue internally the enum is just compiled to a native type (usually int
). Meaning if you have a function that accepts a Fruit
parameter, you could pass any value to it; it doesn't have to be one of your defined values.– D Stanley
Nov 23 '18 at 1:42
Note that even if you assign values, technically you can pass any integer as an
enum
; it just won't correspond to your defined values. That's becasue internally the enum is just compiled to a native type (usually int
). Meaning if you have a function that accepts a Fruit
parameter, you could pass any value to it; it doesn't have to be one of your defined values.– D Stanley
Nov 23 '18 at 1:42
as soon as you start to store values from that enum somewhere (files, a database, ...) and need to maintain a certain degree of version compatibility, you will find that explicitely numbering them and never change a number once assigned is what you want.
– dlatikay
Nov 23 '18 at 2:23
as soon as you start to store values from that enum somewhere (files, a database, ...) and need to maintain a certain degree of version compatibility, you will find that explicitely numbering them and never change a number once assigned is what you want.
– dlatikay
Nov 23 '18 at 2:23
Standard trick to get a range that's useful for validation is to add an extra member, Last = Blueberry. And perhaps you want First = Apple in this case.
– Hans Passant
Nov 23 '18 at 2:31
Standard trick to get a range that's useful for validation is to add an extra member, Last = Blueberry. And perhaps you want First = Apple in this case.
– Hans Passant
Nov 23 '18 at 2:31
|
show 1 more comment
1 Answer
1
active
oldest
votes
As @Fabio stated in a comment, enum auto-values are based off the value of the previous enum, so the following code snippets compile identically:
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
public enum Fruits : uint
{
Apple = 1000,
Orange,
Strawberry,
Banana,
Blueberry,
}
Thank you @Fabio!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439665%2fis-there-any-way-to-give-c-sharp-enums-a-range-of-allowed-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @Fabio stated in a comment, enum auto-values are based off the value of the previous enum, so the following code snippets compile identically:
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
public enum Fruits : uint
{
Apple = 1000,
Orange,
Strawberry,
Banana,
Blueberry,
}
Thank you @Fabio!
add a comment |
As @Fabio stated in a comment, enum auto-values are based off the value of the previous enum, so the following code snippets compile identically:
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
public enum Fruits : uint
{
Apple = 1000,
Orange,
Strawberry,
Banana,
Blueberry,
}
Thank you @Fabio!
add a comment |
As @Fabio stated in a comment, enum auto-values are based off the value of the previous enum, so the following code snippets compile identically:
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
public enum Fruits : uint
{
Apple = 1000,
Orange,
Strawberry,
Banana,
Blueberry,
}
Thank you @Fabio!
As @Fabio stated in a comment, enum auto-values are based off the value of the previous enum, so the following code snippets compile identically:
public enum Fruits : uint
{
Apple = 1000,
Orange = 1001,
Strawberry = 1002,
Banana = 1003,
Blueberry = 1004,
}
public enum Fruits : uint
{
Apple = 1000,
Orange,
Strawberry,
Banana,
Blueberry,
}
Thank you @Fabio!
answered Nov 28 '18 at 1:03
IamsodarncoolIamsodarncool
11416
11416
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439665%2fis-there-any-way-to-give-c-sharp-enums-a-range-of-allowed-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
Set
1000
only to first fruit, others fruits will have value incremented by 1– Fabio
Nov 23 '18 at 1:28
2
Just define the value for your first element (Apple), and you are set. All further elements get values that will be incremented by one. The documentation for
enum
will tell you this, too: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…. Or here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… Documentation is an amazing thing... ;-)– elgonzo
Nov 23 '18 at 1:28
1
Note that even if you assign values, technically you can pass any integer as an
enum
; it just won't correspond to your defined values. That's becasue internally the enum is just compiled to a native type (usuallyint
). Meaning if you have a function that accepts aFruit
parameter, you could pass any value to it; it doesn't have to be one of your defined values.– D Stanley
Nov 23 '18 at 1:42
as soon as you start to store values from that enum somewhere (files, a database, ...) and need to maintain a certain degree of version compatibility, you will find that explicitely numbering them and never change a number once assigned is what you want.
– dlatikay
Nov 23 '18 at 2:23
Standard trick to get a range that's useful for validation is to add an extra member, Last = Blueberry. And perhaps you want First = Apple in this case.
– Hans Passant
Nov 23 '18 at 2:31