StringBuilder first = new StringBuilder();
StringBuilder second = first;
String str = "Love";
有沒有辦法檢查變量「second」是否是類型引用,而變量「str」是Type Value?我一直在谷歌搜索仍然無法得到它,在C#這裏很新。我知道有second.getType()
,但是如果第二個是Type Reference,我不知道。如何確定變量是否是C#中的類型引用?
非常感謝。
其他信息
在這裏,我想坦率地說在這裏,我面對的升C編程測試,當然它是一個開放的書測試,因爲我在一個封閉或限制類:-)我不是。我更熟悉PHP,C/C++,Perl,但在C語言中很新,但我喜歡瞭解它。這是他們的測試。我已經填寫了一些功能,只留下了2或3,那些是ref和unref。如果您看到下面的代碼,我需要在PrintSortedData函數中輸出<>之間的引用類型。測試問題在代碼的評論。也許我還沒有正確的編程邏輯。
/// The DataObject class stored with a key
class DataObject
{
public string key = "";
public int value = 0;
// Populate
public DataObject(string k, int v = 0)
{
key = k;
value = v;
}
}
class Program
{
static Hashtable Data = new Hashtable();
static string[] StaticData = new string[] { "X-Ray","Echo","Alpha", "Yankee","Bravo", "Charlie",
"Delta", "Hotel", "India", "Juliet", "Foxtrot","Sierra",
"Mike","Kilo", "Lima", "November", "Oscar", "Papa", "Qubec",
"Romeo", "Tango","Golf", "Uniform", "Victor", "Whisky",
"Zulu"};
static void Main(string[] args)
{
for (int i = 0; i < StaticData.Length; i++)
Data.Add(StaticData[i].ToLower(), new DataObject(StaticData[i]));
while (true)
{
PrintSortedData();
Console.WriteLine();
Console.Write("> ");
string str = Console.ReadLine();
string[] strs = str.Split(' ');
if (strs[0] == "q")
break;
else if (strs[0] == "print")
PrintSortedData();
else if (strs[0] == "swap")
Swap(strs[1], strs[2]);
else if (strs[0] == "ref")
Ref(strs[1], strs[2]);
else
Console.WriteLine("Invalid Input");
}
}
/// <summary>
/// Create a reference from one data object to another.
/// </summary>
/// <param name="key1">The object to create the reference on</param>
/// <param name="key2">The reference object</param>
static void Ref(string key1, string key2)
{
}
/// <summary>
/// Removes an object reference on the object specified.
/// </summary>
/// <param name="key">The object to remove the reference from</param>
static void UnRef(string key)
{
// Populate
}
/// <summary>
/// Prints the information in the Data hashtable to the console.
/// Output should be sorted by key
/// References should be printed between '<' and '>'
/// The output should look like the following :
///
///
/// Alpha...... -3
/// Bravo...... 2
/// Charlie.... <Zulu>
/// Delta...... 1
/// Echo....... <Alpha>
/// --etc---
///
/// </summary>
static void PrintSortedData()
{
// Populate
ArrayList keys = new ArrayList(Data.Keys);
keys.Sort();
foreach (object obj in keys)
{
Console.Write(obj + "......." + ((DataObject)Data[obj]).value + "\n");
}
}
}
'first','秒'和'str'是*所有*引用。在這種情況下,'first'和'second'都指向同一個對象。 – Gabe 2010-12-18 07:36:15