This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get Enum from Description attribute
Hi All,
I have and Enum defined like this.
public enum SomeType {
[Description("One Value")]
One,
[Description("Two Value")]
Two,
[Description("Three Value")]
Three
}
but when I try to parse a string like this
SomeType test = (SomeType )Enum.Parse(typeof(SomeType ), "Three Value");
I get excetion "Requested value 'Three Value' was not found". Isn't this supposed to work ?
Thanks
No, it's not. You can find the Enum by the enum Name ("One", "Two", "Three"), but not by Description (at least not that way). Maybe via Reflection...
You might wanna take a look at this: How to get C# Enum description from value?
Update
Take a look at #KIvanov's comment and look here: Get Enum from Description attribute
As far as I know
SomeType test = (SomeType )Enum.Parse(typeof(SomeType ), "Three");
would do what you want
Related
This question already has answers here:
String Interpolation with format variable
(7 answers)
Closed 7 years ago.
How do I manage to assign a value to IFormattableString ?
Is it not possible to use as follows
string name = "Booley";
string template = "Hi mr. {name}, hope you enjoy the chicken!";
Console.WriteLine(template,name);
Output
Hi mr. Booley, hope you enjoy the chicken!
I know how to achieve this, but I'm curious why you cannot use the new C# 6.0 feature to make this. Variable names only available at compile time ?
You miss $ before the string. It should be string template = $"Hi mr. {name}, hope you enjoy the chicken!";
This question already has answers here:
What is the difference between String and string in C#?
(66 answers)
Closed 9 years ago.
Sorry about this dumb question, but i´ve recently found out that you can declare a String or string variable at C#. I would like to know the difference between them, which one is used at specific situations, etc. Thanks for the help and for your time.
There is no difference String is the Class name and string is the alias.
The generaly rule of thumb that I have followed is that if you declare a variable use the alias.
string foo = "bar";
If you call a method, use the class name
String.IsNullOrEmpty("");
It is exactly the same for the following"
Boolean and bool
Int32 and int
Double and double
etc
This question already has answers here:
String to enum conversion in C#
(8 answers)
C# using numbers in an enum
(8 answers)
Closed 9 years ago.
HI I have the following enum
public enum Priority : byte
{
A=1,
B+ = 2,
B=4,
C=8,
D=16,
E=32
}
I want to add B+ in the enum but it is giving me error
You can add user friendly description for enum like below :
enum MyEnum
{
[Description("This is black")]
Black,
[Description("This is white")]
White
}
Ref. Link : How to have userfriendly names for enumerations?
How about using a valid identifier like B_Plus?
Yes. It's giving you an error because your code is wrong. You can't make "B+" an enum value because there's a plus sign. Same reason you can't declare int B+. Use a different name.
You won't be able to use + as a name identifier because it's a math operator or string concatenator... it can't be used with enums. Use an alternative syntax, or use an alternative approach. You could consider a state design pattern:
http://www.dofactory.com/patterns/PatternState.aspx#_self2
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.NET Enumeration allows comma in the last field
i noticed while i was refreshing my memory on c# that with enums, you dont get a complaint from the compiler when you leave a comma after the last variable... EG
enum fruit {
apple,
pear,
watermelon,
}
i was wondering you can do this? shouldnt the compiler say "syntax error: ," or something?
It is part of the C# specification, the compiler is simply following it.
Document: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
Page 363, Section 19.7
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C# what is the difference between String and string
I couldn't find the info anywhere, but I'm sure it's just a simple answer. Are they interchangeable??
Yes, they are identical. string is just the C# name for a System.String, which you can also use. See MSDN:
The string type represents a string of Unicode characters. string is an alias for System.String in the .NET Framework.
It's personal preference, but I always use string over String. I guess I like the blue color over the, uh, turquoise color offered by the default syntax highlighting.
You can see the other aliases under the C# value types on MSDN (e.g. int is really System.Int32 while long is really System.Int64).
You get this question a lot because in java there is a difference between for example Integer and int (pointer allocation). In C# there is no difference between String and string, nor there is a difference between Int32 and int etc..