0

I am working on wpf application and i have to pass some global objects from one class to other, so i am declaring a parameterized construtor for the class, my concern is which one would performance better as a parameter , dictionary or hashtable.
i read this post Difference between Dictionary and Hashtable

following code is using hashtable

      public partial class Sample: Window
        {
      Hashtable session = new Hashtable();

 string Path= string.Empty;
 string PathID= string.Empty;

           public Sample(Hashtable hashtable)
            {
                if (session != null)
                {
                    this.session = hashtable;
                    Path= session["Path"].ToString()
                    PathID= session["MainID"].ToString();
                }
                InitializeComponent();
            }
     private void Window_Loaded(object sender, RoutedEventArgs e)
            {
            }
    }
Community
  • 1
  • 1
Buzz
  • 6,030
  • 4
  • 33
  • 47
  • `Hashtable` is untyped, seems to me you want an `IDictionary`, or better still, define a class that actually represents your settings. – Jodrell Aug 16 '12 at 09:23
  • Dictionary, in my experience, is faster, but read only... You can assign a dictionary, but then only read from it. If you want to edit data in it, you'll have to use a hash table. – TheGeekZn Aug 16 '12 at 09:26
  • 1
    @NewAmbition: Why is a [`Dictionary`](http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.100%29.aspx) read-only? Never was ... – O. R. Mapper Aug 16 '12 at 09:28
  • @O.R.Mapper; Read-only as in you can only define it in the beginning, then read from it throughout the application. Dictionary's are meant for quickly finding data in it using wither a key or value. I've never been able to edit a single key/value after defining it. I may be horribly mistaken. – TheGeekZn Aug 16 '12 at 09:30
  • @NewAmbition: You can use methods such as [`Add`](http://msdn.microsoft.com/en-us/library/k7z0zy8k%28v=vs.100%29.aspx) or [`Remove`](http://msdn.microsoft.com/en-us/library/kabs04ac%28v=vs.100%29.aspx) whenever you like on a dictionary, and you can also set or overwrite items by using the [indexer](http://msdn.microsoft.com/en-us/library/9tee9ht2%28v=vs.100%29.aspx). – O. R. Mapper Aug 16 '12 at 09:32
  • @O.R.Mapper, deleting and adding is different to editing an entry. – TheGeekZn Aug 16 '12 at 09:36
  • @NewAmbition: What do you mean by *editing an entry* then? Changing the value associated with a given key? You can use the indexer for that, as I said, or (for complex values) the respective members of the value. – O. R. Mapper Aug 16 '12 at 09:50
  • @jodell:there are doing to be more than 2 item in hashtable,and i thing hash table is going to be more suitable for my need , my only concern was the performance – Buzz Aug 16 '12 at 09:55
  • @Buzz I've edited my answer to illustrate. If you are concerned about performance get explicit. – Jodrell Aug 16 '12 at 10:22

2 Answers2

3

Wouldn't,

public Sample(string path, string mainId)
{
    this.Path = path;
    this.PathID = mainId;

    InitializeComponent();
}

Be simpler, faster and easier to read, bringing errors to compile time etc?


In the event that the values to be passed are too numerous,

class NumerousSettings
{
    public string Path {get; set;};
    public string MainId  {get; set;};
    ...
}

public Sample(NumerousSettings settings)
{
    if (settings == null)
    {
        throw new CallTheDefaultContructorException();
    }

    this.Path = settings.Path;
    this.PathID = settings.MainId;
    ...

    InitializeComponent();
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • i have shown objects for simplicity ,there are gonaa be more than 2 item in hashtable – Buzz Aug 16 '12 at 09:46
  • 1
    @Buzz, Since you now what the settings will be you can declare a type for them. By using weaker typing or unecessary serialization you are saving up the pain for runtime. A defined compiled type is also faster, there is no lookup time. – Jodrell Aug 16 '12 at 10:00
1

i read this post Difference between Dictionary and Hashtable

OK, well Marc's answer there seemed pretty clear...

"If you are .NET 2.0 or above, you should prefer Dictionary (and the other generic collections)

A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary."

If you have no need for thread safety then the Dictionary is the preferred approach for type safety and performance.

Community
  • 1
  • 1
Steve
  • 8,469
  • 1
  • 26
  • 37