[英]How can I determine if a variable is of Type Reference in C#?
StringBuilder first = new StringBuilder();
StringBuilder second = first;
String str = "Love";
Is there a way to check if the variable "second" is Type Reference, whereas the variable "str" is of Type Value? 有没有一种方法可以检查变量“ second”是否为类型引用,而变量“ str”是否为类型值? I have been Googling around still can't get it, very new in C# here.
我一直在谷歌搜索仍然无法获得它,这里的C#非常新。 I know there's
second.getType()
but that does not let me know if second is of Type Reference. 我知道有
second.getType()
但是那不让我知道second是否是类型引用。
Thanks a lot. 非常感谢。
Additional info 附加信息
Here I wanna be frank here, I'm facing a programming test for C Sharp, of course it's an open book test because I'm not in an enclosed or restricted class :-) . 在这里我想坦率地说,我正在面对C Sharp的编程测试,当然这是一个开放式测试,因为我不在封闭或受限类中:-)。 I'm more familiar with PHP, C/C++, Perl, but very new in C sharp, but I love to learn about it.
我对PHP,C / C ++,Perl更为熟悉,但是在C Sharp中非常新,但是我喜欢学习它。 Here is their test.
这是他们的测试。 I have already filled out a few functions, only left out 2 or 3, those are ref and unref.
我已经填写了一些功能,仅遗漏了2或3个,即ref和unref。 If you see the code below I need to print out the Reference type in between < > in PrintSortedData function.
如果您看到下面的代码,则需要在PrintSortedData函数的<>之间打印引用类型。 The test question is at the code's comment.
测试问题在代码的注释处。 Maybe I haven't get the programming logic right yet.
也许我还没有掌握正确的编程逻辑。
/// 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");
}
}
}
Both variables are reference types. 这两个变量都是引用类型。 An instance of the object
StringBuilder
is still a reference type, as is an object of type string
. 对象
StringBuilder
的实例仍然是引用类型, string
类型的对象也是如此。
Value types in C# are either user-defined structs
, enumerations, and numeric types (such as int
, float
, double
, decimal
, etc.) See the documentation on Value Types for more information. C#中的值类型是用户定义的
structs
,枚举和数字类型(例如int
, float
, double
, decimal
等)。有关更多信息,请参见“ 值类型 ”文档。
Reference types are everything else, including classes, interfaces, delegates, and even some built-in types, like string
and object
. 引用类型是所有其他内容,包括类,接口,委托,甚至一些内置类型,例如
string
和object
。 See the documentation on Reference Types for surprisingly not much more detail. 令人惊讶的是,请参阅参考类型上的文档以获取更多细节。
As far as your implicit question, how can you determine the difference between a variable that holds an object you've directly instantiated, and a variable that holds an implicit reference to that object, well, you can't. 就隐式问题而言,如何确定持有直接实例化的对象的变量与持有对该对象的隐式引用的变量之间的区别,您不能。 In C#, these are the same thing.
在C#中,这些是同一回事。 Neither variable holds that object, but rather a pointer to that object (that is, an indirect reference to that object's location).
这两个变量都不保存该对象,而是保存该对象的指针(即对该对象位置的间接引用)。
Of course, not to worry that there's no way to differentiate between the two, because they have exactly the same functionality. 当然,不必担心无法区分两者,因为它们具有完全相同的功能。 If you modify the object pointed to by either one, you also modify the object pointed to by the other.
如果您修改了任何一个指向的对象,那么您还修改了另一个指向的对象。
first
and second
are both references of type StringBuilder
. first
和second
都是StringBuilder
类型的引用。 They happen to be references to the same object; 它们恰好是对同一个对象的引用。 in other words, they have the same object identity .
换句话说,它们具有相同的对象标识 。
str
is a reference of type String
. str
是String
类型的引用。
Here I wanna be frank here, I'm facing a programming test for C Sharp. 在这里,我想坦率地说,我正在面对C Sharp的编程测试。 I'm more familiar with PHP, C/C++, Perl, but very new in C sharp, but I love to learn about it.
我对PHP,C / C ++,Perl更为熟悉,但是在C Sharp中非常新,但是我喜欢学习它。 Here is their test.
这是他们的测试。 I have already filled out a few functions, only left out 2 or 3, those are
我已经填写了一些功能,仅遗漏了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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.