小编Kja*_*ara的帖子

隐式内部接口实现

当我有一个公共界面

public interface IPub { string Foo { get; set; } }
Run Code Online (Sandbox Code Playgroud)

然后我可以通过显式实现这个接口:

public class CFoo : IPub { string IPub.Foo { get; set; } }
Run Code Online (Sandbox Code Playgroud)

或者通过使用public修饰符隐式地:

public class CFoo : IPub { public string Foo { get; set; } }
Run Code Online (Sandbox Code Playgroud)

.有道理:修饰符必须是public因为接口是public.

但是当我有一个内部接口

internal interface IInt { string Bar { get; set; } }
Run Code Online (Sandbox Code Playgroud)

然后我只能明确地实现它:

public class CBar : IInt { string IInt.Bar { get; set; } }
Run Code Online (Sandbox Code Playgroud)

或隐式使用public修饰符:

public class CBar …
Run Code Online (Sandbox Code Playgroud)

c# interface implicit-conversion

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

C#类型转换:显式转换存在但引发转换错误?

我了解到HashSet实现了IEnumerable接口.因此,可以隐式地将HashSet对象转换为IEnumerable:

HashSet<T> foo = new HashSet<T>();
IEnumerable<T> foo2 = foo; // Implicit cast, everything fine.
Run Code Online (Sandbox Code Playgroud)

这也适用于嵌套泛型类型:

HashSet<HashSet<T>> dong = new HashSet<HashSet<T>>();
IEnumerable<IEnumerable<T>> dong2 = dong; // Implicit cast, everything fine.
Run Code Online (Sandbox Code Playgroud)

至少那是我的想法.但如果我做了Dictionary,我遇到了一个问题:

IDictionary<T, HashSet<T>> bar = new Dictionary<T, HashSet<T>>();
IDictionary<T, IEnumerable<T>> bar2 = bar; // compile error
Run Code Online (Sandbox Code Playgroud)

最后一行给出了以下编译错误(Visual Studio 2015):

无法隐式转换类型

System.Collections.Generic.IDictionary<T, System.Collections.Generic.HashSet<T>>System.Collections.Generic.IDictionary<T, System.Collections.Generic.IEnumerable<T>>.

存在显式转换(您是否错过了演员?)

但如果我通过写作进行演员表演

IDictionary<T, IEnumerable<T>> bar2 = (IDictionary<T, IEnumerable<T>>) bar;
Run Code Online (Sandbox Code Playgroud)

然后我在运行时获得了无效的强制转换异常.

两个问题:

  • 我该如何解决这个问题?是迭代密钥并逐步建立新字典的唯一方法吗?
  • 为什么我首先会遇到这个问题,即使HashSet …

c# ienumerable dictionary type-conversion hashset

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

C# 定义函数并以部分应用程序作为委托

考虑以下方法:

int Foo(string st, float x, int j)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

Func<float, int>现在我想通过提供参数st和 的值将其包装在类型的委托中j。但我不知道语法。有人可以帮忙吗?

这是这个想法(可能看起来有点像 Haskell):

Func<float, int> myDelegate = new Func<float, int>(Foo("myString", _ , 42));
// by providing values for st and j, only x is left as a parameter and return value is int
Run Code Online (Sandbox Code Playgroud)

c# delegates partial-application

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

格式DateTime,中间插入文本

我想以一种特定的方式格式化我的DateTime对象。这是基础:

DateTime dt = new DateTime(2018, 11, 20, 15, 30, 5, 0);
Debug.WriteLine(dt.ToString("ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture));
Run Code Online (Sandbox Code Playgroud)

输出将是Tue Nov 20 15:30:05 +01:00 2018。这几乎是我想要的:而不是+01:00我想要的string变量值myText

因此,如果myText具有value "hello world",则输出应为:

Tue Nov 20 15:30:05 hello world 2018

所以我的问题很简单:我需要写什么才能获得格式设置zzz部分除外),但中间要有变量的值string

我试过了dt.ToString("ddd MMM dd HH:mm:ss " + myText + " yyyy", CultureInfo.InvariantCulture),但是myText = "hello world"会输出

Tue Nov 20 15:30:05 3ello …

c# datetime string-concatenation string-formatting

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

Java 中单个元素转换为 Iterable

在 C# 中,我可以将单个元素转换为IEnumerable<>如下所示(此处使用扩展方法):

static class Extensions
{
    static IEnumerable<T> Yield<T>(this T t)
    {
        yield return t;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我需要在IEnumerable<>某处输入但只有一个元素时,我会使用它,如下所示:

var myList = new List<string>("foo".Yield());
Run Code Online (Sandbox Code Playgroud)

在 Java 中是否有一些等效的方法可以做到这一点?

Iterable<>我对从像这样的元素实际创建堆上不感兴趣

List<String> myIterable = new ArrayList<String>();
myIterable.add("foo");
Run Code Online (Sandbox Code Playgroud)

因为我已经知道那种解决方案;我想知道 Java 是否可以像 C# 一样强大且惰性地处理可枚举/可迭代(C# 有 Linq 和 Yield Return,Java 有 Streams)。

c# java iterable enumerable

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