32 热度

C# dotnet 使用 startIndex 提升 IndexOf 的性能

在代码审查 WPF 仓库的时候,小伙伴告诉我说使用 dotnet core 版本的 WPF 开了 ReadyToRun 的提升还不够大,他准备开始一大波业务无关的优化,其中就包含了 xaml 中的字符串相关优化。我在 davidwrighton 大大的优化代码和 pentp 大大的代码审查里面学到了使用 startIndex 提升 IndexOf 的性能,本文就来和大家分享一下。

收录时间: 2020-08-05
分类: C#开发
贡献者: Rector
32 热度

C# dotnet 使用判断文件夹存在的方法判断一个文件路径会怎样

假定我有一个文件的路径,我将这个文件路径放在文件夹判断方法里面,请问此时返回的是存在还是不存在?答案是返回不存在。如下面测试代码,这里的 GelteajoutrerebaKoutigasremawcho.dll 就是程序集,也就是文件是存在的,那么放在 DirectoryInfo 判断输出的内容是否存在...

收录时间: 2020-08-05
分类: C#开发
贡献者: Rector
32 热度

重学c#系列——异常续[异常注意事项](七)

对上节异常的补充,也可以说是异常使用的注意事项。正文减少try catch的使用前面提及到,如果一个方法没有实现该方法的效果,那么就应该抛出异常。如果有约定那么可以按照约定,如果约定有歧义,那么还是要抛出异常。我们知道使用try catch 其实是要消耗性能的,那么是否能避免使用try catch呢?或者减少使用try catch呢?我们使用api的时候,知道http制定了一套错误码,那么我们是否能使用错误码返回来做一个约定的呢?答案是否定的,且不论我们的业务的不同,错误码制定的困难,单是方法里面使用错误码偶和性就非常大。既然无法通过约定一套错误码来解决,那么是否可以先验证该方法能否执行成...

收录时间: 2020-08-05
分类: C#开发
贡献者: Rector
56 热度

C# File.Exists 判断系统文件,警惕32位和64位的差异

今天在调试一个Winform程序,使用File.Exists 判断一个已经存在的驱动文件,程序一直返回false。因为驱动文件属于系统目录,心想难道是权限不够导致的?然后用管理员身份运行软件,依然返回false。吓的我赶紧去系统目录C:\Windows\system32\drives 搜索该文件,一看在这里啊,怎么还是返回false。开始还以为VS出问题了,然后尝试判断一个D盘下的文件,发现可以判断成功。

收录时间: 2020-07-27
分类: C#开发
贡献者: Rector
58 热度

C# 9: Putting it all together with a scavenger hunt

Here we are, friends: our last post in our C# 9 deep dive series. We’ve discussed a lot of topics, so I thought it’d be fun to bundle all we’ve learned into a single post. (You can think of this as a CliffsNotes of the 10,000 or so words I’ve written about C# 9 so far.)

收录时间: 2020-07-23
分类: C#开发
贡献者: Rector
48 热度

C# 9: Minor Improvements for Lambdas

Lambdas will be getting a small upgrade in C# 9 with two new features. Neither will change the way code is written, but they do clarify the developer’s intent.

收录时间: 2020-07-23
分类: C#开发
贡献者: Rector
AD 友情赞助
56 热度

C# 9: Range Operators in Switch Constructs and Pattern

Since C# was first introduced, developers have complained about the lack of a range operator in switch constructs. This made switches far less useful in C# than they were in VB. As part of the pattern-matching enhancements for C# 9, this limitation has been addressed.

收录时间: 2020-07-18
分类: C#开发
贡献者: Rector
60 热度

C# 9 Deep Dive: Target Typing and Covariant Returns

We’ve been quite busy, my friends. In this C# 9 deep dive series, we’ve looked at init-only features, records, pattern matching, and then top-level programs. To complete this series (before showing off everything in a single app), we’ll discuss the last two items featured in the Build 2020 announcement: target typing and covariant returns. These are not related, but I’ve decided to bundle these in a single blog post. This is the fifth post in a six-post series on C# 9 features in-depth...

收录时间: 2020-07-16
分类: C#开发
贡献者: Rector
34 热度

C# 8.0 nullable references: more type system transcendence with DisallowNull

In my last blog in this series, I talked about the AllowNull attribute. This post shows its converse, DisallowNull. As with all the attributes relating to nullable references, the goal is to improve expressiveness, enabling the compiler to report more nullable-related problems without producing spurious warnings.

收录时间: 2020-07-15
分类: C#开发
贡献者: Rector
55 热度

Implementation of the C# IO container

This article is part of an article series about the IO container in C#. In the previous articles, you've seen how a type like IO can be used to distinguish between pure functions and impure actions. The point of the article series is to illustrate the concept that Haskell uses to impose the functional interaction law at compile time. The implementation details really aren't important. Still, I believe that I know my readership well enough that a substantial fraction would be left unsatisfied if I didn't share the implementation details.

