小编ajl*_*ane的帖子

为什么我不能拥有受保护的接口成员?

反对在接口上声明受保护访问成员的论点是什么?例如,这是无效的:

public interface IOrange
{
    public OrangePeel Peel { get; }
    protected OrangePips Seeds { get; }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,接口IOrange将保证实现者至少OrangePips向其继承者提供实例.如果实现者想要,他们可以扩大范围public:

public class NavelOrange : IOrange
{
    public OrangePeel Peel { get { return new OrangePeel(); } }
    protected OrangePips Seeds { get { return null; } }
}

public class ValenciaOrange : IOrange
{
    public OrangePeel Peel { get { return new OrangePeel(); } }
    public OrangePips Seeds { get { return new OrangePips(6); …
Run Code Online (Sandbox Code Playgroud)

c# interface protected access-modifiers

67
推荐指数
6
解决办法
5万
查看次数

为什么我们没有两个空值?

我常常想知道为什么null代表"没有价值"的语言不能区分被动的"我不知道它的价值是什么"和更加自信的"没有价值"..

有几种情况我喜欢区分这两者(特别是在使用用户输入和数据库时).

我想下面,我们两人的名字的状态unknownnull:

var apple;

while (apple is unknown)
{
    askForApple();
}

if (apple is null)
{
    sulk();
}
else
{
    eatApple(apple);
}
Run Code Online (Sandbox Code Playgroud)

显然,我们可以通过手动存储其他地方的状态来避开它,但我们也可以为空值做到这一点.

所以,如果我们能有一个null,为什么我们不能有两个呢?

null programming-languages

31
推荐指数
6
解决办法
2049
查看次数

如何将控件的样式与当前主题相匹配?(WPF)

如果我使用WPF创建自定义控件,如何为与当前应用的主题(Aero,Luna,Classic等)匹配的控件提供样式?

例如,我想在使用Aero时应用以下内容:

<Style>
    <Setter Property="Background" Value="White"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

但是在使用Luna时应用不同的风格:

<Style>
    <Setter Property="Background" Value="#DFDFDF"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式扩展基本主题,以支持我的新控件吗?

wpf themes styles

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

为什么我必须指定所有泛型类型参数?

有没有(技术)原因,C#要求所有泛型类型参数与它们的封闭类名称一起声明?

例如,我想声明这个:

public class FruitCollection<TFruit> : FoodCollection<TFoodGroup>
    where TFruit : IFood<TFoodGroup>
    where TFoodGroup : IFoodGroup { }

public class AppleCollection : FruitCollection<Apple> { }
public class TomatoCollection : FruitCollection<Tomato> { }
Run Code Online (Sandbox Code Playgroud)

TFruit是一个IFood<TFoodGroup>,所以TFoodGroup 必须定义,如果TFruit提供,即使我没有明确声明它.

相反,我必须这样做:

public class FruitCollection<TFoodGroup, TFruit> : FoodCollection<TFoodGroup>
    where TFruit : IFood<TFoodGroup>
    where TFoodGroup : IFoodGroup { }

// Anything other than FruitGroup is an error when combined with Apple
public class AppleCollection : FruitCollection<FruitGroup, Apple> { }

// Anything other …
Run Code Online (Sandbox Code Playgroud)

c# generics

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

如何选择每组的顶部和底部记录?

比如说,我在数据库中有一个表(SQL Server 2008),其数据与此类似(但更多,更大):

| ID | SCORE | GROUP |
-----------------------
| 10 |     1 | A     |
| 6  |     2 | A     |
| 3  |     3 | A     |
|----|-------|-------|
| 8  |     5 | B     |
|----|-------|-------|
| 4  |     1 | C     |
| 9  |     3 | C     |
| 2  |     4 | C     |
| 7  |     4 | C     |
|----|-------|-------|
| 12 |     3 | D     |
| 1  | …
Run Code Online (Sandbox Code Playgroud)

sql sql-server-2008

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