C#中string作为引用类型与类的区别的一个问题
RT。先贴代码
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Value_Ref_test1
{
class Program
{
static void Main(string[] args)
{
point a = new point (10,10) ;
point b = a;
a.x = 20;
Console.WriteLine(a.x);
Console.WriteLine(b.x);
}
}
public class point
{
public int x;
public int y;
public point(int Xvalue,int Yvalue)
{
this.x = Xvalue;
this.y = Yvalue;
}
}
}
这样的话最后会输出
20
20
因为a,b两个变量引向了同一个堆内存上的对象。
但是同样string也是引用类型。
C# code
string a ="123";
string b = a;
b = "321";
再输出的话就是b,a就是321,123。
考虑可能是由于string在编译时被内置,于是又试了一下非内置的string(感觉这样写string不会被内置。)
C# code
Random a = new Random();
string b = a.Next().ToString();
string c = b;
Console.WriteLine("b={0}",b);
Console.WriteLine("c={0}", c);
c = a.Next().ToString();
Console.WriteLine("b={0}", b);
Console.WriteLine("c={0}", c);
OK,最后结果b,c还是输出的不同结果。。。
等待懂得人解答为什么。。。
相关文档:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("/*\n------输出结果------------");
getSplit("ABCDEFG"); ......
在sql语句中涉及到时间类型时 若只想要日期 to_date('2007-7-8','yyyy-mm-dd')
在C#中有datetime类型,代码说明一切
DateTime dt = System.DateTime.Now;
string lsh;
lsh=string.Format("{0:yyyyMMddHHmmss}", dt);
DateTime dt = DateTime.Now;
Label1.Text = dt.To ......
<Language from="SQL" To="C#">
<Type from="bigint" To="long" />
<Type from="binary" To="object" />
<Type from="bit" To="bool" />
<Type from="char" To="string" />
<Type from="datetime" To="DateTime" ......
public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAcces ......
/// <summary>
/// 支持XML序列化的泛型 Dictionary
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("SerializableDictionary")]
public class SerializableDictionary<TKey, TValue& ......