随笔分类 - C#
访问修饰符(C# 编程指南)(链接)
摘要:下面这篇微软官方文档,介绍了C#中的public、private、protected、internal等访问修饰符: Access Modifiers (C# Programming Guide) 文档中有两个地方,这里特别强调下: Classes and structs declared dire
阅读全文
抽象类为什么可以有构造函数?- Constructor of an abstract class in C#(转载)
摘要:问 Why is it possible to write constructor for an abstract class in C#?As far as I know we can't instantiate an abstract class.. so what is it for?You
阅读全文
.NET Core中创建和使用NuGet包
摘要:在.NET Core的项目中,如果我们要在项目中引用其它DLL文件,不建议直接在项目引用中添加DLL文件(虽然在.NET Core项目中也可以这么做),建议是去直接下载DLL文件所属的NuGet包。这样最大的好处是我们可以将要引用DLL文件的所有依赖文件也一起引入到项目中,这样保证了引用文件的完整性
阅读全文
生成的Guid会重复吗?(链接)
摘要:下面这篇帖子讨论了,在生成Guid的时候,会不会产生重复值,答案是会产生,但是机率很小。 Is a GUID unique 100% of the time? 其中这里提到了,V4版本的Guid不再使用计算机的MAC地址作为Guid的生成元素,取而代之的是使用一个随机数来生成Guid。所以这就是为什
阅读全文
C#中重载方法的一些总结
摘要:新建一个.NET Core控制台项目,我们来看看C#中重载方法的一些注意事项。 C#中多个重载方法之间的参数如果有继承关系,那么调用方法时,会调用与传入参数类型最接近的重载方法 我们来举个例子,下面我们定义了两个重载方法Do,它们的参数类型A和B是继承关系,类B继承类A,那么我们在调用Do方法时,到
阅读全文
DateTimeOffset的构造函数抛出异常:UTC offset of local dateTime does not match the offset argument(转载)
摘要:DateTimeOffset Error: UTC offset of local dateTime does not match the offset argument 问: I'm trying to create a small method that converts the time fr
阅读全文
C#中,接口转换实验
摘要:在C#中,接口是一个比较特殊的存在,一个接口的实例,可以转换为没有实现该接口的类,并且不会发生编译错误。 为此,我们新建一个.NET Core控制台项目,然后使用如下代码来进行实验: using System; namespace NetCoreInterfaceTypeConversion { /
阅读全文
TransactionScope and database connections(转载)
摘要:问 Do TransactionScope work with closed database connections? using (var transaction = new TransactionScope(TransactionScopeOption.Required)) { // crea
阅读全文
Async/Await模式中使用TransactionScope时,要设置参数为TransactionScopeAsyncFlowOption.Enabled枚举(转载)
摘要:TransactionScope and Async/Await. Be one with the flow! You might not know this, but the 4.5.0 version of the .NET Framework contains a serious bug re
阅读全文
Does disposing StreamReader or StreamWriter close the stream?(转载)
摘要:问 I am sending a stream to methods to write on, and in those methods I am using a binary reader/wrtier. When the reader/writer gets disposed, either b
阅读全文
使用自定义的行分隔符,从StreamReader中读取一行字符串
摘要:在C#中,StreamReader的ReadLine方法是不支持自定义行分隔符的。这导致很多文本文件的行分隔符如果不是"\r"和"\n",那么使用StreamReader就无法正确读取到一行字符串。 所以我们这里采用一个.NET Core控制台项目,自定义一个ReadLineWithDelimite
阅读全文
SQL Server和C#中无法将小数字符串直接转换为整数类型
摘要:有时候我们会将一个小数字符串转换为整数,例如将"31.0"转换为整数类型,因为这个小数本来就是一个整数,它的小数位为0。 SQL Server 如果我们在SQL Server中直接将字符串'31.0'转换为INT类型,会报错: DECLARE @text NVARCHAR(50)=N'31.0' S
阅读全文
Is default(CancellationToken) equivalent to CancellationToken.None?(转载)
摘要:问 Looking at the implementation of CancellationToken.None, it is simply returning default(CancellationToken). However, I see no reference in Cancellat
阅读全文
关于TransactionScope.Complete方法(链接)
摘要:相信有些同学会困扰,当我们执行TransactionScope.Complete()方法的时候,是否Transaction被真正提交到数据库了。 对于这一点,MSDN上有这么一段描述: If the TransactionScope object created the transaction in
阅读全文
Do I need to dispose of Tasks?(链接)
摘要:Do I need to dispose of Tasks? Here’s my short answer to this question: “No. Don’t bother disposing of your tasks.”
阅读全文
为什么C#接口中不能声明async异步函数(转载)
摘要:Unable to declare Interface “ async Task<myObject> MyMethod(Object myObj); ” 问 I'm unable to declare interface IMyInterface { async Task<myObject> MyM
阅读全文
How does SqlDataReader handle really large queries?(转载)
摘要:问 Actually I'm not sure the title accurately describes the question, but I hope it is close enough. I have some code that performs a SELECT from a dat
阅读全文
What is the use for Task.FromResult<TResult> in C#(转载)
摘要:问: In C# and TPL (Task Parallel Library), the Task class represents an ongoing work that produces a value of type T.I'd like to know what is the need
阅读全文
await Task.Yield()和await Task.CompletedTask有什么不同
摘要:有时候我们在代码中要执行一些非常耗时的操作,我们不希望这些操作阻塞调用线程(主线程)的执行,因为调用线程(主线程)可能还有更重要的工作要做,我们希望将这些非常耗时的操作由另外一个线程去执行,这个时候就可以用到await Task.Yield(),它借助了C# 5.0中的异步函数关键字await as
阅读全文
Whats the benefit of passing a CancellationToken as a parameter to Task.Run?(转载)
摘要:问: Obviously I realize it enables me to cancel the task, but this code achieves the same effect without having to pass the token into Task.RunWhat is
阅读全文
浙公网安备 33010602011771号