收录时间: 2020-07-14
分类: C#开发
贡献者: Rector
54 热度

C#各类集合介绍

集合(Collection)类是专门用于数据存储和检索的类。这些类提供了对栈(stack)、队列(queue)、列表(list)和哈希表(hash table)的支持。大多数集合类实现了相同的接口。平常在工作中List集合是用的最多的,其次是Array(数组).今天整理一下各类形式的集合,不用就容易忘,必须要记录一下...

收录时间: 2020-07-13
分类: C#开发
贡献者: Rector
73 热度

Regex Named Groups and Using Them in C#

While working on an issue in Codespaces, I figured this would be a good case to implement some regex. Each time I work with regex I need to figure out how it works again, but also each time I am impressed with how powerful it is. And actually, this time I learned something new: regex named groups. Regex has the ability to name each matched group which is very easy to subsequently use in C# code. In this post, I will tell you how to do exactly that.

收录时间: 2020-07-10
分类: C#开发
贡献者: Rector
56 热度

C# dotnet 使用 FileStream 随机文件读写

本文说的随机文件读写的随机的反义词是顺序,这里的随机文件读写对应顺序文件读写。表示文件可以不按照顺序进行读写。进行文件读写的时候,基本上读是几乎不存在问题的,而写的话就稍微坑了一点,在 dotnet 里面默认没有提供 RandomAccessStream 类,这个 RandomAccessStream 类仅在 UWP 中可以使用。如果在不引用 UWP 的 WPF 里面,或者在 ASP.NET Core 以及 Xamarin 里面,也可以通过 FileStream 的 Seek 方法做到进行随机的读写...

收录时间: 2020-07-07
分类: C#开发
贡献者: Rector
75 热度

C# Futures: Null Check Operator

In the June 24th C# Language Design Meeting, Microsoft made a subtle change to the parameter null checking syntax. By changing it from Type parameterName! to Type parameterName!! they effectively introduced a new ‘null check operator’.

收录时间: 2020-07-03
分类: C#开发
贡献者: Rector
87 热度

C# 从1到Core--委托与事件

委托与事件在C#1.0的时候就有了,随着C#版本的不断更新,有些写法和功能也在不断改变。本文温故一下这些改变,以及在NET Core中关于事件的一点改变。一、C#1.0 从委托开始1. 基本方式什么是委托,就不说概念了,用例子说话。某HR说他需要招聘一个6年 .NET5 研发经验的“高级”工程师,他想找人(委托)别人把这条招聘消息发出去。这样的HR很多,所以大家定义了一个通用的发消息规则:public delegate string SendDelegate(string message);这就像一个接口的方法,没有实际的实现代码,只是定义了这个方法有一个string的参数和返回值。所有想发招...

收录时间: 2020-07-01
分类: C#开发
贡献者: Rector
AD 友情赞助
65 热度

C# 9 Deep Dive: Init-only features

A few weeks ago, we took a quick tour of some upcoming C# 9 features that will make your development life easier. We dipped our toes in the water. But now it’s time to dig a little deeper.I’m starting a new series over the next several weeks, that will showcase all of the announced featuresincrement...

收录时间: 2020-07-01
分类: C#开发
贡献者: Rector
71 热度

Misusing C#: Multiple Main() Methods

Sometimes you run across things you should probably never do in real life but are possible in your development environment. Having more than one "Main()" method in an application is one of those things in the .NET world.

收录时间: 2020-07-01
分类: C#开发
贡献者: Rector
75 热度

C# 9.0 新特性之 Lambda 弃元参数

大家好,这是 C# 9.0 新特性短系列的第 5 篇文章。弃元(Discards) 是在 C# 7.0 的时候开始支持的,它是一种人为丢弃不使用的临时虚拟变量。语法上它是用来赋值的,但它却不被分配存储空间,即没有值,所以不能从中读取值。弃元用 _(下划线) 表示,下划线是一个关键字,只能赋值,不能读取,例如...

收录时间: 2020-06-30
分类: C#开发
贡献者: Rector
52 热度

C# 8.0 nullable references: NotNull

So far in this sub-series on attributes for getting better results from C# 8's nullable references feature I've described the AllowNull and DisallowNull attributes. Now it's NotNull's turn. As with all of these attributes, this enables us to describe our intent in more detail so that the compiler can find more null-related programming errors without producing spurious warnings. This particular post is concerned with a scenario where the successful execution of a method implies that we can now be certain that something isn't null, and we'd like the compiler to know that.

收录时间: 2020-06-30
分类: C#开发
贡献者: Rector
74 热度

Differences between String and string in C#

In this article, we’re going to tackle the popular question among many developers, and that’s “What’s the difference between string and String” and “When should I use string, and when should I use String” in my applications. Both of these versions exist for a reason, so let’s see what they are and if they’re indeed that important.

收录时间: 2020-06-24
分类: C#开发
贡献者: Rector
AD 友情赞助