Override Tostring() of enum to accommodate runtime string changes [duplicate] - c#

This question already has answers here:
How can I internationalize strings representing C# enum values?
(2 answers)
How do I have an enum bound combobox with custom string formatting for enum values?
(21 answers)
Enum ToString with user friendly strings
(25 answers)
Closed 10 years ago.
I have an enum like this
enum Animal:byte
{
Cat=0,
Dog=1,
Horse=2
}
I want to override the ToString() of it to to write a custom string because my application is a multi-language one(Cat.ToString() should retrieve the translated word of cat). so the Description keyowrd can't be used here.can anyone give me a hint to find a neat way to solve this?

Related

C# Add conditional directly inside string [duplicate]

This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.

C# double/decimal always have two decimals [duplicate]

This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 7 years ago.
I need 45.7 to be 45.70
Math.Round(d, 2) have no effect.
Have tried with decimal and double type.
I would appreciate any kind of help.
You need to apply a string format:
string.Format("{0:N2}", 45.7)
Check here for docs on the string formatters

How can specify new value for default system enum [duplicate]

This question already has answers here:
Can I add an enum to an existing .NET Structure, like Date?
(3 answers)
Closed 8 years ago.
I have one question
How can I specify new value into system enum?
For example, the LoginFailureAction has a enum that define with microsoft in c# how can I get new value into this enum?
Of course, you can't change the default value of a type. The type is the type, and you can't change it.

Display Text for Enum [duplicate]

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

C# Double.ToString() [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Custom numeric format string to always display the sign
To format a double to certain precision in C# I write this:
d.ToString("#0.00");
What if I want to force a the sign to appear..
e.g
+2.54 and -2.54
Hope this will work.
YourDoublenumber.ToString("+#;#");

Categories

Resources