Dictionary is value type or reference type?
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.
It is definitely a reference type.It is because variables of dictionary store references to their data (objects).
Good question!I have some confusing too.Why in this code value will be null if I remove “ref” ?
public StatisticData(){ SetDefaultRecords(ref CommonRecords);}private void SetDefaultRecords(ref Dictionary<RecordName, string> dictionary){ dictionary = Enum.GetValues(typeof(RecordName)) .Cast<RecordName>() .ToDictionary(key => key, "");}
public StatisticData()
{
SetDefaultRecords(ref CommonRecords);
}
private void SetDefaultRecords(ref Dictionary<RecordName, string> dictionary)
dictionary =
Enum.GetValues(typeof(RecordName))
.Cast<RecordName>()
.ToDictionary(key => key, "");
Dictionary is a reference Type.
Reference Type because it has a reference of key and value
refrence type
It is reference type.
Reference Type because it has a reference of key and value…
Reference Type
Value Type
It is a class hence it is a Reference Type. its value could be a value type (if used as structure).
Dictionary is a reference typevar countryCodes = new Dictionary(string, string) {{"IN", "India"},{"JP", "Japan"} }
var countryCodes = new Dictionary(string, string) {{"IN", "India"},{"JP", "Japan"} }
It’s reference type