1.cs

来自「Professional ASP.NET source code」· CS 代码 · 共 63 行

CS
63
字号

using System.Collections;
using System;

public struct Person
{
    public string Name;
    public int Age;
}


public class MainClass
{
	public static void Main()
	{
		Hashtable dictionary;
		Person p;

		// Create a Hastable - this is a reference type, so we use New
		// to create the object on the managed CLR heap.

		dictionary = new Hashtable();

		// Setup our Person Structure - This is value type, so we do
		// not use New, and the type is just allocated on the stack.

		p.Name = "Richard Anderson";
		p.Age = 29;

		// Add our Person value type to the dictionary.  This will cause
		// the value type to be boxed within a reference type, and added
		// to the dictionary.

		dictionary.Add( "Rich", p );

		// Setup the value type (overwriting the old values)

		p.Name = "Sam Anderson";
		p.Age = 28;
		
		// Add our Person value type to the dictionary.  Again, this will cause
		// the value type to be boxed within a reference type and adde to the
		// dictionary.

		dictionary.Add( "Sam", p );

		// Retreive a person by Name.  The CType keyword tells VB that
		// the object type returned by the Item function is a Person
		// type.  Since the Person type is a value type, the CLR knows
		// to unbox the Person type from the returned object type.

		p = (Person)dictionary["Rich"];

		// We use the Person type

		Console.WriteLine("Name is {0} and Age is {1}",
			p.Name, p.Age );
		
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?