Generics - can i use the same generic name for all situations?

D

Dmitry Nogin

Hi,

I would like to use dictionaries to implement my property value storing mechanics.
(I have a lot but many of them are just about to keep <null> most of the time, so it might save a lot of memory.)

So, my solution is:

class Person

{

private readonly ReferenceStock<string> _strings = new ReferenceStock<string>();

private readonly ValueStock<int> _ints = new ValueStock<int>();

private readonly ValueStock<bool> _bools = new ValueStock<bool>();



public string Name

{

get { return _strings["Name"]; }

set { _strings["Name"] = value; }

}



public int? Age

{

get { return _ints["Age"]; }

set { _ints["Age"] = value; }

}




public int Rating

{

get { return _ints["Rating", 0]; }

set { _ints["Rating", 0] = value; }

}

public bool? Married

{

get { return _bools["Married"]; }

set { _bools["Married"] = value; }

}

}

where ReferenceStock/ValueStock are:


public class ValueStock<T> where T : struct

{

private readonly Dictionary<string, T> _values;



public ValueStock()

{

_values = new Dictionary<string, T>();

}



public T? this[string name]

{

get

{

if (_values.ContainsKey(name))

return _values[name];



return null;

}

set

{

if (value.HasValue)

_values[name] = value.Value;

else

if (_values.ContainsKey(name))

_values.Remove(name);

}

}



public T this[string name, T defaultValue]

{

get

{

return this[name] ?? defaultValue;

}

set

{

if (value.Equals(defaultValue))

this[name] = null;

else

this[name] = value;

}

}
}


public class ReferenceStock<T> where T : class

{

private readonly Dictionary<string, T> _values;



public ReferenceStock()

{

_values = new Dictionary<string, T>();

}



public T this[string name]

{

get

{

if (_values.ContainsKey(name))

return _values[name];



return null;

}

set

{

if (value != null)

_values[name] = value;

else

if (_values.ContainsKey(name))

_values.Remove(name);

}

}

}

The question is:

Do I have any chances to use the same name instead of ReferenceStock and ValueStock (like C++ templates allow to do in this situation)?



I just want to be able to write the following for every possible type of parameter:



private readonly FieldStock<string> _strings;

private readonly FieldStock<int> _ints;

private readonly FieldStock<bool> _bools;



Thanks,
-- dmitry
 
J

Jon Skeet [C# MVP]

I would like to use dictionaries to implement my property value storing mechanics.
(I have a lot but many of them are just about to keep <null> most of the time, so it might save a lot of memory.)

The question is:

Do I have any chances to use the same name instead of ReferenceStock and ValueStock (like C++ templates allow to do in this situation)?

Absolutely. All you really need is something like a
Dictionary<string,T> - and then use default(T) to find the appropriate
default value. It would be a lot simpler if you were happy to use
FieldStock<int?> as then I supect you could get away without any
special-casing of value types in your code for FieldStock.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top