Get Enum values as string array [duplicate] - c#

This question already has answers here:
Convert System.Array to string[]
(5 answers)
Closed 8 years ago.
I want to get all Enum.values as string[].
I tryed using
Array mPriorityVals = Enum.GetValues(typeof(MPriority));
But how do I cast it as string[]?

You just need Enum.GetNames method, Enum.GetValues gives the result as EnumType rather than string.
string[] names = Enum.GetNames(typeof (MPriority));
I suggest you to just use GetNames, don't call GetValues and cast it to string as suggested in comment.

Related

Get `Type` for Array of Type at Runtime [duplicate]

This question already has an answer here:
Creating Array Type at runtime
(1 answer)
Closed 9 months ago.
I have a variable prop of type PropertyInfo.
I want to create a Type variable which is an array of the prop.PropertyType.
For normal generic types I can do typeof(myType<>).MakeGenericType(prop.PropertyType) but arrays aren't generics.
The best I've been able to come up with so far is:
Array.CreateInstance(prop.PropertyType, 0).GetType()
But this is really inelligant, am I missing something?
You can use Type.MakeArrayType() to do this:
var arrayType = prop.PropertyType.MakeArrayType();

How to avoid Newtonsoft JObject.Parse type conversion [duplicate]

This question already has answers here:
Json.NET Disable the deserialization on DateTime
(2 answers)
JToken: Get raw/original JSON value
(3 answers)
JObject.Parse modifies end of floating point values
(3 answers)
Closed 2 years ago.
When I parse a String that contains a valid RFC3339 datetimestring, JOject.Parse() automatically makes an implizit type conversion to DateTime:
String toParse = "{\"datetime\": \"2020-08-04T23:45:00+02:00\"}";
JObject p = JObject.Parse(toParse);
String d = (String)p["datetime"];
Console.Write(d);
// Gives 08/04/2020 23:45:00
// But I want 2020-08-04T23:45:00+02:00
I think the "unwanted" typecast takes place while reading the p["datetime"] and not during JOject.Parse(), but how can I totally avoid that behavior and let it only deserialize everything as string?
I have this problem with different types, also decimals which are automatically casted, by I need them as String.
Thank you!

Runtime error on casting Array to string Enumerator [duplicate]

This question already has answers here:
obtain generic enumerator from an array
(7 answers)
Closed 8 years ago.
This is a follow up question to Design issues and implementing Enumerable.AsEnumerable<FarPoint.Win.Spread.Row>
I have casted my farpoint row to a 2 d object array like this:
object[,] nthRow = fpSpread2.ActiveSheet.GetArray(e.Row, e.Column, 1, FarPointSpread1.ActiveSheet.ColumnCount);
I try to cast this to string so that i can apply Linq but i get the following error at runtime:
IEnumerator<string> narry = (IEnumerator<string>)nthRow.GetEnumerator();
Unable to cast object of type 'ArrayEnumerator' to type 'System.Collections.Generic.IEnumerator`1[System.String]'.
How can i resolve this problem?
Even for multidimensional arrays you can call Cast<T> on it.
using System.Linq;
...
nthRow.Cast<string>().GetEnumerator() // returns IEnumerator<string>
But each element in the array should actually be type string. If you want to do formatting like ToString(), you can first cast them into object and then call Select<T>.

string Replace not working in asp.net [duplicate]

This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
string mystring="the are boys";
string[] tags = {"the"};
string[] replace ={"they"}
mystring.Replace(tags[0],replace[0]) // is not working
mystring.Replace("the","they") // is working
I thought both are same but first statement is not working. The second one is.
Please help me to solve the problem.
I assume that you don't assign the return value of String.Replace to the variable. But since strings are immutable you have to do that:
mystring = mystring.Replace(tags[0],replace[0])

Converting Array Type in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert string[] to int[] in one string of code using LINQ
I have an array of strings I want to convert it to array of int
is there any way to convert it directly without looping
I mean without use foreach, for , LINQ select statement, etc.
Any suggestion please.
Array.ConvertAll: http://msdn.microsoft.com/en-us/library/exc45z53.aspx

Categories