小编Sam*_*ach的帖子

与具有相同名称和不同返回类型的成员的接口

为什么我不能做以下事情?

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)

.net c# interface method-signature

2
推荐指数
1
解决办法
1085
查看次数

使用C#5来电信息的解决方案示例

我正在努力想到C#5的来电信息功能的真实使用

在什么情况下你需要知道谁调用你的方法?除了跟踪和可能的调试之外还有什么其他用法?

它是面向方面编程的实现吗?

c# aop c#-5.0 .net-4.5

2
推荐指数
1
解决办法
333
查看次数

将json映射到C#对象

我有一个像这样生成的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)

但它仍然说它无法正确反序列化.我试过让它成为一系列盗版但仍然失败了.非常感谢您提供协助.

c# parsing json deserialization

2
推荐指数
1
解决办法
174
查看次数

为什么使用readonly IEnumerable <T>

你为什么要申报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# ienumerable readonly immutability mutability

1
推荐指数
1
解决办法
925
查看次数

如何用一组特殊字符显示枚举为V1.0,V2.0和V3.0

如何在C#中将枚举值显示为V1.0,V2.0,V3.0?

enum value
{
    V1.0,
    V2.0,
    V3.0
}
Run Code Online (Sandbox Code Playgroud)

c# enums

1
推荐指数
1
解决办法
77
查看次数

从 url 下载 .xls 文件

我正在尝试从 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)

c# excel httpwebrequest httpwebresponse

1
推荐指数
1
解决办法
9594
查看次数

C#this.Enqueue()使用"temp"对象数据进行排队时的奇怪行为

我正在经历这种行为.考虑这段代码

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)

.net c# oop

1
推荐指数
1
解决办法
67
查看次数

在c#中实例化泛型类

在下面的代码中,输出是;

厄尼伯特埃尔莫

为什么最后的输出是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)

.net c# generics class visual-studio-2010

0
推荐指数
1
解决办法
264
查看次数

我如何获得文字大小?

我想为文本创建一个Rectangle.但我不知道字符串的大小.现在我得到如下大小(对于带大写的字符串无效):

var textSize = e.Graphics.MeasureString(text, e.Appearance.Font);
Run Code Online (Sandbox Code Playgroud)

但是如果我的字符串是大写的,这个函数将返回错误的值.

如果字符串是大写的,如何获取字符串的大小?

.net c# string graphics

0
推荐指数
1
解决办法
200
查看次数

将List <object>转换为List <U>

我想转换List<Object>List<U>这里为U的是可以动态生成的类型,但列表不接受U在C#

c#

-1
推荐指数
1
解决办法
79
查看次数

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)

c# arrays object

-2
推荐指数
1
解决办法
255
查看次数