-3

what is the best way to check if a certain Type is nullable or not.

some suggested codes out there are unable to test other Type especially string.

VJPPaz
  • 917
  • 7
  • 21
  • Yes that's fine, but its a duplicate. Already asked and answered. Make sure to check for duplicates before you ask a new question. – JK. Aug 18 '15 at 03:43
  • In fact this was was created AFTER he already posted the answer in that – Dijkgraaf Aug 18 '15 at 03:44
  • Well, that's what happens when you try to game the system for a few measly rep points. Its quickly spotted and you lose far more than you gain. – JK. Aug 18 '15 at 03:48
  • sorry, i didn't know i would lose some point from it. noted, hehe – VJPPaz Aug 18 '15 at 04:51
  • `string` is a reference type, so it's nullable by definition – w.b Aug 18 '15 at 04:59
  • yeah but If you are creating a generic you will never know if the generic type is a string or not. Example, if you are trying to create an Extension for IEnumerable to generate a DataTable from it with the help of Reflection – VJPPaz Aug 18 '15 at 05:02

2 Answers2

1
!certainType.IsValueType || Nullable.GetUnderlyingType(certainType) != null
Reza ArabQaeni
  • 4,848
  • 27
  • 46
-3

the simplest way to do it:

    public static bool IsNullable(this Type type)
    {
        if (type.IsValueType) return Activator.CreateInstance(type) == null;
        return true;
    }

these are my unit tests and all passed

    IsNullable_String_ShouldReturn_True
    IsNullable_Boolean_ShouldReturn_False
    IsNullable_Enum_ShouldReturn_Fasle
    IsNullable_Nullable_ShouldReturn_True
    IsNullable_Class_ShouldReturn_True
    IsNullable_Decimal_ShouldReturn_False
    IsNullable_Byte_ShouldReturn_False
    IsNullable_KeyValuePair_ShouldReturn_False

actual unit tests

    [TestMethod]
    public void IsNullable_String_ShouldReturn_True()
    {
        var typ = typeof(string);
        var result = typ.IsNullable();
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void IsNullable_Boolean_ShouldReturn_False()
    {
        var typ = typeof(bool);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_Enum_ShouldReturn_Fasle()
    {
        var typ = typeof(System.GenericUriParserOptions);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_Nullable_ShouldReturn_True()
    {
        var typ = typeof(Nullable<bool>);
        var result = typ.IsNullable();
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void IsNullable_Class_ShouldReturn_True()
    {
        var typ = typeof(TestPerson);
        var result = typ.IsNullable();
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void IsNullable_Decimal_ShouldReturn_False()
    {
        var typ = typeof(decimal);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_Byte_ShouldReturn_False()
    {
        var typ = typeof(byte);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_KeyValuePair_ShouldReturn_False()
    {
        var typ = typeof(KeyValuePair<string, string>);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }
VJPPaz
  • 917
  • 7
  • 21
  • you answered your own question? – Aniket Inge Aug 18 '15 at 03:40
  • Answering your own question is allowed normally. But in this case he's taken his answer to another question http://stackoverflow.com/a/32063068/325727 and created a new duplicate question just to post the exact same answer again. So downvotes are fully deserved here. – JK. Aug 18 '15 at 03:50
  • well that is also my answers -VJPPaz, i didn't know is a bad idea to repost the answer. i should learn from it XD – VJPPaz Aug 18 '15 at 04:53