23 热度

C# 9

The official planned next C# release is C# 9. You can find in the link below the Language Version Planning for C#. As shown above, in the list, there are 34 proposals/features are planned for C# 9, but that does not mean that all of those proposals will be released in C# 9...

收录时间: 2019-12-10
分类: C#开发
贡献者: Rector
18 热度

C# Channels - Publish / Subscribe Workflows

In this article, we'll explore the synchronization data structures in .NET's System.Threading.Channels namespace and learn how to use them for designing concurrent workflows. It would be helpful to have some basic understanding of .NET's Task Parallel Library (TPL), but it's in no means necessary. Let's start by introducing some definitions.

收录时间: 2019-12-09
分类: C#开发
贡献者: Rector
24 热度

C#使用Consul集群进行服务注册与发现

我个人觉得,中间件的部署与使用是非常难记忆的;也就是说,如果两次使用中间件的时间间隔比较长,那基本上等于要重新学习使用。所以,我觉得学习中间件的文章,越详细越好;因为,这对作者而言也是一份珍贵的备忘资料。Consul简介Consul一个什么,我想大家通过搜索引擎一定可以搜索到;所以,我就不在重复他的官方描述了。这里,我为大家提供一个更加好理解的描述。Consul是什么?Consul本质上是一个Socket通信中间件。它主要实现了两个功能,服务注册与发现与自身的负载均衡的集群。我们可以把他理解为一个没有界面的应用程序,因为没有界面,所以想启动Consul就只能使用命令行了;也因为没有界面,一...

收录时间: 2019-12-06
分类: C#开发
贡献者: Rector
15 热度

Protecting C# Applications That Use Dynamic Types

The dynamic type in C# provides flexibility that is not available in other statically-typed languages. Since its introduction in C# 4.0 (.NET 4.5), we have worked with customers who wanted to know more about how dynamic types are impacted by the obfuscation process.When a dynamic type is used, the c...

收录时间: 2019-12-06
分类: C#开发
贡献者: Rector
20 热度

C# 异步编程

基于Task的异步编程模式(TAP)是Microsoft为.Net平台下使用Task进行编程所提供的一组建议,这种模式提供了可以被await消耗(调用)方法的APIs,并且当使用async关键字编写遵守这种模式的方法时,手写Task通常很有用。通常TAP用起来与普通方式没什么两样,但是不支持ref和out参数。     任务和线程的区别:     1、任务是架构在线程之上的,也就是说任务最终还是要抛给线程去执行。     2、任务跟线程不是一对一的关系,比如开10个任务并不是说会开10个线程,这一点任务有点类似线程池,但是任务相比线程池有很小的开销和精确的控制。     3、Task的优势Th...

收录时间: 2019-12-05
分类: C#开发
贡献者: Rector
20 热度

Embracing nullable reference types

Probably the most impactful feature of C# 8.0 is Nullable Reference Types (NRTs). It lets you make the flow of nulls explicit in your code, and warns you when you don’t act according to intent. The NRT feature holds you to a higher standard on how you deal with nulls, and as such it issues new warnings on existing code. So that those warnings (however useful) don’t break you, the feature must be explicitly enabled in your code before it starts complaining. Once you do that on existing code, you have work to do to make that code null-safe and satisfy the compiler that you did.

收录时间: 2019-11-28
分类: C#开发
贡献者: Rector
16 热度

C# 8 Nullable Types

I recently released a two-part course on C# 8. The first partis for beginning programmers. The second partis for intermediate to advanced programmers. This post is the first in a series that focuses on the new features in C# 8.I recently had Mads Torgenson on my Podcast (to be posted). Mads is...

收录时间: 2019-11-28
分类: C#开发
贡献者: Rector
28 热度

正则表达式 第五篇:C# 正则表达式

本文整理C#正则表达式的元字符,正则表达式是由字符构成的表达式,每个字符代表一个规则,表达式中的字符分为两种类型:普通字符和元字符。普通字符是指字面含义不变的字符,按照完全匹配的方式匹配文本,而元字符具有特殊的含义,代表一类字符。把文本看作是字符流,每个字符放在一个位置上,例如,正则表达式 “Room\d\d\d”,前面四个字符Room是普通字符,后面的字符\是转义字符,和后面的字符d组成一个元字符\d,表示该位置上有任意一个数字。用正则表达式的语言来描述是:正则表达式 “Room\d\d\d”共捕获7个字符,表示“以Room开头、以三个数字结尾”的一类字符串,我们把这一类字符串称作一个模式(...

收录时间: 2019-11-23
分类: C#开发
贡献者: Rector
26 热度

