If I have a SotedList
SortedList sList = …
and I want a string array of the Keys, then this code does that
IList ccList = sList.Keys; string[] cc = new string[cc.Count]; ccList.CopyTo(cc,0);
Is it possible to do this in a single line of code?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » Developers, developers, developers » DevOps Lounge » Get the Keys From a Generic Sorted List (C# 2005)
I’m in the process of converting a zillion lines of code from framework 1.1 to framework 2.0. As we convert to generics we are entering code like this a lot, so a one-liner would mean less time to convert. Plus, it’s just a simple copy: should be one line.
BTW, there was a typo in the code: here is a working example:
private void button1_Click(object sender, EventArgs e) { SortedList sList = new SortedList(); sList.Add("A", "1"); sList.Add("B", "2"); sList.Add("C", "3"); // // Should be a one-liner for the next three lines IList keyList = sList.Keys; string[] keys = new string[keyList.Count]; keyList.CopyTo(keys, 0); }
I think the problem here is the CopyTo function. Since it is a void and expects a variable to be passed in, you’re going to have to declare the array variable on its own line before calling the CopyTo method.
Of course, you could always remove the line breaks and make it one long line (keeling the semi-colons).
Also, have you looked into using a Code generator (such as My Generation or CodeSmith)? These are generally used in conjunction with a data tier, but can also be manipulated to generate based on any data source. It may take a bit of setup work, but you’ll find the results to be staggering!
BTW – I thought you have been a VB(.NET) guy in the past. Working with C# now?
>the problem here is the CopyTo function
Right, it will take a different approach. The real problem is that IList does not have a ToArray method. I tried casting it into something else, but failed.
> you could always remove the line breaks
> you have been a VB(.NET) guy
At this point, I’ve forgotten more than I know about VB. For about a year, I’ve been creating tools for a Geographic Information System, ArcMap, using C#. Its object model is so horrific http://edndoc.esri.com/arcobjects/8.3/Diagrams/Map Layer Object Model.pdf[/url], that I’ll never complain about Office objects again.
Eureka!
string[] keys = new List(sList.Keys).ToArray();
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications