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

Conversion between collection interface types

Hi All,

I have a class that has a property returning IList. For example,

class A
{
public IList<stringMyList
{
get { return _list; }
set { _list = new List<string>(value); }
}

private List<string_list;
}

Now, I have a method that has an IEnumerable and tries to set MyList
property of an object of type A. Here is how I have done it:

void f(A a, IEnumerable<stringlist)
{
a.MyList = new List<string>(list);
}

The above works fine, but I think there must be a better way to
achieving this rather than create temporary List<stringobjects. This
happens twice - 1) While converting from IEnumerable<to IList<>, and
2) While converting from IList<to List<Last thing I would want is
to change the return type of the property or the signature of f(). Any
suggestions?

On side notes, how bad is it performance wise?

- Satyajit

May 23 '07 #1
3 1285
satyajit <sa*********@gmail.comwrote:

<snip>
The above works fine, but I think there must be a better way to
achieving this rather than create temporary List<stringobjects. This
happens twice - 1) While converting from IEnumerable<to IList<>, and
2) While converting from IList<to List<Last thing I would want is
to change the return type of the property or the signature of f(). Any
suggestions?
Two options:

1) Have an extra setter in A which takes an IEnumerable<string>
2) Create a lightweight implementation of IList<Twhich accepts (and
retains) an IEnumerable<Tand does the bare minimum needed by the List
<Tconstructor.
On side notes, how bad is it performance wise?
Well, it'll be at least O(n), but possibly worse because of the copying
that will occur when the internal buffer fills up. Unfortunately I'm
too tired to work out whether that makes it O(n log n) or O(n^2). The
former, I suspect, because the copying will only happen O(log n) times
assuming the internal buffer size goes up proportionally (eg doubling)
rather than linearly.

--
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
May 23 '07 #2
On May 23, 3:34 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
<snip>
The above works fine, but I think there must be a better way to
achieving this rather than create temporary List<stringobjects. This
happens twice - 1) While converting from IEnumerable<to IList<>, and
2) While converting from IList<to List<Last thing I would want is
to change the return type of the property or the signature of f(). Any
suggestions?

Two options:

1) Have an extra setter in A which takes an IEnumerable<string>
Do you mean that I write one more property (say MyList1) that takes an
IEnumerable<>?
2) Create a lightweight implementation of IList<Twhich accepts (and
retains) an IEnumerable<Tand does the bare minimum needed by the List
<Tconstructor.
Would any of the classes provided by the .NET class library (closest)
fit in here?

Thanks.

May 23 '07 #3
satyajit <sa*********@gmail.comwrote:
Two options:

1) Have an extra setter in A which takes an IEnumerable<string>

Do you mean that I write one more property (say MyList1) that takes an
IEnumerable<>?
I wouldn't write a whole extra property - just a single method.
2) Create a lightweight implementation of IList<Twhich accepts (and
retains) an IEnumerable<Tand does the bare minimum needed by the List
<Tconstructor.

Would any of the classes provided by the .NET class library (closest)
fit in here?
I don't know of any, I'm afraid.

--
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
May 24 '07 #4

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

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
3
by: eXavier | last post by:
Hello, I have class (let's say A) wrapping Int32 type and have added implicit conversion operators for converting between int and A. Now I would like to have also explicit operator for conversion...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: hellrazor | last post by:
Hi there, I'm trying to consume a web-service that is supposed to return a collection of a custom object. The web-service was not created with C# or VS.net. It was created with IBM VisualAge...
1
by: Akshay Kumar | last post by:
I have a collection class and in order to make it type safe etc , I only add and retrive members of Interface types. But during XMLSerialization I get errors. Is there any workaround ? ...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
1
by: CSharper | last post by:
It is a strange question; I have a collection of Types which is derived off of IDictionary. When ever something changes in the collection, I want to mark the collection as changed a boolean...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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:
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.