小编Ben*_*Ben的帖子

一个具有多个IEnumerable <T>接口的类 - 如何处理非泛型方法?

我想我理解为什么IEnumerable<T>继承IEnumerable之后,阅读帖子后: 为什么IEnumerable<T>从IEnumerable继承?

但是,我不确定在应用2个通用接口时如何最好地实现非泛型方法?这是我正在编写的代码示例:

public interface IComponentA { /* ... Interface A Code ... */ }

public interface IComponentB { /* ... Interface B Code ... */ }

public class ComponentModel: IEnumerable<IComponentA>, IEnumerable<IComponentB>
{
    public ComponentModel() { }

    private List<IComponentA> ListOfComponentA = new List<IComponentA>();
    private List<IComponentB> ListOfComponentB = new List<IComponentB>();

    // ... Some public methods to add and remove components (for A and B).

    IEnumerator<IComponentA> IEnumerable<IComponentA>.GetEnumerator()
    {
        return ListOfComponentA.GetEnumerator();
    }

    IEnumerator<IComponentB> IEnumerable<IComponentB>.GetEnumerator()
    {
        return ListOfComponentB.GetEnumerator(); …
Run Code Online (Sandbox Code Playgroud)

.net c#

16
推荐指数
3
解决办法
6896
查看次数

当下一个字符是Prime符号(char)697时,String.StartsWith not working

我正在尝试使用带有Prime符号的字符串,但我遇到了String.StartsWith方法的一些问题.为什么以下代码抛出异常?

string text_1 = @"123456";
string text_2 = @"?ABCDEF";

string fullText = text_1 + text_2;
if (!fullText.StartsWith(text_1))
{
    throw new Exception("Unexplained bad error.");
}
Run Code Online (Sandbox Code Playgroud)

我怀疑这个问题是因为这个Prime符号(char)697被视为重音,因此正在改变它之前的字母.(我不认为它应该是 - 它应该是主要符号,所以不应该改变它前面的数字).我不确定如何测试这个.我确实尝试了这个答案中提出的方法,但它返回false:

IsLetterWithDiacritics(text_1[5]) //  == False
IsLetterWithDiacritics(fullText[5]) // == False
IsLetterWithDiacritics(fullText[6]) // == False
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

c# string escaping startswith diacritics

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

如何在运行时设置ChartArea的BackImage?

任何人都知道为什么ChartArea.BackImage是string类型的属性?对于这种类型的Image属性来说,它是否更有意义?

或者换句话说,如何将ChartArea的背景图像设置为在运行时生成的图像(例如,从GraphicPath对象)?

欢迎提出建议,谢谢.

c# asp.net charts mschart

5
推荐指数
2
解决办法
2945
查看次数

我什么时候应该使用延迟加载以及它应该有多懒惰

我想问一下延迟加载。我经常读到我们应该拒绝它,但我为什么要加载可能从未使用过的数据?

作为讨论的一个例子,让我们使用一个Customer

-Id
-Title
-FormOfAddress
-FirstName
-LastName
-Picture*
-DOB
-Phone
-Mobile
-Address*/Billing Address*
    -Id
    -Street
    -Number
    -Country*
        -Id
        -Name
        -Zipcode
-Bankdetails*
    -Id
    -AccountHolder
    -AccountNumber
    -Bank*
        -Id
        -Name
        -BankCode
    -IBAN
Run Code Online (Sandbox Code Playgroud)

*根据座右铭“仅加载您需要的内容” ,标记我将延迟加载的对象。

编辑

好吧,我的问题似乎还不够清楚,所以这里可能是我想知道的更好的表述:

我想知道为什么大多数人都劝阻懒惰加载,是因为他们不能使用它还是它有非常糟糕的缺点?

c# lazy-loading mvvm

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

以 XML 格式保存 ImageSource (BitmapSource)

我正在尝试在XML 文件中保存和加载ImageSource(或BitmapSource)。快速浏览一下 SO 给了我这个答案

看起来不错,所以我试了一下,但我得到了一个奇怪的结果。

当我尝试此代码时,一切正常:

BitmapSource testImgSrc = new WriteableBitmap(new BitmapImage(new Uri("pack://application:,,,/MyNameSpace;component/Images/MyImg.png")));
BackgroundImage = testImgSrc;
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这段代码时,图像根本没有出现:

BitmapSource testImgSrc = new WriteableBitmap(new BitmapImage(new Uri("pack://application:,,,/MyNameSpace;component/Images/MyImg.png")));
string testImgStr = ImageToBase64(testImgSrc);
BitmapSource testImg = Base64ToImage(testImgStr);
BackgroundImage = testImg;
Run Code Online (Sandbox Code Playgroud)

似乎没有任何错误或异常。当逐步执行代码时,BackgroundImage它看起来像是被设置为一个有效的图像对象。

我的 WPF 表单有一个图像控件,它的源代码绑定到一个返回属性结果的BackgroundImage属性。我猜绑定工作正常,因为第一个测试按预期工作。

谁能帮我理解为什么第二个测试没有显示我的图像?

c# wpf image xml-serialization

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

为什么GetHashCode应该实现与Equals相同的逻辑?

这个MSDN页面中,它说:

警告:

如果重写GetHashCode方法,则还应该重写Equals,反之亦然.如果在对两个对象进行相等性测试时,重写的Equals方法返回true,则重写的GetHashCode方法必须为两个对象返回相同的值.

我也看到了许多类似的建议,我可以理解,当重写Equals方法时,我也想要覆盖GetHashCode.据我所知,GetHashCode与哈希表查找一起使用,这与等式检查不同.

这是一个帮助解释我想问的例子:

public class Temperature /* Immutable */
{
    public Temperature(double value, TemperatureUnit unit) { ... }

    private double Value { get; set; }
    private TemperatureUnit Unit { get; set; }

    private double GetValue(TemperatureUnit unit)
    {
        /* return value converted into the specified unit */
    }

    ...

    public override bool Equals(object obj)
    {
        Temperature other = obj as Temperature;
        if (other == null) { return false; }
        return (Value == other.GetValue(Unit));
    }

    public …
Run Code Online (Sandbox Code Playgroud)

c# equality gethashcode

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