Parses the enum. - CSharp System

CSharp examples for System:Enum

Description

Parses the enum.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Numerics;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//w ww . j ava2 s .c  om

public class Main{
        /// <summary>
        /// Parses the enum.
        /// </summary>
        public static T AsEnum<T>(this string val) where T : struct
        {
            T fallBack = default(T);
            T type;
            if (String.IsNullOrWhiteSpace(val) || !Enum.TryParse(val, true, out type))
            {
                type = fallBack;
            }
            return type;
        }
}

Related Tutorials