如何为C#Auto-Property提供默认值?我要么使用构造函数,要么还原为旧语法.
使用构造函数:
class Person
{
public Person()
{
Name = "Initial Name";
}
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用普通属性语法 (使用默认值)
private string name = "Initial Name";
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
我正在使用 .NET Core 3.1 辅助服务模板来构建 Windows 服务。
我认为基本流程应该在 内处理ExecuteAsync,大致如下:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
_Logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await SomeMethodThatDoesTheWork(stoppingToken);
}
catch (Exception ex)
{
_Logger.LogError(ex, "Global exception occurred. Will resume in a moment.");
}
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试从进程中正确发出我希望应用程序关闭的信号。
停止服务时,我假设StopAsync()被调用。这似乎工作正常。所以我认为await StopAsync()会成功,但之后,ExecuteAsync仍然继续运行(所以我猜StopAsync不会要求取消,你不应该自己调用它)。
所以我用另一个布尔值修改了我的循环:
while (!stoppingToken.IsCancellationRequested && !_ShuttingDown)
Run Code Online (Sandbox Code Playgroud)
这确实退出循环,并且永远不会ExecuteAsync再次进入。但是,该应用程序只是继续运行。根据调试器,它只是停留在host.Run().
我如何向应用程序主机发出我想要关闭的信号?
我在针对.NET Framework 4.6.1的项目中使用VisualStudio 2017.
玩Task,CancellationToken和本地方法,我来到这个代码:
class Program
{
static void Main(string[] args)
{
CancellationToken o;
Console.WriteLine(o);
}
}
Run Code Online (Sandbox Code Playgroud)
哪个编译.现在,如果您将类型更改o为a int或string它将无法编译,则会给出错误:
在访问之前,可能不会初始化局部变量"o".
我试图CancellationToken在名为的结构中反编译并复制代码MyCancellationToken.那也不会编译.
我设法编译的唯一情况是一个空结构或包含一个结构的结构CancellationToken.
struct EmptyStruct
{
}
struct MagicStruct
{
public CancellationToken a;
}
class Program
{
static void Main(string[] args)
{
EmptyStruct a;
MagicStruct b;
Console.WriteLine(a);
Console.WriteLine(b);
}
}
Run Code Online (Sandbox Code Playgroud)
CancellationToken不需要初始化?有趣的事实:如果你将它与本地函数混合,你可以这样写:
class Program
{
static void Main(string[] args)
{
Work();
CancellationToken …Run Code Online (Sandbox Code Playgroud) 在Java中,Supplier接口代表一个没有参数和通用返回值的函数。
Supplier<String> randomPasswordSupplier = () -> "secret";
String randomPassword = randomPasswordSupplier.get();
Run Code Online (Sandbox Code Playgroud)
C# 中是否有与此接口等效的接口?
使用ReSharper,有没有办法更改此代码:
TValue this[TIndex index]
{
get // optional
{
// getter code
}
set // optional
{
// setter code
}
}
Run Code Online (Sandbox Code Playgroud)
对于这段代码:
TValue GetStuff(TIndex index)
{
// getter code
}
void SetStuff(TIndex index, TValue value)
{
// setter code
}
Run Code Online (Sandbox Code Playgroud)
当然,还会自动更新代码的使用情况.
我现在不需要逆操作,但知道它可能很有趣.
我需要返回一个的第一个元素IEnumerable.如果IEnumerable为空,我会返回一个特殊值.
这个代码看起来像这样:
return myEnumerable.FirstOrDefault() ?? mySpecialValue;
Run Code Online (Sandbox Code Playgroud)
这很好,工作正常,直到myEnumerable包含可空的东西.
在这种情况下,我得到的最好的是:
return myEnumerable.Any() ? myEnumerable.First() : mySpecialValue;
Run Code Online (Sandbox Code Playgroud)
但在这里我有多个枚举myEnumerable!
我该怎么做 ?我宁愿避免必须抓住任何例外.
我有一个实现一些接口的类:
public interface IMyInterface
{
string GetKey();
void SetData(List<int> data);
}
public class A : IMyInterface { .. }
public class B : IMyInterface { .. }
Run Code Online (Sandbox Code Playgroud)
我有一个方法可以获取这些类的集合并做同样的事情:
public void methodA(List<A> items)
{
foreach(var item in items)
{
// do something with A.GetKey();
// do something with A.SetData();
}
}
public void methodB(List<B> items)
{
foreach(var item in items)
{
// do something with B.GetKey();
// do something with B.SetData();
}
}
Run Code Online (Sandbox Code Playgroud)
我想使这个方法通用:
public GenericMethod<T>(T items) where T: IEnumerable
{ …Run Code Online (Sandbox Code Playgroud) 我在使用此IEnumerable扩展的应用程序上工作:
public static bool IsEmpty<TSource>([NoEnumeration] this IEnumerable<TSource> source)
{
return !source.Any(); // not actual implementation
}
Run Code Online (Sandbox Code Playgroud)
但是很多用法都是这样的:
var notEmpty = !stuffs.IsEmpty()
Run Code Online (Sandbox Code Playgroud)
在这里,我得到了一个双重(无用)的否定.
我想自动重新计算所有这些用法是这样的:
var notEmpty = stuffs.Any()
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?与ReSharper?
我不知道如何将币安服务器给出的时间戳转换为有效的DateTime。
Binance.API.Csharp.Client.Models.General.ServerInfo返回
1615724572987转换后DateTime给出
1/2/0001 9:52:52 PM
显然是不正确的。
我试图找到有关 ServerInfo 类型的描述,但只有 GetHtml 函数。
我在java中编写了一个简单的程序:
public class Will {
int static square(int x) {
int y = x * x;
return y;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number...");
int n = sc.nextInt();
int result = Will.square(n);
System.out.println("Square of " + n + " is " + result);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我收到了这些错误:
square.java:6: error: <identifier> expected
int static square (int x)
^
square.java:6: error: invalid method declaration; return type required
int static square (int x)
^ …Run Code Online (Sandbox Code Playgroud) c# ×9
java ×2
resharper ×2
.net-core ×1
asp.net ×1
binance ×1
constructor ×1
generics ×1
ienumerable ×1
struct ×1