473,396 Members | 1,891 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,396 software developers and data experts.

Generics without direct instantiation

Hi,

This is just for my own interest - I don't actually want to do it, I'm just
curious to know if it's possible...

Say we have the following method which accepts a List<string>...

private void MyMethod(List<stringplstMyList)
{
...
}

We could make a call to MyMethod like this:

List<stringlstMyList = new List<string>();
lstMyList.Add("This is a string");
MyMethod(lstMyList);
However, although it might not be particularly elegant, we could also do the
following:

MyMethod(new List<string>(new string[] { "This is a string" });

This made me wonder if it would be possible to pass a Dictionary<string,
stringvariable in the same way e.g.

private void MyMethod(Dictionary<stringpdicMyDictionary)
{
...
}

Dictionary<string, stringdicMyDictionary = new Dictionary<string,
string>();
dicMyDictionary.Add("This is a key", "This is a value");
MyMethod(dicMyDictionary);

Could we also do something like MyMethod(new Dictionary<string,
string>(...................);
Mar 3 '07 #1
6 1175
Mark Rae <ma**@markNOSPAMrae.comwrote:
This is just for my own interest - I don't actually want to do it, I'm just
curious to know if it's possible...
<snip>
Could we also do something like MyMethod(new Dictionary<string,
string>(...................);
I'm not sure exactly what you're asking. You could certainly call
MyMethod(new Dictionary<string,string>());

or use any of the other constructors of Dictionary<K,V- but there
aren't any constructors which take an IEnumerable in the same way that
List<Tdoes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 3 '07 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
I'm not sure exactly what you're asking. You could certainly call
MyMethod(new Dictionary<string,string>());
Yes, but that would pass an empty Dictionary object...
or use any of the other constructors of Dictionary<K,V- but there
aren't any constructors which take an IEnumerable in the same way that
List<Tdoes.
OK - I think that answers my question - thanks...
Mar 3 '07 #3
Mark Rae wrote:
Hi,

This is just for my own interest - I don't actually want to do it, I'm just
curious to know if it's possible...

Say we have the following method which accepts a List<string>...

private void MyMethod(List<stringplstMyList)
{
...
}

We could make a call to MyMethod like this:

List<stringlstMyList = new List<string>();
lstMyList.Add("This is a string");
MyMethod(lstMyList);
However, although it might not be particularly elegant, we could also do the
following:

MyMethod(new List<string>(new string[] { "This is a string" });

This made me wonder if it would be possible to pass a Dictionary<string,
stringvariable in the same way e.g.

private void MyMethod(Dictionary<stringpdicMyDictionary)
{
...
}

Dictionary<string, stringdicMyDictionary = new Dictionary<string,
string>();
dicMyDictionary.Add("This is a key", "This is a value");
MyMethod(dicMyDictionary);

Could we also do something like MyMethod(new Dictionary<string,
string>(...................);

No, there is no constructor for a dictionary that takes an array.

--
Göran Andersson
_____
http://www.guffa.com
Mar 3 '07 #4


"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:eB*************@TK2MSFTNGP06.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>I'm not sure exactly what you're asking. You could certainly call
MyMethod(new Dictionary<string,string>());

Yes, but that would pass an empty Dictionary object...
>or use any of the other constructors of Dictionary<K,V- but there
aren't any constructors which take an IEnumerable in the same way that
List<Tdoes.

OK - I think that answers my question - thanks...
This is another good reason not to declare methods that accept the concrete
generic collections. If you declare MyMethod as

void MyMethod(IDictionary<string,stringmyDictionary)
or
void MyMethod(IList<stringmyList)

Then you can create new types whose constructors you control to pass in for
convenience.

David

Mar 3 '07 #5
On Mar 3, 5:14 am, "Mark Rae" <m...@markNOSPAMrae.comwrote:
Hi,

This is just for my own interest - I don't actually want to do it, I'm just
curious to know if it's possible...

Say we have the following method which accepts a List<string>...

private void MyMethod(List<stringplstMyList)
{
...

}

We could make a call to MyMethod like this:

List<stringlstMyList = new List<string>();
lstMyList.Add("This is a string");
MyMethod(lstMyList);

However, although it might not be particularly elegant, we could also do the
following:

MyMethod(new List<string>(new string[] { "This is a string" });

This made me wonder if it would be possible to pass a Dictionary<string,
stringvariable in the same way e.g.

private void MyMethod(Dictionary<stringpdicMyDictionary)
{
...

}

Dictionary<string, stringdicMyDictionary = new Dictionary<string,
string>();
dicMyDictionary.Add("This is a key", "This is a value");
MyMethod(dicMyDictionary);

Could we also do something like MyMethod(new Dictionary<string,
string>(...................);
I think what you're trying to do will be possible with the new 3.0 c#
specification.

Mar 4 '07 #6
"Sir C4" <Va*******@gmail.comwrote in message
news:11**********************@i80g2000cwc.googlegr oups.com...
I think what you're trying to do
As I mentioned, I'm not actually trying to do this - it was just to satisfy
my curiosity...
will be possible with the new 3.0 c# specification.
Oh really...? I'll have a look - thanks for the tip...
Mar 4 '07 #7

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

Similar topics

8
by: Jon Slaughter | last post by:
I'm reading Alexandrescu's Modern C++ design about the concept of policies. What I'm wondering is if policies are a common design pattern in C++(OOP) nowdays? It seems like a good idea but I'm...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
4
by: Michael Sparks | last post by:
I started writing some code with the 2.0 beta compiler, and found out quickly that specialization is not supported with generics. For example, I want to do something like this: class number...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
17
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
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...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
5
by: Wiktor Zychla [C# MVP] | last post by:
suppose I have a class class MyContainter { List<something1list1; List<something2list2; ... List<somethingnlistn; othertype1 otherfield1; ...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.