0

I have an Enum abc(N=0,Y=1,M=2), i want to convert it to CSV so I used string.Join as below

var abc_arr = Enum.GetValues(typeof(abc));
var abc_csv = string.Join("','", abc_arr );

But it does not return N,Y,M but instead returns "System.abc[]" Why?

Well the problem is Enum.GetValues is strongly typed so you have to explicitly set the resulting type

1

3 Answers 3

3

If you want the names, you should use: Enum.GetNames(typeof(abc)). GetValues returns the values of the enum. If you want that, you can do it via a cast: (int[])Enum.GetValues(typeof(abc))

Sign up to request clarification or add additional context in comments.

Comments

1

You should cast first the result of GetValues:

var abc_arr = (abc[])Enum.GetValues(typeof(abc));
var abc_csv = string.Join(",", abc_arr );

Furthermore, I changed a bit your separator.

You could take a look here for a demo.

Comments

1
var abc_arr = Enum.GetNames(typeof(abc));
var csv = string.Join(",", abc_arr);

You should call GetNames to return string[] of the names, also your separator should not have ','. It should be only ,

2 Comments

I have to use it in an sql query so I used ',' not ,
@irfandar okay add it if you need it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.