假设对象A有6个字段,现在对象A被序列化,经过一段时间后,再添加3个字段并对对象进行反序列化.
class Person
{
public string name;
public void SetName(string name)
{
name = name;
}
}
public void static Main(string[] args)
{
Person aPerson = new Person();
aPerson.SetName("ruby");
Console.WriteLine("person name is {0}}, aPerson.name);
}
Run Code Online (Sandbox Code Playgroud)
这个人的名字是空的.这可以消除使用this.name = name;
人名是空的(空字符串)是什么原因?
默认情况下,类中每个成员的类型修饰符都是私有的,即使Main()函数类型修饰符是私有的.CLR如何调用外部世界不可见的主要方法?
这可能是一个基本问题,但CIL的含义是什么?
我知道CIL是一种平台和CPU无关的中间语言,但是有人会解释一些有关这方面的细节以及CPU不可知的含义.
我写了一个示例,
当我创建一个如下所示的委托实例时
AddFunctions d1 += new AddFunctions(Function1);
Run Code Online (Sandbox Code Playgroud)
我收到了编译错误,因此删除了+ =并以这种方式创建
AddFunctions d1 = new AddFunctions(Function1);
Run Code Online (Sandbox Code Playgroud)
我只是很想知道,为什么在创建单个委托实例时不允许多播(+ =)?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Delegates
{
public delegate void AddFunctions();
class Program
{
static void Main(string[] args)
{
AddFunctions d1 = new AddFunctions(Function1);
d1 -= d1;
d1();
}
static void Function1()
{
}
static void Function2()
{
}
}
}
Run Code Online (Sandbox Code Playgroud) 在文件中写下person的内容
名字:Abcd
SecondName:Tsfsdfs
为此,我写了一个示例应用程序
namespace BinaryStream
{
class Program
{
static void Main(string[] args)
{
StreamWriter binWriter = new StreamWriter(@"c:\happybirthday.txt");
string name = "Sachin";
string secondname = "tendulkar";
string wishes = "happy birthday";
string firstname = string.Format("FirstName: {0} \n", name);
binWriter.Write(firstname);
string sn = string.Format("SecondName: {0} \n", secondname);
binWriter.Write(sn);
binWriter.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我打开记事本中的内容,字符串不会显示在下一行中这是什么原因?格式部分是否正确?
可能重复:
将System.Array转换为List
我想将aPersonDataArr转换为list IList<aPersonDataArr> aPerList = ???和viceVersa - 从列表到数组.如何做到这一点,我不愿意使用LINQ.还有其他办法吗?
class PersonData
{
public string PersonName;
public int Age;
}
class Program
{
static void Main(string[] args)
{
PersonData[] aPersonDataArr = new PersonData[2];
for (int i = 0; i < aPersonDataArr.Length; i++)
{
aPersonDataArr[i].PersonName = "abcd";
aPersonDataArr[i].Age = 10;
}
}
}
Run Code Online (Sandbox Code Playgroud) 如果委托指向5个方法,则在调用委托时,在第一个方法中发生重复.因为异常发生,所以无法调用4个函数中的其余函数.如何让代理人在异常幸福的情况下调用其他函数
class CloneExample : ICloneable
{
private int m_data ;
private double m_data2 ;
public object Clone()
{
return this.MemberwiseClone();
}
public CloneExample()
{
}
public CloneExample(int data1, int data2)
{
m_data = data1;
m_data2 = data2;
}
public override string ToString()
{
return String.Format("[d1,d2] {0} {1}", m_data, m_data2);
}
}
class Program
{
static void Main(string[] args)
{
CloneExample aEx = new CloneExample(10,20);
CloneExample aEx2 = (CloneExample)aEx.Clone();
Console.WriteLine("the first object value is {0}", aEx.ToString());
Console.WriteLine("the first object value is {0}", …Run Code Online (Sandbox Code Playgroud) 我必须用这种格式写出内容
somename1 value1 value2 value1 value2 ....
somename2 value1 value2 value1 value2 ....
somename3 value1 value2 value1 value2 ....
somename4 value1 value2 value1 value2 ....
somename5 value1 value2 value1 value2 ....
Run Code Online (Sandbox Code Playgroud)
value1和value2是一个点的x和y坐标的一部分,所以为此我创建了一个类:
class Points
{
string name;
double[] X;
double[] Y;
}
class PointsCollection
{
List<Points> aPoints
}
//now when accessing the PointCollection
foreach(Points aPoints in PointsCollection)
{
stream.Write(aPoints.Name)
//now i have to write the value1 and valud2
}
possible code
//now when accessing the PointCollection
foreach(Points aPoints in PointsCollection) …Run Code Online (Sandbox Code Playgroud)