在C#中,类型实例变量的默认值是int?
什么?
例如,在以下代码中,MyNullableInt
如果从未明确分配,将具有什么值?
class MyClass
{
public int? MyNullableInt;
}
Run Code Online (Sandbox Code Playgroud)
(似乎答案几乎肯定是null
或者0
,但是那些是什么?)
ReSharper 建议更改以下代码:
if (MyString is string myString)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
对象模式:
if (MyString is { } myString)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
它说:
源表达式始终是模式类型,匹配所有非空值。
我以前从未见过这种语法,也找不到任何有关它的文档。它是什么,它有什么作用?
我有这Bank
门课:
public class Bank : INotifyPropertyChanged
{
public Bank(Account account1, Account account2)
{
Account1 = account1;
Account2 = account2;
}
public Account Account1 { get; }
public Account Account2 { get; }
public int Total => Account1.Balance + Account2.Balance;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
Bank
取决于其他类,并具有Total
根据这些其他类的属性计算的属性.每当Account.Balance
更改任何这些属性时,PropertyChanged
都会引发Account.Balance
:
public class Account : INotifyPropertyChanged
{
private int …
Run Code Online (Sandbox Code Playgroud) appsettings.json
我的项目中有这个文件,如下所示:
{
"ConnectionStrings": {
"MyConnectionString": "Server=SQLSERVER;Database=MyDatabse;Trusted_Connection=True;"
},
"NLog": {
"targets": {
"database": {
"type": "Database",
"dbProvider": "System.Data.SqlClient",
"connectionString": "Server=SQLSERVER;Database=MyDatabse;Trusted_Connection=True;"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不想在多个地方写我的连接字符串。我可以以某种方式引用以前的连接字符串吗?
我已经尝试过:"connectionString": "${appsetting:name=ConnectionStrings.MyConnectionString}"
,这是行不通的。
如何提高PropertyChanged
对SomeProperty
类B
?
此示例无法编译,因为PropertyChanged
无法以这种方式访问...
public class A : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
public class B : A
{
private object _someProperty;
public object SomeProperty
{
get => _someProperty;
set
{
_someProperty = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SomeProperty)))
}
}
}
Run Code Online (Sandbox Code Playgroud) c# ×5
wpf ×2
.net-core ×1
appsettings ×1
asp.net-core ×1
data-binding ×1
events ×1
integer ×1
nlog ×1
nullable ×1
resharper ×1
xaml ×1