Search code examples
c#value-typereference-type

How to Find the Object is a Value Type OR Reference Types in C#


I'm having a Method, it has one parameter of type object.

In that I have to find the object is Value Type or Reference Type

Public void MyMethod(object param)
{
    if(param is Value Type)
    {
        // Do Some Operation related to Value Type
    } 
    else if(param is Reference Type)
    {
        // Do Some Operation related to Reference Type
    } 
}

List of Value Types

  • string
  • int
  • float
  • bool, etc.,

List of Reference Types

  • List
  • Array
  • Stack
  • Dictionary, etc.,

Solution

  • You can use properties IsValueType and IsClass on Type:

    if(param.GetType().IsValueType)
    {
        // param is value type
    } 
    else if(param.GetType().IsClass)
    {
        // param is reference type
    }