Generics without direct instantiation

M

Mark Rae

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<string> plstMyList)
{
...
}

We could make a call to MyMethod like this:

List<string> lstMyList = 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,
string> variable in the same way e.g.

private void MyMethod(Dictionary<string> pdicMyDictionary)
{
...
}

Dictionary<string, string> dicMyDictionary = 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>(...................);
 
J

Jon Skeet [C# MVP]

Mark Rae said:
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...

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<T> does.
 
M

Mark Rae

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<T> does.

OK - I think that answers my question - thanks...
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
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<string> plstMyList)
{
...
}

We could make a call to MyMethod like this:

List<string> lstMyList = 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,
string> variable in the same way e.g.

private void MyMethod(Dictionary<string> pdicMyDictionary)
{
...
}

Dictionary<string, string> dicMyDictionary = 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.
 
D

David Browne

Mark Rae said:
Yes, but that would pass an empty Dictionary object...


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,string> myDictionary)
or
void MyMethod(IList<string> myList)

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

David
 
S

Sir C4

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<string> plstMyList)
{
...

}

We could make a call to MyMethod like this:

List<string> lstMyList = 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,
string> variable in the same way e.g.

private void MyMethod(Dictionary<string> pdicMyDictionary)
{
...

}

Dictionary<string, string> dicMyDictionary = 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.
 
M

Mark Rae

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...
 

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