473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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 <nullmost 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<Twhere 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<Twhere 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

Aug 19 '08 #1
3 1255
On Aug 19, 5:59*am, "Dmitry Nogin" <dmitry.no...@hotmail.comwrote:
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 <nullmost of the time, so it might save a lot of memory.)
<snip>
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
Aug 19 '08 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message news:8e**********************************@79g2000h sk.googlegroups.com...
On Aug 19, 5:59 am, "Dmitry Nogin" <dmitry.no...@hotmail.comwrote:
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 <nullmost 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.
Thanks.

I tried this, but in case of T==int? (which does not look so good, because of no constraints applicable here) - how can I expose property of original none-nullable type?

Can we provide generic "specializations" (in c++ template understanding) anyhow?
-- dmitry
Aug 19 '08 #3
On Aug 19, 1:18*pm, "Dmitry Nogin" <dmitry.no...@hotmail.comwrote:
"Jon Skeet [C# MVP]" <sk...@pobox.comwrote in messagenews:8e**********************************@7 9g2000hsk.googlegroups.com...
On Aug 19, 5:59 am, "Dmitry Nogin" <dmitry.no...@hotmail.comwrote:
I would like to use dictionaries to implement my property value storingmechanics.
(I have a lot but many of them are just about to keep <nullmost 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.

Thanks.

I tried this, but in case of T==int? (which does not look so good, because of no constraints applicable here) - how can I expose property of original none-nullable type?
At what point? If you could provide a short but complete program with
your current state of play, and what it is you don't have yet, that
would be helpful.

I would change the Rating property, for instance from:
return _ints["Rating", 0];
to
return _ints["Rating"] ?? 0;

At that point your dictionary class doesn't really have to know the
difference between nullable and non-nullable - and you'd be specifying
the defaults anyway.
Can we provide generic "specializations" (in c++ template understanding) anyhow?
Not really, no. Generics in .NET are somewhat different to C++
templates.

Jon
Aug 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Keith R | last post by:
Currently, generic types are not CLS compliant. This puts library authors in a quandry who are faced with three bad alternatives: 1. CLS Compliance but no generics, 2. Using generic types but...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
11
by: Alexander van Doormalen | last post by:
I have a xml file with data from various sources. For example: <root> <Account> <Id>1</Id> <Name>Somename</Name> <City>Somecity</City> <ContactPersons> <ContactPerson> <Id>1</Id>
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.