Wednesday, August 8, 2012

Convert enum to string and vice versa



            Vehicle myPreference = Vehicle.Bicycle;
            string vehicle = myPreference.ToString(); // returns string with value: "Bicycle"

            Vehicle secondChoice = (Vehicle)Enum.Parse(typeof(Vehicle), "Spaceship", true); // returns enum: Vehicle.Spaceship

No comments:

Post a Comment