By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

confusion regarding hashtable.

P: n/a
Hi all,

I am having one confusion regarding hashtable.

I am having function in which i am passing hashtable as reference. In
function i am creating one hashtable which is local to that function.
Then i am setting this hash table to hashtable which i am passing as
ref.

So my question is how scope is mention when i am assigning local
variable of function to reference parameter.

please correct me if i am wrong.

thanks in advance.

Apr 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Apr 18, 11:26 am, archana <trialproduct2...@yahoo.comwrote:
Hi all,

I am having one confusion regarding hashtable.

I am having function in which i am passing hashtable as reference. In
function i am creating one hashtable which is local to that function.
Then i am setting this hash table to hashtable which i am passing as
ref.

So my question is how scope is mention when i am assigning local
variable of function to reference parameter.

please correct me if i am wrong.

thanks in advance.
The ref keyword affects this:
Without the ref keyword the parameter variable is local to the method:

public void Method1()
{
Hashtable table = new Hashtable();
table.Add(1, "One");
Method2(table);

// table is still the hashtable created here
// so outputs false
Console.WriteLine(table.Contains(2));
}
public void Method2(Hashtable table1)
{
Hashtable table2 = new Hashtable();
table2.Add(2, "Two");

// this only reassigns the local "table1" reference
table1 = table2;
}

With the ref keyword you can change the reference:
public void Method1()
{
Hashtable table = new Hashtable();
table.Add(1, "One");
Method2(ref table);

// now table reference has been reassinged
// so this outputs false
Console.WriteLine(table.Contains(2));
}
public void Method2(ref Hashtable table1)
{
Hashtable table2 = new Hashtable();
table2.Add(2, "Two");

// this reassigns the reference which was passed in
table1 = table2;
}
Terry

Apr 18 '07 #2

P: n/a
On Apr 18, 11:26 am, archana <trialproduct2...@yahoo.comwrote:
I am having one confusion regarding hashtable.

I am having function in which i am passing hashtable as reference. In
function i am creating one hashtable which is local to that function.
Then i am setting this hash table to hashtable which i am passing as
ref.

So my question is how scope is mention when i am assigning local
variable of function to reference parameter.
You need to be very clear about the difference between a parameter
passed *by* reference (i.e. where you've got "ref" in the signature)
and reference types where the reference is passed by value.

See http://pobox.com/~skeet/csharp/parameters.html for more info on
this.

Jon

Apr 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.