小编Jog*_*gge的帖子

可空类型"int"的默认值是多少?(包括问号)?

在C#中,类型实例变量的默认值是int?什么?

例如,在以下代码中,MyNullableInt如果从未明确分配,将具有什么值?

class MyClass
{
    public int? MyNullableInt;
}
Run Code Online (Sandbox Code Playgroud)

(似乎答案几乎肯定是null或者0,但是那些是什么?)

c# integer nullable

89
推荐指数
2
解决办法
6万
查看次数

ReSharper 建议将模式匹配代码更改为对象模式

ReSharper 建议更改以下代码:

if (MyString is string myString)
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

对象模式:

if (MyString is { } myString)
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

它说:

源表达式始终是模式类型,匹配所有非空值。

我以前从未见过这种语法,也找不到任何有关它的文档。它是什么,它有什么作用?

c# resharper

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

在另一个类中更改先决条件属性时,为依赖属性引发PropertyChanged?

我有这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)

c# data-binding wpf xaml inotifypropertychanged

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

在 appsettings.json 中为 NLog 连接字符串引用变量

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}",这是行不通的。

c# nlog appsettings .net-core asp.net-core

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

如何提高派生类中的属性?

如何提高PropertyChangedSomePropertyB

此示例无法编译,因为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# wpf events propertychanged inotifypropertychanged

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