This question already has answers here:
Is it possible to add a string with spaces or special characters to an enum?
(2 answers)
Closed 8 years ago.
I have an enumeration as below
private enum gettype
{
Xml/adf = 1,
xml/adf and html = 2
};
but as of my knowledge I know that we could not declare special characters spaces in between.
I even tried "display" and "description" though didn't work.
So does any one know any work around.
You cannot. Enum members must be valid C# identifiers. You can decorate them with a variety of attributes, however:
using System.ComponentModel;
private enum gettype
{
[Description("Xml/adf")]
XmlAdf = 1,
[Description("Xml/adf and html")]
XmlAdfAndHtml = 2
}
Now to convert the enum value to a description string, or vice versa you'd have to use reflection, one way or other. For example:
var enumValue = gettype.XmlAdfAndHtml;
var attr = (DescriptionAttribute[])
typeof(gettype).GetField(enumValue.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), true);
var stringValue = attr[0].Description; // Xml/adf and html
Refers the below stack overflow links which may helpful to you.
Is it possible to add a string with spaces or specialcharacters to an enum
How can I use a special char in a C# enum?
Related
This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
How I can check if my string has the value ".all" in. Example:
string myString = "Hello.all";
I need to check if myString has .all in order to call other method for this string, any ideas how I can do it?
you could use myString.Contains(".all")
More info here
Use IndexOf()
var s = "Hello.all";
var a = s.IndexOf(".all", StringComparison.OrdinalIgnoreCase);
If a = -1 then no occurrences found
If a = some number other than -1 you get the index (place in the string where it starts).
So a = 5 in this case
Simply call .Contains(".all") on the string object:
if (myString.Contains(".all")
{
// your code to call the other method goes here
}
There is no need for regex to do that.
Optionally, as mentioned by #ZarX in comments, you can check if the string ends with your keyword with .EndsWith(".all"), which will return true if the string ends with your keyword.
This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 19 days ago.
I understand that the # symbol can be used before a string literal to change how the compiler parses the string. But what does it mean when a variable name is prefixed with the # symbol?
The # symbol allows you to use reserved word. For example:
int #class = 15;
The above works, when the below wouldn't:
int class = 15;
The # symbol serves 2 purposes in C#:
Firstly, it allows you to use a reserved keyword as a variable like this:
int #int = 15;
The second option lets you specify a string without having to escape any characters. For instance the '\' character is an escape character so typically you would need to do this:
var myString = "c:\\myfolder\\myfile.txt"
alternatively you can do this:
var myString = #"c:\myFolder\myfile.txt"
An important point that the other answers forgot, is that "#keyword" is compiled into "keyword" in the CIL.
So if you have a framework that was made in, say, F#, which requires you to define a class with a property named "class", you can actually do it.
It is not that useful in practice, but not having it would prevent C# from some forms of language interop.
I usually see it used not for interop, but to avoid the keyword restrictions (usually on local variable names, where this is the only effect) ie.
private void Foo(){
int #this = 2;
}
but I would strongly discourage that! Just find another name, even if the 'best' name for the variable is one of the reserved names.
It allows you to use a C# keyword as a variable. For example:
class MyClass
{
public string name { get; set; }
public string #class { get; set; }
}
This question already has answers here:
Enum ToString with user friendly strings
(25 answers)
Closed 7 years ago.
I need my enum to return a formatted string to the view, for example:
public enum status
{
NotStarted,
InProgress,
}
return: Not Started and In Progress. How I do it? Thanks (language C#)
enums don't do that. You'd need to provide a map of enum values to display strings or you could do something like define an attribute with a display string that you can use (which requires some fiddly reflection to get the attribute for a given enum value, but has the advantage that you can define the display name right where you define the enum value).
For example, you can use a Dictionary<status,string> to map them:
var myMap = new Dictionary<status,string>()
{
{ status.NotStarted, "Not Started" },
{ status.InProgress, "In Progress" }
};
Now to get the display string for a given status value s you'd just do something like:
var s = status.NotStarted;
var displayString = myMap[s]; // "Not Started"
Of course, you'd put this in a class somewhere so it's only defined once in one place.
Another rather brittle, quick-and-dirty way to do it would be to exploit the fact that your enum names are Pascal-cased and use something like a regex to take the enum name and insert an extra space. But that's pretty hacky. So you could do something like:
var r = new Regex("([A-Z][a-z]*)([A-Z][a-z]*)");
var displayString = r.Replace(s.ToString(),"$1 $2"); // "Not Started"
But that would choke on any enum values that didn't fit the pattern of two Pascal-cased words. Of course, you could make your regex more flexible, but that's beyond the scope of the question.
Calling ToString on an emum value is the equivalent to Enum.GetName which would give you the named value i.e.
Console.WriteLine(status.NotStarted.ToString()) // NotStarted
From there, assuming the format is consistent, you could convert the string from Pascal casing to a whitespace separated string e.g.
string result = Regex.Replace(status.NotStarted, "([a-z])([A-Z])", "$1 $2");
Console.WriteLine(result); // Not Started
See example.
Enum.GetName(typeof (Status), Status.InProgress));
This question already has answers here:
C# numeric enum value as string
(8 answers)
Closed 8 years ago.
Having an enum like this:
enum Items { one, two, three, four};
How can I assign result to a string variable like
string itemType = Items.2;
or
string itemType = Items.one;
Can you please let me know if it is possible or not? Thanks
You can use Enum.GetName method.
var name = Enum.GetName(typeof (Items), Items.one);
Or:
var name = Enum.GetName(typeof (Items), 0);
Add `.ToString() to your code:
string itemType = Items.one.ToString();
Call ToString() on the enum. You should be able to do Items.one.ToString() and get the string representation of the enum.
This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 22 days ago.
I understand that the # symbol can be used before a string literal to change how the compiler parses the string. But what does it mean when a variable name is prefixed with the # symbol?
The # symbol allows you to use reserved word. For example:
int #class = 15;
The above works, when the below wouldn't:
int class = 15;
The # symbol serves 2 purposes in C#:
Firstly, it allows you to use a reserved keyword as a variable like this:
int #int = 15;
The second option lets you specify a string without having to escape any characters. For instance the '\' character is an escape character so typically you would need to do this:
var myString = "c:\\myfolder\\myfile.txt"
alternatively you can do this:
var myString = #"c:\myFolder\myfile.txt"
An important point that the other answers forgot, is that "#keyword" is compiled into "keyword" in the CIL.
So if you have a framework that was made in, say, F#, which requires you to define a class with a property named "class", you can actually do it.
It is not that useful in practice, but not having it would prevent C# from some forms of language interop.
I usually see it used not for interop, but to avoid the keyword restrictions (usually on local variable names, where this is the only effect) ie.
private void Foo(){
int #this = 2;
}
but I would strongly discourage that! Just find another name, even if the 'best' name for the variable is one of the reserved names.
It allows you to use a C# keyword as a variable. For example:
class MyClass
{
public string name { get; set; }
public string #class { get; set; }
}