Conversion between c # enumeration

Enumeration will be used in C #, but have you ever thought, how to convert between an enumeration? This article I will look at it conversion between enumeration.
C # does not support conversion between different enumeration, but CLR support, so we can use CLR assignment compatibility to achieve the conversion between the enumeration. Look at the code you will understand:

enum Country1
{
	CN,
	USA,
	UK,
	JP
}

enum Country2
{
	CN,
	USA,
	UK,
	JP
}

class Orogram
{
	static void Main()
	{
		Country1[] c1 = (Country1[])(Array) new Country2[50];
	}
}

The above code is very simple, the main advantage of the Array to operate. Note that two types enumerated type must not have the same group can use this method to conversion.
In a real project to convert between an enumeration almost useless, and potentially unexpected results.

Published 204 original articles · won praise 101 · Views 350,000 +

Guess you like

Origin blog.csdn.net/gangzhucoll/article/details/104199933