Rajanikant Hawaldar
Dictionary is value type or reference type?

Dictionary is value type or reference type?

By Rajanikant Hawaldar in C# on Jul 14 2021
  • Sonil  Kumar
    Jul, 2021 26

    Value types - Stored on Stack. GC not required for it as it is holding value itself so no memory leak. Struct in C# are example of value type objects i.e. integer, bool and other data types except String(It is a class).

    Reference Type - It is stored on Heap and GC is needed to clean unused objects in memory. Actual value is stored on Stack but that reference is stored on Heap. If you use GetHashCode() method on int object it will give you the value it contains. But when you use GetHashCode() method on class it givens you a hashcode, which is the memory address of that object. String is a class and a reference type.

    • 2
  • Jin Necesario
    Feb, 2023 4

    It is definitely a reference type.
    It is because variables of dictionary store references to their data (objects).

    • 0
  • Alex Burov
    Jan, 2023 22

    Good question!
    I have some confusing too.
    Why in this code value will be null if I remove “ref” ?

    1. public StatisticData()
    2. {
    3. SetDefaultRecords(ref CommonRecords);
    4. }
    5. private void SetDefaultRecords(ref Dictionary<RecordName, string> dictionary)
    6. {
    7. dictionary =
    8. Enum.GetValues(typeof(RecordName))
    9. .Cast<RecordName>()
    10. .ToDictionary(key => key, "");
    11. }

    • 0
  • Rajeev Kumar
    Jan, 2023 6

    Dictionary is a reference Type.

    • 0
  • Dipak Barnwal
    Apr, 2022 26

    Reference Type because it has a reference of key and value

    • 0
  • rahul gupta
    Sep, 2021 17

    refrence type

    • 0
  • Ganesan C
    Aug, 2021 10

    It is reference type.

    • 0
  • MUMTAZ AHMAD
    Jul, 2021 31

    Reference Type because it has a reference of key and value…

    • 0
  • MUMTAZ AHMAD
    Jul, 2021 31

    Reference Type

    • 0
  • Jay Pankhaniya
    Jul, 2021 29

    Value Type

    • 0
  • Sonil  Kumar
    Jul, 2021 25

    It is a class hence it is a Reference Type. its value could be a value type (if used as structure).

    • 0
  • SA
    Jul, 2021 21

    Dictionary is a reference type
    var countryCodes = new Dictionary(string, string) {{"IN", "India"},{"JP", "Japan"} }

    • 0
  • Shweta Lodha
    Jul, 2021 21

    It’s reference type

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS