Hashtable with MultiDimensional Key in C#
I think a better approach is to encapsulate the many fields of your multi-dimensional key into a class / struct. For example
struct Key {
public readonly int Dimension1;
public readonly bool Dimension2;
public Key(int p1, bool p2) {
Dimension1 = p1;
Dimension2 = p2;
}
// Equals and GetHashCode ommitted
}
Now you can create and use a normal HashTable and use this wrapper as a Key.
Hashtable with MultiDimensional Key in C#
I think a better approach is to encapsulate the many fields of your multi-dimensional key into a class / struct. For example
struct Key {
public readonly int Dimension1;
public readonly bool Dimension2;
public Key(int p1, bool p2) {
Dimension1 = p1;
Dimension2 = p2;
}
// Equals and GetHashCode ommitted
}
Now you can create and use a normal HashTable and use this wrapper as a Key.
C# multi-dimensional array, ArrayList, or hash table?
Are you sure it wouldn't be more appropriate to create a class/struct to contain the data? For example:
class Person
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string Phone
{
get;
set;
}
}
Then you'd just create an array of Person
:
var array = new Person[1];
array[0] = new Person() { FirstName = "Joe", LastName = "Smith", Phone = "foo" };
Or, since you say "flexible size", maybe you'd want a list instead:
var list = new List<Person>();
list.Add(new Person());
Update: The syntax used to set array[0]
in the first example is an object initializer; the following two snippets are roughly equivalent:
var foo = new Person();
foo.FirstName = "John";
var bar = new Person() { FirstName = "John" };
So yes, you could just call list.Add(new Person() { ... })
if you wanted to. You could also make use of collection initializers in this case:
var john = new Person() { FirstName = "John" };
var joe = new Person() { FirstName = "Joe" };
var list = new List<Person>() { john, joe };
Adding more than one value to a single key in a hashtable in c#?
In the general case, you could use a List<string>
for the value, and just Add
to it. However, you can probably simplify with LINQ via ToLookup
:
var groups = File.ReadLines(path)
.Select(line => line.Split(':'))
.ToLookup(x => x[0], x => x[1].Trim());
Now you can access groups[key]
which gives you all the values with that prefix, or you can foreach
over groups
to get each combination of .Key
and values.
In terms of your code, this is:
var groups = File.ReadLines(@"e:\test.txt")
.Select(line => line.Split(':'))
.ToLookup(x => x[0], x => x[1].Trim());
foreach(var val in groups["Book"])
listBox1.Items.Add(val);
(no need to check for existence first, it just works correctly if no match)
However! You only need to do this if you still want all the values after this code, i.e. you use groups
somewhere else. If you don't, you can be more frugal and just abandon the unwanted data:
var values = File.ReadLines(@"e:\test.txt")
.Where(line => line.StartsWith("Book:"))
.Select(line => line.Substring(5).Trim());
foreach(var val in values)
listBox1.Items.Add(val);
Edit: minor thing - a vexing method signature means that line.Split(':')
actually creates an array every time, because params
; so I usually use:
static readonly char[] Colon = {':'};
and
line.Split(Colon)
Which is measurably more efficient if it is a hot path.
how can I make string multidimensional array with string keys and values in C#?
Arrays in C# only allow you to find an element by the index (integer), not by an arbitrary string. In C#, that's a Dictionary<>
.
You can use a dictionary of a dictionary, but it's not as easy:
var data = new Dictionary<string, Dictionary<string, string>>();
data["settings"] = new Dictionary<string, string>();
data["settings"]["width"] = "bbb";
But that seems overly complicated. If you know you'll have "settings", then it's probably more readable to just have one dictionary for settings:
var settings = new Dictionary<string, string>();
settings["width"] = "bbb";
Creating a multi-dimensional hashtable with generics in VB ASP.NET?
OK, I'm better at C# than vb.net, but I'll give this a go....
Dim myHash as Dictionary(Of string, Dictionary(Of string, Integer));
Related Topics
How to "Flatten" or "Index" 3D-Array in 1D Array
Post Form Data Using Httpwebrequest
How to Disable a Tab Inside a Tabcontrol
How to Modify or Delete Items from an Enumerable Collection While Iterating Through It in C#
Retrieve Image from Database in ASP.NET
Why Are Private Fields Private to the Type, Not the Instance
Why Is the "F" Required When Declaring Floats
X509Certificate Constructor Exception
Make Listview.Scrollintoview Scroll the Item into the Center of the Listview (C#)
Programmatically Set Browser Proxy Settings in C#
How to Run Visual Studio Without Plugin and All Third Party Feature
Tabcontrol with Close and Add Button
What Is the Worst Gotcha in C# or .Net