Reputation: 1
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? I have been Googling around still can't get it, very new in C# here. I know there's second.getType()
but that does not let me know if second is of Type Reference.
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 :-) . I'm more familiar with PHP, C/C++, Perl, but very new in C sharp, but I love to learn about it. Here is their test. I have already filled out a few functions, only left out 2 or 3, those are ref and unref. If you see the code below I need to print out the Reference type in between < > in PrintSortedData function. 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");
}
}
}
Upvotes: 0
Views: 2619
Reputation: 1
Here I wanna be frank here, I'm facing a programming test for C Sharp. I'm more familiar with PHP, C/C++, Perl, but very new in C sharp, but I love to learn about it. Here is their test. I have already filled out a few functions, only left out 2 or 3, those are
/// 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");
}
}
}
Upvotes: 0
Reputation: 285077
first
and second
are both references of type 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
.
Upvotes: 1
Reputation: 245012
Both variables are reference types. An instance of the object StringBuilder
is still a reference type, as is an object of type 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.
Reference types are everything else, including classes, interfaces, delegates, and even some built-in types, like string
and 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. 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.
Upvotes: 2