(this == null) in C#!

前端 未结 6 1295
一个人的身影
一个人的身影 2020-12-12 11:52

Due to a bug that was fixed in C# 4, the following program prints true. (Try it in LINQPad)

void Main() { new Derived(); }

class Base {
    pu         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-12 12:35

    Not sure if this is what you are looking for

        public static T CheckForNull(object primary, T Default)
        {
            try
            {
                if (primary != null && !(primary is DBNull))
                    return (T)Convert.ChangeType(primary, typeof(T));
                else if (Default.GetType() == typeof(T))
                    return Default;
            }
            catch (Exception e)
            {
                throw new Exception("C:CFN.1 - " + e.Message + "Unexpected object type of " + primary.GetType().ToString() + " instead of " + typeof(T).ToString());
            }
            return default(T);
        }
    

    example: UserID = CheckForNull(Request.QueryString["UserID"], 147);

提交回复
热议问题