为什么我不能做以下事情?
public interface ICommunication
{
int Send(Dictionary<string, string> d);
int Send(byte[] b);
Dictionary<string, string> Receive();
byte[] Receive(); // Error
}
Run Code Online (Sandbox Code Playgroud)
符号Receive()
是不同的,但参数是相同的.为什么编译器会查看参数而不是成员签名?
ICommunication'已经定义了一个名为'Receive'的成员,它具有相同的参数类型.
我怎么能绕过这个?
我可以重命名Receive()
如下,但我更喜欢保持命名Receive()
.
public interface ICommunication
{
int Send(Dictionary<string, string> d);
int Send(byte[] b);
Dictionary<string, string> ReceiveDictionary();
byte[] ReceiveBytes();
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样生成的json文件
[
{
"test1": "Pirates",
"test2": "Hello World"
},
{
"test1": "Pirates",
"test2": "Hello World"
}
]
Run Code Online (Sandbox Code Playgroud)
从我遇到的一个问题中,我使用了这个链接Json 2 Cshartp Object并查看了应该制作的类.但是......我认为它应该是一个阵列,但它没有名字?所以我尝试制作这样的物体
public class pirateships
{
public string test1 { get; set; }
public string test2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后DeserializeObject<T>()
使用
pirateships coords = JsonConvert.DeserializeObject<pirateships>(reader.ReadToEnd());
Run Code Online (Sandbox Code Playgroud)
但它仍然说它无法正确反序列化.我试过让它成为一系列盗版但仍然失败了.非常感谢您提供协助.
你为什么要申报IEnumerable<T>
readonly
?
从这篇关于异步和等待的文章中我们得到以下代码.
class OrderHandler
{
private readonly IEnumerable<Order> _orders;
public OrderHandler()
{
// Set orders.
}
public IEnumerable<Order> GetAllOrders()
{
return _orders;
}
}
Run Code Online (Sandbox Code Playgroud)
IEnumerable<T>
是不可改变的.这与readonly
关键字有何不同?
如何在C#中将枚举值显示为V1.0,V2.0,V3.0?
enum value
{
V1.0,
V2.0,
V3.0
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从 url 下载 xls 文件:http : //www.site.com/ff/excel/file.aspx? deven =0
我正在使用此代码,但下载完成后,文件未正确下载。如何正确下载此文件?
string remoteFilename="http://www.site.com/ff/excel/file.aspx?deven=0";
string localFilename = "D:\\1\\1.xls";
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
response.ContentType = "application/vnd.ms-excel";
if (response != null)
{
// Once the WebResponse object has …
Run Code Online (Sandbox Code Playgroud) 我正在经历这种行为.考虑这段代码
classQtData temp_data = new classData(); //consider this...
public int AddData(ref classSerialPort serial_com )
{
int return_number_of_packet_read;
int index_a;
return_number_of_packet_read = 0;
while (serial_com.GetRawData(ref raw_vector) > 0)
{
//assign temp__data stuffs....
temp_data.rolling_counter = (uint)raw_vector[40];
this.Enqueue(temp_data);
return_number_of_packet_read++;
}
return return_number_of_packet_read;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果while循环执行(比如说)3次,则同一个temp_data
对象(最后插入的)将被排队三次,而不是插入3个不同的对象.
否则,此代码片段按预期工作,将正确的元素排入队列:
public int AddData(ref classSerialPort serial_com )
{
int return_number_of_packet_read;
int index_a;
return_number_of_packet_read = 0;
while (serial_com.GetRawData(ref raw_vector) > 0)
{
classQtData temp_data = new classData();
//assign temp__data stuffs....
temp_data.rolling_counter = (uint)raw_vector[40];
this.Enqueue(temp_data);
return_number_of_packet_read++;
}
return return_number_of_packet_read; …
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,输出是;
厄尼伯特埃尔莫
为什么最后的输出是Elmo?不应该是厄尼吗?因为我实例化dog.Creature
对象new Cat();
.我认为,Name
财产Cat
类覆盖Name
财产Creature
类.
class Class1
{
public static void Main(string[] args)
{
var dog = new Dog();
var cat = new Cat();
dog.Creature = new Cat();
Console.WriteLine(cat.Name); //outputs Ernie
Console.WriteLine(dog.Name); //outputs Bert
Console.WriteLine(dog.Creature.Name); //outputs Elmo, why not Ernie?
Console.Read();
}
}
public class Animal<T> where T : Creature
{
public T Creature { get; set; }
private string _name = "Oscar";
public string Name { get { …
Run Code Online (Sandbox Code Playgroud) 我想为文本创建一个Rectangle.但我不知道字符串的大小.现在我得到如下大小(对于带大写的字符串无效):
var textSize = e.Graphics.MeasureString(text, e.Appearance.Font);
Run Code Online (Sandbox Code Playgroud)
但是如果我的字符串是大写的,这个函数将返回错误的值.
如果字符串是大写的,如何获取字符串的大小?
我想转换List<Object>
到List<U>
这里为U
的是可以动态生成的类型,但列表不接受U
在C#
我正在尝试将输入文件读入数组.我的文件看起来像:
00000*5071177*M010165767HAZZ JONES FAKE B M12/16/196901/06/2014000000036209 00000*5071178*M0201657677315 FAKE ST MOON TX56464 485942934 MAINTENCE
当第一行的第一个单词中的模式与第二行的第二个块中的模式匹配时,我希望将由空格分解的整行划分为数组或对象.
所以我的数组将包含类似 [5071177] - >数组([琼斯,假,B,M12/16/196901,假ST,月亮等]); 什么是实现这一目标的最佳方法?
StreamReader sR = new StreamReader("Desktop/records2.txt");
StreamWriter sW = new StreamWriter("Desktop/new.txt");
while (sR.Peek() != -1) // stops when it reachs the end of the file
{
string line = sR.ReadLine();
// var myArray = line.Split('\n');
string[] myarray = line.Split(' ');
// "line" EDITING GOES HERE
sW.WriteLine(myarray); /
}
Run Code Online (Sandbox Code Playgroud)