Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Enum, TryParse, cast to enum
//Rextester.Program.Main is the entry point for your code. Don't change it. //Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354) using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public enum AppInfoResult { UPDATED = 0, UPDATE_OPTIONAL = 1, UPDATE_MANDATORY = 2, UNKNOWN = 3 } public static void Main(string[] args) { Console.WriteLine("Hello, world!"); var enumAsEnum = AppInfoResult.UPDATE_MANDATORY; var enumAsInt = (int) enumAsEnum; Console.WriteLine($"Enum {enumAsEnum} as int: {enumAsInt}"); var intForConvertingToEnum = 1; //var intForConvertingToEnum = 4; //No enum returned! Returns 4 !!! AppInfoResult intConvertedToEnum = (AppInfoResult)intForConvertingToEnum; Console.WriteLine($"int {intForConvertingToEnum} as enum: {intConvertedToEnum}"); var appInfoResult1 = "UPDATED"; if (!Enum.TryParse(appInfoResult1, true, out AppInfoResult result1)) result1 = AppInfoResult.UNKNOWN; Console.WriteLine($"Parsed string: {appInfoResult1} --> {result1}"); var appInfoResult2 = "KKKKKKK"; if (!Enum.TryParse(appInfoResult2, true, out AppInfoResult result2)) result2 = AppInfoResult.UNKNOWN; Console.WriteLine($"Parsed string: {appInfoResult2} --> {result2}"); var longAsStr = "123"; var longAsLong = long.Parse(longAsStr); Console.WriteLine($"long.Parse \"{longAsStr}\" --> {longAsLong}"); var objectExample = "string example"; //var stringAsLong = long.Parse(objectExample); //EXCEPTION !!! //var objectExample = "111"; if (long.TryParse(objectExample, out long objectAsLong)) Console.WriteLine($"long.TryParse \"{objectExample}\" --> {objectAsLong}"); else Console.WriteLine($"Cannot parse \"{objectExample}\" as long"); } } }
run
|
edit
|
history
|
help
0
merge 2 sorted array
Leapyear
cup tournament prediction
What is the best way to iterate over a Dictionary in C#?
Simplified login system
First Class
dxsdxsd
Simple Constructor Implementation
c++ MICHELL MARTINEZ
any predicate test