如何在C#中确定变量是否为Type Reference?

时间:2010-12-18 07:31:51

标签: c# .net types reference

StringBuilder first = new StringBuilder();
StringBuilder second = first;

String str = "Love";

有没有办法检查变量“second”是否为Type Reference,而变量“str”是否为Type Value?我一直在谷歌搜索仍然无法得到它,这里的C#非常新。我知道有second.getType()但是如果第二个是类型参考,我不知道。

非常感谢。

其他信息

在这里,我想坦率地说,我正在面对C Sharp的编程测试,当然这是一个开放的书测试,因为我不是在封闭或限制类:-)。我对PHP,C / C ++,Perl更熟悉,但是在C sharp中非常新,但我喜欢了解它。这是他们的考试。我已经填写了一些函数,只剩下2或3,那些是ref和unref。如果您看到下面的代码,我需要在<之间打印出Reference类型。 >在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");
        }
    }
}

3 个答案:

答案 0 :(得分:2)

两个变量都是引用类型。对象StringBuilder的实例仍然是引用类型,类型为string的对象。

C#中的值类型是用户定义的structs,枚举和数字类型(例如intfloatdoubledecimal,有关更多信息,请参阅Value Types上的文档。

引用类型是其他所有内容,包括类,接口,委托,甚至一些内置类型,如stringobject。有关详细信息,请参阅Reference Types上的文档。


至于你的隐含问题,你怎么能确定一个包含你直接实例化的对象的变量和一个包含对该对象的隐式引用的变量之间的区别,嗯,你不能。在C#中,这些是相同的。这两个变量都不包含该对象,而是指向该对象的指针(即对该对象位置的间接引用)。

当然,不要担心没有办法区分两者,因为它们具有完全相同的功能。如果修改任何一个指向的对象,则还要修改另一个指向的对象。

答案 1 :(得分:1)

firstsecond都是StringBuilder类型的引用。它们碰巧是对同一个对象的引用;换句话说,它们具有相同的对象标识strString类型的引用。

答案 2 :(得分:0)

在这里,我想坦率地说,我正面临C Sharp的编程测试。我对PHP,C / C ++,Perl更熟悉,但在C sharp中非常新,但我喜欢了解它。这是他们的考试。我已经填写了一些函数,只剩下2或3,那些是

/// 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");
        }
    }
}