. NET determines whether an object is a numeric type

Source: Internet
Author: User

It was a very simple thing to hear at first glance, but it was a bit of a surprise to get up.

First of all, of course, the GetType () method reflects its type information and then parses it, but the type information is not simply given a property to judge.

The foreigner gives the method is:

 Public Static BOOLIsNumeric ( ThisType DataType) {        if(DataType = =NULL)            Throw NewArgumentNullException ("DataType"); return(DataType = =typeof(int)                || DataType = =typeof(Double)                || DataType = =typeof(Long)                || DataType = =typeof( Short)                || DataType = =typeof(float)                || DataType = =typeof(Int16)|| DataType = =typeof(Int32)|| DataType = =typeof(Int64)|| DataType = =typeof(UINT)                || DataType = =typeof(UInt16)|| DataType = =typeof(UInt32)|| DataType = =typeof(UInt64)|| DataType = =typeof(sbyte)                || DataType = =typeof(single)); }

I'm going to take a walk. He's trying to be poor. Compare to all known numeric types .... This should be possible, that is, performance is almost and indecent.

And he seems to have forgotten the decimal ...

I looked at these numeric types, which seem to be structs rather than classes, and all have common interfaces:

IFormattable, IComparable, iconvertible

Where the IFormattable interface is an interface where the numeric type differs from several other underlying types.

This is very good, the code is as follows:

 Public Static BOOL Isnumerictype ( this Type o)    {        returntypeof(IFormattable));    }

In addition to the basic type there are also nullable type Nullable<t>, which is commonly used for example double? This, for generic type matching I do not know what to do, in a hurry did not delve into, with a lazy way to achieve:

 Public Static BOOL Isnullablenumerictype ( this Type o)    {        if (!o.name.startswith ("  Nullable"returnfalse;         return o.getgenericarguments () [0]. Isnumerictype ();    }

See, just to determine the type name is not to "Nullable" start, if it is the first generic parameter type of the above judgment, so certainly not 100% reliable, I hope that insight can improve this method and share it out ha.

. NET determines whether an object is a numeric type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.