locked
what is default type in .net c# is it value type or reference type ? RRS feed

  • Question

  • User264732274 posted

    please tell me what is default type in .net c# is it value type or reference type ? just for the proof include some code sample too. thanks

    Monday, February 15, 2016 9:26 AM

Answers

  • User541108374 posted

    Hi,

    the best explanation is to simply read the documentation about the default keyword: https://msdn.microsoft.com/en-us/library/xwth0h0d.aspx.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 9:37 AM
  • User541108374 posted

    Hi,

    the url u gave that is not right because i like to know ref type or value type is default in .net ?

    I think we mixed up things. I thought you asked about the default keyword while it seems now you're asking what's the default type in .NET? If the latter's the case then I can say that both reference types and value types are first class citizens in .NET. So from that point of view and to answer your question both are default in .NET.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 2:48 PM
  • User-434868552 posted

    @sudip_inn

    var sudip_inn = 123;
    var XIII      = "moderator";
    
    Console.WriteLine(sudip_inn.GetType());  // outputs "System.Int32"
    Console.WriteLine(XIII.GetType());       // outputs "System.String"

    what is default type

    there is no default type ... type is defined by the c# language specification.

    is it value type or reference type

    study https://msdn.microsoft.com/en-us/library/s1ax56ch.aspx "Value Types (C# Reference)"

      and https://msdn.microsoft.com/en-us/library/490f96s2.aspx "Reference Types (C# Reference)"


    MORE  EXPLANATION

    sudip_inn, if you look at the code above, you may be tempted to assume that the first variable defaulted to System.Int32 and the second variable defaulted to System.String.

    The is not the case; the c# compiler looked at the right side of the equal sign and from there determined that the first variable is Int32 and the second variable is String.

    From there, Int32 has been pre-defined as a value-type and String is a class and therefore a reference-type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 7:18 PM
  • User753101303 posted

    Hi,

    The context is still a bit unclear. For methods the default is to pass them as value types and you have to mark it explicitely as ref. See https://msdn.microsoft.com/en-us/library/8f1hz171.aspx

    If you meant if a given variable is a reference or a value type it depends on its type. https://msdn.microsoft.com/en-us/library/bfft1t3c.aspx should give the list of value types.

    You could also use https://msdn.microsoft.com/en-us/library/system.type.isvaluetype(v=vs.110).aspx

    String is a special case. If I remember a previous discussion, someone found it is listed differently depending on the doc you are looking at. My suggestion was that you should include the "point of view" you are using that is basically this is a C# value type even though it is technically a CLR reference type (ie it depends if you consider the language behavior or the actual underlying runtime type) so it's possible you'll find some inconsistencies depending if the context is rather about the C# language or about the CLR.

    Edit: or from " default type in .net c# is it value type or reference type?" it seems there is a small confusion. "value" or "reference" or not types. A given type is either in the "value type" category or in the "reference type" category. Or if you meant rather when passing parameters the default is to pass them "by value" (rather than "by reference"). This is the same underlying idea but not really the same thing (ie you can pass a "reference type" "by value" and a "value type" "by reference" ie the type doesn't matter here). I would't say that an argument is a "value type" by default. I believe more correct to say it is passed "by value" (even if the argument type is in the "reference type" category).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2016 2:11 PM
  • User-434868552 posted

    @PatriceSc

    PatriceSc

    String is a special case.

    No, it's not.

    PatriceSc

    String .... basically this is a C# value type

    No, it's not.

    http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type

    https://blogs.msdn.microsoft.com/ericlippert/2009/04/27/the-stack-is-an-implementation-detail-part-one/

    https://blogs.msdn.microsoft.com/ericlippert/2009/05/04/the-stack-is-an-implementation-detail-part-two/

    PatriceSc

    For methods the default is to pass them as value types and you have to mark it explicitly as ref. See https://msdn.microsoft.com/en-us/library/8f1hz171.aspx

    PatriceSc, you have misinterpreted the above article "Method Parameters (C# Reference)"

    "if a parameter is declared for a method without ref or out, the parameter can have a value associated with it."

    compare:

    public void stringPassed(String myString)
    { String theString = myString; }
    
    public void int32Passed(Int32 myNumber)
    { Int32 theNumber = myNumber; }

    it can be confusing since the MSIL looks identical:

    stringPassed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  stloc.0     // theString
    IL_0003:  ret         
    
    int32Passed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  stloc.0     // theNumber
    IL_0003:  ret     

    HOWEVER, here the difference is more clearly shown:

    public void stringPassed(String myString)
    { Console.WriteLine(myString.GetType()); }
    
    public void int32Passed(Int32 myNumber)
    { Console.WriteLine(myNumber.GetType()); }

    MSIL:

    stringPassed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  callvirt    System.Object.GetType
    IL_0007:  call        System.Console.WriteLine
    IL_000C:  nop         
    IL_000D:  ret         
    
    int32Passed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  box         System.Int32
    IL_0007:  call        System.Object.GetType
    IL_000C:  call        System.Console.WriteLine
    IL_0011:  nop         
    IL_0012:  ret    

    https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx "Boxing and Unboxing (C# Programming Guide)"

    "Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type."

    The String parameter stringPassed is clearly a reference type, while the Int32 parameter int32passed is clearly a value type.

    https://msdn.microsoft.com/en-us/library/14akc2c7.aspx "ref (C# Reference)"

    PatriceSc, as you know, the ref keyword has nothing to do with reference types or value types; the ref keyword has a permission effect on what  can be done to reference types and value types that are passed into to a method; when reference types and value types that are passed into a method are marked with the ref keyword, "any change to the parameter in the called method is reflected in the calling method".

    public void stringPassed(ref String myString)
    { myString = "B-)"; }
    
    public void int32Passed(ref Int32 myNumber)
    { myNumber = 42; }

    MSIL:

    stringPassed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  ldstr       "B-)"
    IL_0007:  stind.ref   
    IL_0008:  ret         
    
    int32Passed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  ldc.i4.s    2A 
    IL_0004:  stind.i4    
    IL_0005:  ret    

    "There is no boxing of a value type when it is passed by reference."

    https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stind_i4(v=vs.110).aspx 

    Stores a value of type int32 at a supplied address.
     

    PatriceSc, FWIW, the O.P. asked one question and then asked a different question at http://forums.asp.net/post/6021069.aspx above.

    @sudip_inn

    (a) there is no default type in c#    see http://forums.asp.net/post/6021086.aspx above

    (b) there is no default way of passing parameters because parameters are for all intents and purposes passed the same way; however, when modified by the ref keyword, the calling method gives permission to the called method to modify the actual stored values marked with the ref keyword.

    public void int32PassedByValue(Int32 myNumber)
    { myNumber = 42; // change locally}
    
    public void int32PassedByReference(ref Int32 myNumber)
    { myNumber = 42; // change in calling method}

    MSIL:

    int32PassedByValue:
    IL_0000:  nop         
    IL_0001:  ldc.i4.s    2A 
    IL_0003:  starg.s     01 
    IL_0005:  ret         
    
    int32PassedByReference:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  ldc.i4.s    2A 
    IL_0004:  stind.i4    
    IL_0005:  ret

    FWIW, perhaps it would have been better had the keyword been "mutable" instead of "ref"; maybe there would be less confusion.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2016 2:11 AM

All replies

  • User541108374 posted

    Hi,

    the best explanation is to simply read the documentation about the default keyword: https://msdn.microsoft.com/en-us/library/xwth0h0d.aspx.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 9:37 AM
  • User264732274 posted

    the url u gave that is not right because i like to know ref type or value type is default in .net ?

    please provide relevant url. thanks

    Monday, February 15, 2016 11:19 AM
  • User541108374 posted

    Hi,

    the url u gave that is not right because i like to know ref type or value type is default in .net ?

    I think we mixed up things. I thought you asked about the default keyword while it seems now you're asking what's the default type in .NET? If the latter's the case then I can say that both reference types and value types are first class citizens in .NET. So from that point of view and to answer your question both are default in .NET.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 2:48 PM
  • User264732274 posted

    i am not sure guess default is value type suppose if i have a function in class hello and it accept two argument. one is int and other is string. so when we would call hello() and pass two variable value and from the function when we assign new value to that two variable and when function return to its calling environment then we probably see two variable value has not been changed..........it means default is value type.

    but we need to change variable value from function permanently then we can use ref type then value would be change permanently.

    what u say..........my understanding is ok ?

    thanks

    Monday, February 15, 2016 5:54 PM
  • User-434868552 posted

    @sudip_inn

    var sudip_inn = 123;
    var XIII      = "moderator";
    
    Console.WriteLine(sudip_inn.GetType());  // outputs "System.Int32"
    Console.WriteLine(XIII.GetType());       // outputs "System.String"

    what is default type

    there is no default type ... type is defined by the c# language specification.

    is it value type or reference type

    study https://msdn.microsoft.com/en-us/library/s1ax56ch.aspx "Value Types (C# Reference)"

      and https://msdn.microsoft.com/en-us/library/490f96s2.aspx "Reference Types (C# Reference)"


    MORE  EXPLANATION

    sudip_inn, if you look at the code above, you may be tempted to assume that the first variable defaulted to System.Int32 and the second variable defaulted to System.String.

    The is not the case; the c# compiler looked at the right side of the equal sign and from there determined that the first variable is Int32 and the second variable is String.

    From there, Int32 has been pre-defined as a value-type and String is a class and therefore a reference-type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 7:18 PM
  • User753101303 posted

    Hi,

    The context is still a bit unclear. For methods the default is to pass them as value types and you have to mark it explicitely as ref. See https://msdn.microsoft.com/en-us/library/8f1hz171.aspx

    If you meant if a given variable is a reference or a value type it depends on its type. https://msdn.microsoft.com/en-us/library/bfft1t3c.aspx should give the list of value types.

    You could also use https://msdn.microsoft.com/en-us/library/system.type.isvaluetype(v=vs.110).aspx

    String is a special case. If I remember a previous discussion, someone found it is listed differently depending on the doc you are looking at. My suggestion was that you should include the "point of view" you are using that is basically this is a C# value type even though it is technically a CLR reference type (ie it depends if you consider the language behavior or the actual underlying runtime type) so it's possible you'll find some inconsistencies depending if the context is rather about the C# language or about the CLR.

    Edit: or from " default type in .net c# is it value type or reference type?" it seems there is a small confusion. "value" or "reference" or not types. A given type is either in the "value type" category or in the "reference type" category. Or if you meant rather when passing parameters the default is to pass them "by value" (rather than "by reference"). This is the same underlying idea but not really the same thing (ie you can pass a "reference type" "by value" and a "value type" "by reference" ie the type doesn't matter here). I would't say that an argument is a "value type" by default. I believe more correct to say it is passed "by value" (even if the argument type is in the "reference type" category).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2016 2:11 PM
  • User-434868552 posted

    @PatriceSc

    PatriceSc

    String is a special case.

    No, it's not.

    PatriceSc

    String .... basically this is a C# value type

    No, it's not.

    http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type

    https://blogs.msdn.microsoft.com/ericlippert/2009/04/27/the-stack-is-an-implementation-detail-part-one/

    https://blogs.msdn.microsoft.com/ericlippert/2009/05/04/the-stack-is-an-implementation-detail-part-two/

    PatriceSc

    For methods the default is to pass them as value types and you have to mark it explicitly as ref. See https://msdn.microsoft.com/en-us/library/8f1hz171.aspx

    PatriceSc, you have misinterpreted the above article "Method Parameters (C# Reference)"

    "if a parameter is declared for a method without ref or out, the parameter can have a value associated with it."

    compare:

    public void stringPassed(String myString)
    { String theString = myString; }
    
    public void int32Passed(Int32 myNumber)
    { Int32 theNumber = myNumber; }

    it can be confusing since the MSIL looks identical:

    stringPassed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  stloc.0     // theString
    IL_0003:  ret         
    
    int32Passed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  stloc.0     // theNumber
    IL_0003:  ret     

    HOWEVER, here the difference is more clearly shown:

    public void stringPassed(String myString)
    { Console.WriteLine(myString.GetType()); }
    
    public void int32Passed(Int32 myNumber)
    { Console.WriteLine(myNumber.GetType()); }

    MSIL:

    stringPassed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  callvirt    System.Object.GetType
    IL_0007:  call        System.Console.WriteLine
    IL_000C:  nop         
    IL_000D:  ret         
    
    int32Passed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  box         System.Int32
    IL_0007:  call        System.Object.GetType
    IL_000C:  call        System.Console.WriteLine
    IL_0011:  nop         
    IL_0012:  ret    

    https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx "Boxing and Unboxing (C# Programming Guide)"

    "Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type."

    The String parameter stringPassed is clearly a reference type, while the Int32 parameter int32passed is clearly a value type.

    https://msdn.microsoft.com/en-us/library/14akc2c7.aspx "ref (C# Reference)"

    PatriceSc, as you know, the ref keyword has nothing to do with reference types or value types; the ref keyword has a permission effect on what  can be done to reference types and value types that are passed into to a method; when reference types and value types that are passed into a method are marked with the ref keyword, "any change to the parameter in the called method is reflected in the calling method".

    public void stringPassed(ref String myString)
    { myString = "B-)"; }
    
    public void int32Passed(ref Int32 myNumber)
    { myNumber = 42; }

    MSIL:

    stringPassed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  ldstr       "B-)"
    IL_0007:  stind.ref   
    IL_0008:  ret         
    
    int32Passed:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  ldc.i4.s    2A 
    IL_0004:  stind.i4    
    IL_0005:  ret    

    "There is no boxing of a value type when it is passed by reference."

    https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stind_i4(v=vs.110).aspx 

    Stores a value of type int32 at a supplied address.
     

    PatriceSc, FWIW, the O.P. asked one question and then asked a different question at http://forums.asp.net/post/6021069.aspx above.

    @sudip_inn

    (a) there is no default type in c#    see http://forums.asp.net/post/6021086.aspx above

    (b) there is no default way of passing parameters because parameters are for all intents and purposes passed the same way; however, when modified by the ref keyword, the calling method gives permission to the called method to modify the actual stored values marked with the ref keyword.

    public void int32PassedByValue(Int32 myNumber)
    { myNumber = 42; // change locally}
    
    public void int32PassedByReference(ref Int32 myNumber)
    { myNumber = 42; // change in calling method}

    MSIL:

    int32PassedByValue:
    IL_0000:  nop         
    IL_0001:  ldc.i4.s    2A 
    IL_0003:  starg.s     01 
    IL_0005:  ret         
    
    int32PassedByReference:
    IL_0000:  nop         
    IL_0001:  ldarg.1     
    IL_0002:  ldc.i4.s    2A 
    IL_0004:  stind.i4    
    IL_0005:  ret

    FWIW, perhaps it would have been better had the keyword been "mutable" instead of "ref"; maybe there would be less confusion.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2016 2:11 AM