473,800 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Twhe re T : struct

{

private readonly Dictionary<stri ng, T_values;



public ValueStock()

{

_values = new Dictionary<stri ng, T>();

}



public T? this[string name]

{

get

{

if (_values.Contai nsKey(name))

return _values[name];



return null;

}

set

{

if (value.HasValue )

_values[name] = value.Value;

else

if (_values.Contai nsKey(name))

_values.Remove( name);

}

}

public T this[string name, T defaultValue]

{

get

{

return this[name] ?? defaultValue;

}

set

{

if (value.Equals(d efaultValue))

this[name] = null;

else

this[name] = value;

}

}
}
public class ReferenceStock< Twhere T : class

{

private readonly Dictionary<stri ng, T_values;



public ReferenceStock( )

{

_values = new Dictionary<stri ng, T>();

}



public T this[string name]

{

get

{

if (_values.Contai nsKey(name))

return _values[name];



return null;

}

set

{

if (value != null)

_values[name] = value;

else

if (_values.Contai nsKey(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<stri ng_strings;

private readonly FieldStock<int_ ints;

private readonly FieldStock<bool _bools;

Thanks,
-- dmitry

Aug 19 '08 #1
3 1269
On Aug 19, 5:59*am, "Dmitry Nogin" <dmitry.no...@h otmail.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<stri ng,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.co mwrote in message news:8e******** *************** ***********@79g 2000hsk.googleg roups.com...
On Aug 19, 5:59 am, "Dmitry Nogin" <dmitry.no...@h otmail.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<stri ng,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 "specialization s" (in c++ template understanding) anyhow?
-- dmitry
Aug 19 '08 #3
On Aug 19, 1:18*pm, "Dmitry Nogin" <dmitry.no...@h otmail.comwrote :
"Jon Skeet [C# MVP]" <sk...@pobox.co mwrote in messagenews:8e* *************** *************** ***@79g2000hsk. googlegroups.co m...
On Aug 19, 5:59 am, "Dmitry Nogin" <dmitry.no...@h otmail.comwrote :
I would like to use dictionaries to implement my property value storingmechanic s.
(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<stri ng,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 "specialization s" (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
2164
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 losing CLS Compliance, or 3. Supporting both, bloating the code and muddying the waters with dual versions of generic classes. This latter is seen in mscorlib with collections classes, Nullable, and EventHandler as examples. I believe there is a...
17
3331
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
10
2608
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 other than straining our relationship... I just downloaded Visual C# Express Edition to mess with .NET 2.0 generics. Being a numerically inclined developer, the first thing I wanted to write was a generic complex number class. I also have some...
12
2747
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 it defines the language more completely. Great to use.
11
1617
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
2821
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 RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to another library and call this list of functions. This could provide a new way to build rule base...
4
10127
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<>, both works. private void Form1_Load(object sender, EventArgs e) { System.Collections.SortedList QA1 = new System.Collections.SortedList();
3
2730
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 OLE Object (load a picture from a given path). Something like this I would expect: _reports.crImage local_Report = new _reports.crImage();
3
1302
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 current non-generic implementation is functional, but not as clean as I would like. Although not sure, I believe my problems stem from lacking support of co- and contravariance in C# (which I'm desperately hoping will make it in the next version)....
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.