From an int:
YourEnum foo = (YourEnum)yourInt;
From a string:
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString); // The foo.ToString().Contains(",") check is necessary for enumerations marked with an [Flags] attribute if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(",")) { throw new InvalidOperationException($"{yourString} is not an underlying value of the YourEnum enumeration.") }
Update:
From number you can also
YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum) , yourInt);
Related Posts:
- Get int value from enum in C#
- How can I convert String to Int?
- Convert int to string?
- Convert char to int in C#
- String was not recognized as a valid DateTime ” format dd/MM/yyyy”
- Convert double to float by cast or Convert.ToSingle()?
- How to enumerate an enum
- ImageMagick creating blank transparent square(s) according to width
- Why has the int32 type a maximum value of 2³¹ − 1? [duplicate]
- What is a PDB file?
- C# error: “An object reference is required for the non-static field, method, or property”
- Error : Could not load file or assembly or one of it’s dependencies
- Join/Where with LINQ and Lambda
- C# compiler error: “not all code paths return a value”
- Algorithms in O(n^2) vs O(n) [duplicate]
- The remote certificate is invalid according to the validation procedure [duplicate]
- WCF vs ASP.NET Web API
- The located assembly’s manifest definition does not match the assembly reference
- Compiler Error Message: The compiler failed with error code -2146232576
- Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details
- A generic error occurred in GDI+, JPEG Image to MemoryStream
- “Could not find a part of the path” error message
- WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery
- What does the %= operator do? [duplicate]
- What does vary:accept-encoding mean?
- what is this oleaut32.dll?
- Why not use Html.EditorForModel()
- What is and how to fix System.TypeInitializationException error?
- Where can I find System.Web.Helpers, System.Web.WebPages, and System.Web.Razor?
- How to Use TextureBrush for painting an Image
- Can’t use System.Windows.Forms
- How to use the “Using” statement in ASP.net razor webpages?
- Where can I find the assembly System.Web.Extensions dll?
- Regex for numbers only
- There is no argument given that corresponds to the required formal parameter – .NET Error
- How do I remedy “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?
- Is there an exponent operator in C#?
- How can I export data to an Excel file
- How to use html.ValidationMessageFor
- Could not establish trust relationship for SSL/TLS secure channel — SOAP
- EF 5 Enable-Migrations : No context type was found in the assembly
- How to convert an Stream into a byte[] in C#?
- Visual studio 2019 freezes when debugging
- Merge two (or more) lists into one, in C# .NET
- Why should I use IHttpActionResult instead of HttpResponseMessage?
- How do you sort a dictionary by value?
- EntityType has no key defined error
- Convert integer to binary in C#
- HTML.ActionLink method
- Thread-safe List
property - Styles.Render in MVC4
- Exception from HRESULT: 0x800A03EC Error
- num1 >= 0x80 – What is 0x80?
- regular expression “.*[^a-zA-Z0-9_].*”
- Sharing violation IOException while reading and writing to file C#
- How to delete object?
- There is already an open DataReader associated with this Command which must be closed first
- Cannot implicitly convert type ‘int’ to ‘string’ C# class
- If a folder does not exist, create it
- C# “No suitable method found to override.” — but there is one
- Fastest way to remove first char in a String
- System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
- What is the purpose of global.asax in asp.net
- How to convert datetime format to date format in crystal report using C#?
- How to add a string to a string[] array? There’s no .Add function
- How to block until an event is fired in c#
- How to use WPF Background Worker
- Case insensitive ‘Contains(string)’
- How to catch all exceptions in c# using try and catch?
- Getting “Handshake failed…unexpected packet format” when using WebClient.UploadFile() with “https” when the server has a valid SSL certificate
- Most efficient way to remove special characters from string
- Receiving access denied error from Visual Studio when trying to change target framework
- How to force garbage collector to run?
- Page vs Window in WPF?
- CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
- Sort list by field (C#)
- How to post data to specific URL using WebClient in C#
- Value does not fall within the expected range
- The type or namespace name could not be found
- How to execute a stored procedure within C# program
- The remote server returned an error: (405) Method Not Allowed. WCF REST Service
- TTS Voices for Windows7 64 bits
- How to play a sound in C#, .NET
- How can I delete a file that is in use by another process?
- Convert SQL to LINQ Query
- How to resolve file being used by another process?
- How to crop an image using C#?
- Call a stored procedure with parameter in c#
- Type Checking: typeof, GetType, or is?
- C# Macro definitions in Preprocessor
- Adding images to buttons from Imagelist on a toolstrip?
- Multiple cases in switch statement
- System.ArgumentNullException: Value cannot be null. Parameter name: source
- Sequence contains more than one element
- Drawing circles with System.Drawing
- Parser Error Message: Could not load type ‘sometype’
- Func vs. Action vs. Predicate [duplicate]
- Cannot declare instance members in a static class in C#
- the source file is different from when the module was built
- How to set session timeout in web.config