小编use*_*035的帖子

什么是Java 8 java.util.function.Consumer <>的c#等价物?

在C#中是否有相当于这个接口?

例:

Consumer<Byte> consumer = new Consumer<>();
consumer.accept(data[11]);
Run Code Online (Sandbox Code Playgroud)

我四处搜寻Func<>,Action<>但我不知道.

Consumer.accept()接口的原始Java代码非常简单.但不适合我:

void accept(T t);

/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation.  If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to …
Run Code Online (Sandbox Code Playgroud)

c# java consumer

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

C#条件使用和默认参数

假设我们有一个基于某些文件,连接或其他资源的操作。

我们需要一种可以与提供的资源一起运行的方法,或者-如果未提供,则创建自己的方法:

string Foo(Resource parameter = null)
{
    if (parameter == null)
    {
        using (var res = new Resource)
        {
            res.Something();
               /*......*/
               /*......*/
               /*......*/
            return /*..........*/
        }
    }
    else 
    {
            parameter.Something();
               /*......*/
               /*......*/
               /*......*/
            return /*..........*/
    }
 }
Run Code Online (Sandbox Code Playgroud)

}

它有效,但这对我来说真的很丑。是否可以用更紧凑,更“漂亮”的方式编写它?

我不能使用:

string Foo(Resource parameter = null)
{
    using (var res = parameter ?? new Resource())
    {
        res.Something();
           /*......*/
           /*......*/
           /*......*/
        return /*..........*/
    }
}
Run Code Online (Sandbox Code Playgroud)

因为如果作为参数传递它会处理我的原始资源。

PS。这个问题的正确标签是什么?#coding-style标记为“请勿使用”。

c#

6
推荐指数
2
解决办法
241
查看次数

如何在Linux中使用.net6创建命名管道(mkfifo)?

我想在 Linux 中使用 .net6 创建一个命名管道(“mkfifo”)。

使用NamedPipeServerStream类对我没有帮助,因为它创建一个套接字文件而不是管道。

这将创建一个套接字:

var notAPipeButsocket = new NamedPipeServerStream("/tmp/my.notpipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
        notAPipeButsocket.WaitForConnection();
Run Code Online (Sandbox Code Playgroud)

看来微软选择在 Linux 中使用套接字显式实现“NamedPipeServerStream”:Why not use Unix Domain Sockets for Named Pipes?

如何在Linux中使用.net6创建真正的命名管道文件并写入其中?

上下文:我想用 WireShark 打开管道。

c# pipe .net-core .net-6.0

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

加载属性延迟加载

我有一个属性,getter应该只在第一次加载它的值.第二次返回加载的值而不再加载它:

private Object _MemberValue;

public Object MemberValue
{
    get
    {
        if(_MemberValue == null)
        {
            _MemberValue = LoadMember();
        }

        return _MemberValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

在VB.NET中有Static关键字.有了它,您不必声明一个类宽成员.

Public Property MemberValue as Object
    Get
        Static value as Object = Nothing

        If (value is Nothing) Then
            value = LoadMember()
        End If

        Return value
    End Get
End Property
Run Code Online (Sandbox Code Playgroud)

在C#中没有这样的关键字.

是否有更好的C#实现此问题或其他模式?

c# vb.net design-patterns lazy-evaluation

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

为什么在这种情况下类型推断不起作用?

假设我有一个POCO和一个List类:

class MyClass
{...}

class MyClasses : List<MyClass> 
{...}
Run Code Online (Sandbox Code Playgroud)

和以下方法将映射IEnumerable<MyClass>MyClasses列表:

public static TListType ToListOfType<TListType, TItemType>(this IEnumerable<TItemType> list) where TListType : IList<TItemType>, new()
{
    var ret = new TListType();

    foreach (var item in list)
    {
        ret.Add(item);
    }

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

我希望此代码可以编译,但不会:

var list = someListOfMyClass.ToListOfType<MyClasses>();
Run Code Online (Sandbox Code Playgroud)

但是我得到了

错误CS1061'IEnumerable'不包含'ToListOfType'的定义,并且找不到可访问的扩展方法'ToListOfType'接受类型为'IEnumerable'的第一个参数(您是否缺少using指令或程序集引用?)

但是,这确实有效:

var list = someListOfMyClass.ToListOfType<MyClasses, MyClass>();
Run Code Online (Sandbox Code Playgroud)

我不明白为什么类型推断不足以使编译器知道项目类型是什么,因为this变量是已知类型的列表。

c#

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

DataGrid - 更改编辑行为

我有一个带有一些复杂行为的ChildViewModels的ObservableCollection.

当我去编辑一行时 - DataGrid进入'编辑模式' - 这有效地禁用了当前单元格之外的UI通知,直到提交了行 - 这是预期的行为,更重要的是它可以被更改吗?

例:

public class ViewModel
{
    public ViewModel()
    {
        Childs = new ObservableCollection<ChildViewModel> {new ChildViewModel()};
    }
    public ObservableCollection<ChildViewModel> Childs { get; private set; }
}
public class ChildViewModel : INotifyPropertyChanged
{
    private string _firstProperty;
    public string FirstProperty
    {
        get { return _firstProperty; }
        set
        {
            _firstProperty = value;
            _secondProperty = value;
            OnPropetyChanged("FirstProperty");
            OnPropetyChanged("SecondProperty");
        }
    }

    private string _secondProperty;
    public string SecondProperty
    {
        get { return _secondProperty; }
        set
        {
            _secondProperty = value;
            OnPropetyChanged("SecondProperty"); …
Run Code Online (Sandbox Code Playgroud)

c# wpf datagrid mvvm

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

Object.Equals()“最终”是如何实现的?

出于好奇,我想看看该Object.Equals()方法是如何实现的。

因此,我浏览了Object 的源代码,最终找到了RuntimeHelpers.Equals()。但是 RuntimeHelpers 的 Equals 方法是在外部实现的,没有 Dllimport 属性。

我在哪里可以找到 的“外部”实现RuntimeHelpers.Equals()

c# .net-core

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