Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
3.12/5 (3 votes)
See more:
Hi All, I have searched everywhere, but possibly not asking the right way in Google. Hence I am asking here. I am learning C sharp and ASP.net and am going through a lot of codes.

I am seeing this type of expressions at a lot of places and I don't know how it works and how I can make mine. For example:

C#
objectinstance["String1"]="SomeString"


How does this work?

Here is one place where I found out:
C#
HttpCookie mycookie = new HttpCookie("freshCookie")
mycookie["UserInfo"] = "Admin"


What is happening in the second line of the code? What is the internal mechanism by which this statement works?

Also there is one more place where I found such a thing:
C#
int ClickCount=1
ViewState["Clicks"]=ClickCount


Again, what type is ViewState? Is it a class? How does such a statement works?
Please help me.
Posted

The easiest way to look at it is to reduce it to it's basics: A Dictionary class.
C#
Dictionary<string, string> list = new Dictionary<string, string>();
This creates a new Dictionary instance, which takes a string Key and a string Value.
You can assign a new value to a new key:
C#
list["hello"] = "there";
And a new element is created in the Dictionary, which has the Key "hello" and the Value "there".
Like a book based Dictionary, you can access a Value just by specifying the Key:
C#
Console.WriteLine(list["hello"]);
Would print "there"

You can create a Dictionary which any .NET class as the key, and any other .NET class as the value, and because it is a Generic class the Value will always be the type you specified. So if you have a Dictionary of TextBoxes:
C#
Dictionary<float, TextBox> list = new Dictionary<float,TextBox>();
TextBox tb = new TextBox();
tb.Text = "Hello, 1.3!";
list[1.3F] = tb;
tb = new TextBox();
tb.Text = "Goodbye, 17.9!";
list[17.9F] = tb;
...
Console.WriteLine(list[1.3F].Text);
...
foreach (TextBox t in list.Values)
    {
    Console.WriteLine(t.Text);
    }
It will sort itself out...
(though I wouldn't really recommend a float Key in practice! :laugh: )

In the case of an HttpCookie, it does the same thing - it uses an internal Dictionary to hold the Key/Value pairs - but it also generates the code to update the cookies on the client machine, or fetch the value from the client via the postback.
 
Share this answer
 
Comments
ridoy 21-Sep-13 13:02pm    
5ed.
The name in brackets is a property (or attribute) of the object, and the expression is setting that property to a specific value. So in the last sample above, the Clicks property of the ViewState[^] object is set to the value 1.
 
Share this answer
 
The Solution 2 is not the most fundamental level of the explanation of such things. What you need to understand first is the indexers:
http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900