.NET基本问题 [英] .NET fundamental question
问题描述
大家好,我到处搜索,但可能没有在谷歌中找到正确的方法。因此我在这里问。我正在学习C sharp和ASP.net并且正在学习很多代码。
我在很多地方看到这种表达方式我不喜欢我知道它是如何工作的以及我如何制造我的。例如:
objectinstance [ String1] = SomeString
这是怎么回事?
这是我发现的一个地方:
HttpCookie mycookie = new HttpCookie( freshCookie)
mycookie [ UserInfo] = 管理员
代码的第二行发生了什么?这个陈述的内部机制是什么?
还有一个地方我找到了这样的东西:
int ClickCount = 1
ViewState [ 点击] = ClickCount
再一次,ViewState是什么类型的?这是一堂课吗?这样的陈述是如何起作用的?
请帮助我。
最简单的方法是将它减少到它的基础:一个字典类。
字典<字符串,字符串> list = new Dictionary< string,string>();
这会创建一个新的Dictionary实例,它接受一个字符串Key和一个字符串Value。
您可以为新密钥分配新值:
list [ hello] = there;在Dictionary中创建一个新元素,其中键为hello,值为there。
像基于书籍的词典一样,你可以访问a只需指定键值:
Console.WriteLine(list [ 你好]);
打印
那里
您可以创建一个以任何.NET类作为键的字典,以及任何其他.NET类作为值,并且因为它是一个Generic类,Value将始终是您指定的类型。所以如果你有一个TextBoxes字典:
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 = 再见,17.9!;
list [ 17 .9F] = tb;
...
Console.WriteLine(list [ 1 .3F] .Text);
...
foreach (TextBox t in list.Values)
{
Console.WriteLine(t.Text);
}
它会自行排序......
(虽然我不会真的推荐一个浮动键实践!:笑:)
在HttpCookie的情况下,它做同样的事情 - 它使用内部字典来保存键/值对 - 但它也生成要更新的代码客户端计算机上的cookie,或通过回发从客户端获取值。
括号中的名称是对象的属性(或属性),表达式设置为属性为特定值。因此,在上面的上一个示例中, Clicks 属性。 ui.control.viewstate.aspx>ViewState
[ ^ ]对象设置为值1
。
解决方案2不是解释此类事物的最基本级别。您首先需要了解的是索引器:
http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx [ ^ ]。
-SA
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:
objectinstance["String1"]="SomeString"
How does this work?
Here is one place where I found out:
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:
int ClickCount=1
ViewState["Clicks"]=ClickCount
Again, what type is ViewState? Is it a class? How does such a statement works?
Please help me.
The easiest way to look at it is to reduce it to it's basics: A Dictionary class.
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:
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:
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:
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.
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, theClicks
property of theViewState
[^] object is set to the value1
.
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
这篇关于.NET基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!