我是C#的新手,只是有关默认构造函数和自动属性的问题。基于一个关于stackoverflow的问题: 如何在struct构造函数中设置auto属性支持字段的值?
所以我们有以下结构
public struct SomeStruct
{
public SomeStruct(String stringProperty, Int32 intProperty)
{
this.StringProperty = stringProperty;
this.IntProperty = intProperty;
}
public String StringProperty { get; set; }
public Int32 IntProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是如何在不调用默认构造函数by的情况下对我进行编译:this()呢?另一个问题是为什么同一规则不适用于类?没有默认构造函数的情况下可以拥有自动属性吗?
我是C#的初学者,只是有关泛型方法的问题。例如:
public static IQueryable<TResult> Where<TSource>(...)
Run Code Online (Sandbox Code Playgroud)
因此,我们仅将此LINQ方法称为:
var test = _context.Recipes.Where(r => !r.IsDeleted)
Run Code Online (Sandbox Code Playgroud)
代替
var test = _context.Recipes.Where<Recipe>(r => !r.IsDeleted)
Run Code Online (Sandbox Code Playgroud)
所以为什么我们不只是将通用方法声明为
public static IQueryable<TResult> Where(...)
Run Code Online (Sandbox Code Playgroud) 我是C#的新手,只是一个关于多级继承的问题假设我们有以下类:
Class Employee
{
public virtual void CalculateBonus() {
...
}
}
class SalesPerson : Employee
{
public override void CalculateBonus() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
假设我们还有另一个派生自SalesPerson的类
class PTSalesPerson : SalesPerson
{
public override void CalculateBonus() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题很简单,基类中的virtual关键字需要被其子类覆盖。应用相同的逻辑,virtual关键字也应在SalesPerson类中应用如下:
class SalesPerson : Employee
{
public virtual override void CalculateBonus() {
... //compiler error
}
}
Run Code Online (Sandbox Code Playgroud)
这样PTSalesPerson可以覆盖其父方法。这是否意味着“虚拟”关键字只需要出现在1级基类中?还是覆盖=覆盖+虚拟?