Why does Type.IsByRef for type String return false if String is a reference type?

前端 未结 3 510
甜味超标
甜味超标 2021-01-01 23:36

According to this a string (or String) is a reference type.

Yet given:

Type t = typeof(string);

then

if (t.IsBy         


        
3条回答
  •  孤独总比滥情好
    2021-01-02 00:02

    You should use IsValueType instead:

    bool f = !typeof (string).IsValueType; //return true;
    

    As for IsByRef, the purpose of this property is to determine whether the parameter is passed into method by ref or by value.

    Example you have a method which a is passed by ref:

    public static void Foo(ref int a)
    {
    }
    

    You can determine whether a is pass by reference or not:

      bool f = typeof (Program).GetMethod("Foo")
                                     .GetParameters()
                                     .First()
                                     .ParameterType
                                     .IsByRef;   //return true
    

提交回复
热议问题