Easy unit testing of null argument validation (C# 8 edition)

Basically, for each of the specified parameters, the AssertThrowsWhenArgumentNull method rewrites the lambda expression by replacing the corresponding argument with null, compiles and executes it, and checks that it throws an ArgumentNullException with the appropriate parameter name. This method has served me well for many years, as it drastically reduces the amount of code to test argument validation. However, I wasn’t completely satisfied with it, because I still had to specify the names of the non-nullable parameters explicitly…

收录时间: 2019-11-20
分类: C#开发
贡献者: Rector
28 热度

Using foreach with index in C#

for and foreach loops are among the most useful constructs in a C# developer’s toolbox. To iterate a collection, foreach is, in my opinion, more convenient than for in most cases. It works with all collection types, including those that are not indexable such as IEnumerable, and doesn’t require to access the current element by its index.

收录时间: 2019-11-19
分类: C#开发
贡献者: Rector
59 热度

C# 8 - 其它新特性

其它关于C# 8和.NET Core 3.0新特性的文章: C# 8 - Nullable Reference Types 可空引用类型 C# 8 - 模式匹配 C# 8 - Range 和 Index(范围和索引) C# 8.0 - 默认接口方法 C# 8 - using声明 和 异步流 .NET ...

收录时间: 2019-11-06
分类: C#开发
贡献者: Rector
66 热度

C# 8 - using声明 和 异步流

这两个主题没什么关系,但是怕文章太短被移除主页。 using声明 using语句块 这两个主题没什么关系,但是怕文章太短被移除主页。 using声明 using语句块 尽管.NET Core运行时有垃圾收集器(GC)来负责内存清理工作,但是我们还是要自己确保当非托管资源不再使用的时候应该被清理掉。以 ...

收录时间: 2019-11-01
分类: C#开发
贡献者: Rector
49 热度

如何利用C# Roslyn编译器写一个简单的代码提示/错误检查?

OK, 废话不多说,这些天在写C#代码时突然对于IDE提示有了一些想法,之前也有了解过,不过并没有深入。先看个截图:   一段再简单不过的代码了,大家注意看到 count 字段下面的绿色波浪线了吗,我们通常为理解一个 warning ,鼠标移上去:   提示“声明了该字段但没有使用过” 当然,这对项目代码编译没有什么影响。不过,我想了一下,如果我们可以重写IDE的编译器,把我们的一些规则写进去,对于团队的代码review其实是很高效的一件事情。我们通常的代码review会有滞后,都是通过提交PR或代码评审,如果我们把这个检查在开发人员写代码的时候第一时间提示出来或给error提示,实现所见即所...

收录时间: 2019-11-01
分类: C#开发
贡献者: Rector
35 热度

Defer with C# 8.0

Go (https://golang.org) has a really nice little language feature called defer, which is a keyword that lets you defer a statement until the current function returns, and you can see an example here. Given all the new language features in C# 8.0, I wanted to see what it would look like to use this in C# today.

收录时间: 2019-11-01
分类: C#开发
贡献者: Rector
63 热度

C# 8.0 的默认接口方法

直接看例子 例子 直接看例子 有这样一个接口: 有这样一个接口: 然后有三个它的实现类: 然后有三个它的实现类: 然后在main方法里面调用: 然后在main方法里面调用: 截至目前,程序都可以成功的编译和运行。 截至目前,程序都可以成功的编译和运行。 IPerson接口变更 IPerson接 ...

收录时间: 2019-10-31
分类: C#开发
贡献者: Rector
47 热度

How to write to a file with C# in 2019

Today's tip isn't exactly about a hot new version of ASP.NET Core or anything like that. I often find myself googling "simple" questions like how to write text to a file in the most quickly and effectively way. Most results are either blog posts using .NET 2 as an example, or StackOverflow answers from 2010. Common for all samples is that only the essential lines of code are added, and no focus on crucial aspects like error handling are highlighted. This post is my attempt to sum up what I've learned over the years.

收录时间: 2019-10-30
分类: C#开发
贡献者: Rector
60 热度

C#调用RabbitMQ实现消息队列

我在刚接触使用中间件的时候,发现,中间件的使用并不是最难的,反而是中间件的下载,安装,配置才是最难的。所以,这篇文章我们从头开始学习RabbitMq,真正的从头开始。关于消息队列其实消息队列没有那么神秘,我们这样想一下,用户访问网站,最终是要将数据以HTTP的协议的方式,通过网络传输到主机的某个端口上的。那么,接收数据的方式是什么呢?自然是端口监听啦。那消息队列是什么就很好解释了?它就是端口监听,接到数据后,将数据排列起来。那这件事,我们不用中间件能做吗?当然能做啦,写个TCP/UDP/Socket的软件就可以做啦。举个简单的例子,如下图:既然自己可以做消息队列,那为什么要用RabbitM...

收录时间: 2019-10-22
分类: C#开发
贡献者: Rector
117 热度

C#, Span and async

C# 7.2 introduced support for ref struct types. These are types that are required to live on the stack. The most notable ref struct types are Span and ReadOnlySpan which have made it possible to dramatically improve the efficiency of certain kinds of work by reducing or even eliminating object allocations.

收录时间: 2019-10-18
分类: C#开发
贡献者: Rector
66 热度

C# 8.0 and .NET Standard 2.0 - Doing Unsupported Things

Here is a slide a from by Mads Torgersen at .NET Conf, saying that for library authors you want to consider targeting both netcoreapp3.0 and netstandard2.0, and use C# 8.0 to get "nullified" during this "Nullable adoption phase". You can catch the full video here.

收录时间: 2019-10-17
分类: C#开发
贡献者: Rector
57 热度

Problem with timers and threads

I'm a learn-by-example C# coder who isn't very advanced, which is why this problem is completely stumping me regardless of the amount of information on the internet.I'm essentially creating a program that is, on a timer, repeatedly polling a website to get some information. During this process, a We...

收录时间: 2019-10-17
分类: C#开发
贡献者: Rector