我在一个简单类中的属性上方使用 [Required()]:
public class A
{
[Required()]
public string Str { get; set; }
public int Salary { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在 Main() 中,我创建了该类的一个实例,但没有设置属性:
static void Main(string[] args)
{
A a = new A();
}
Run Code Online (Sandbox Code Playgroud)
我预计会得到一个异常,因为我没有为 Str 属性设置任何值,但我没有得到任何值。我是否错过了[必填]的目的?
我一直在寻找关于这两者之间差异的良好解释,但并没有真正找到。
到目前为止我所知道的是:相关id是一个字符串(Guid已转换为字符串),而交付标签是一个int。每条消息的关联 ID 都是唯一的,并且传递标记仅在通道中唯一(通道是范围)。
好吧……但是目的有什么不同呢?为什么一条消息需要两个标识符?
一个简单的控制台应用程序,在 Visual Studio 2019、.Net Framework 4.7、Windows 中:
static void Main(string[] args)
{
try
{
Console.WriteLine("In try");
throw new IndexOutOfRangeException();
}
finally
{ *// Surprisingly this part is not being executed.*
Console.WriteLine("In finally");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我确信在没有异常的情况下以及在有异常的情况下都会调用finally块。我在文档中读到:
但是,如果异常未处理,则finally块的执行取决于异常展开操作的触发方式。反过来,这取决于您的计算机的设置方式。
嗯,我很困惑。我是否需要对这个“展开操作”做一些事情,以便在出现未处理的异常时调用finally?
c# finally try-catch console-application unhandled-exception
在未完成的任务之前等待将控制权交给调用者,直到任务完成。
当您在 Main() 中使用它时,谁将获得控制权?
public static async Task Main()
{
await F1() ; //This await will pass the control to ???
}
public static async Task F1()
{
await Task.Run(()=>{...}) ; //This await will pass the control to Main()
}
Run Code Online (Sandbox Code Playgroud) 据我所知,在 Xamarin.Forms 中,您需要一个源和一个路径,才能执行绑定。
现在,我遇到了这段代码:
<Grid Padding="10">
<Label Text="{Binding Title}" d:Text="{Binding .}" FontSize="20"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
TFS 是本地部署的,这 3 个都是在线的。这很清楚。
我不明白的是:这三个有什么区别?
看起来它们都是一样的,但我想我错过了一些东西。
我有 Visual Studio 2019 和一个空的.net 框架项目。我想仅通过代码构建一个 Windows 窗体项目(不使用任何模板)。
为了这个目标,我需要 System.Windows.Forms 程序集:
右键单击项目 --> 添加 --> 引用 --> COM ----> 该程序集在哪里?
ps 有一个叫做 System_windows_forms ('-' 而不是 '.')的东西,这不是我要找的。
我有一个在thread1上运行的代码。我以同步方式调用函数(使用异步方法,但它不应该打扰我 - 异步方法不会将代码变为异步)。
我有一个等待,ConfigureAwait 设置为 false,所以我理解了它是一个任务延续之后的代码,它假设在与等待之前的代码不同的线程中运行(因为 ConfigureAwait 被设置为 false)。
通过我的测试 - 所有代码都在同一个线程中运行。如何?为什么等待下面的代码不在不同的线程上运行?这是代码:
public async void F1()
{
Console.WriteLine($"Thread.CurrentThread.ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
int x = await F2().ConfigureAwait(false);
Console.WriteLine($"Thread.CurrentThread.ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
}
private async Task<int> F2()
{
Console.WriteLine("Begins F2");
Console.WriteLine($"Thread.CurrentThread.ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine("Finishes F2");
return 7;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
Thread.CurrentThread.ManagedThreadId=1
Begins F2
Thread.CurrentThread.ManagedThreadId=1
Finishes F2
Thread.CurrentThread.ManagedThreadId=1
Run Code Online (Sandbox Code Playgroud) 据我所知,抽象方法只能存在于抽象类中。尽管如此,我可以看到微软这样做了(在 Xamarin.Forms 中):
public static class DependencyService
{
….
public static void Register<T>() where T : class;
….
}
Run Code Online (Sandbox Code Playgroud)
这是非抽象类中的抽象方法。怎么会这样?
c# ×5
.net ×3
async-await ×2
abstract ×1
annotations ×1
assemblies ×1
attributes ×1
azure-devops ×1
binding ×1
datacontext ×1
finally ×1
rabbitmq ×1
saas ×1
static ×1
try-catch ×1
winforms ×1
xamarin ×1