You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 4.7.2与.NET 6默认字符串排序差异及配置方法

.NET不同框架下字符串排序行为差异及配置方法

我编写了如下程序:

项目文件

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net472;net6.0</TargetFrameworks>
  </PropertyGroup>
</Project>

C#代码

using System;
using System.Collections.Generic;
using System.Linq;

internal static class Program
{
    private static void Main()
    {
        var list = new List<string>()
        {
            "were",
            "we're",
            "weird",
        };

        foreach (var item in list.OrderBy(i => i))
        {
            Console.WriteLine(item);
        }
    }
}

当以net472编译运行时,输出结果为:

weird
were
we're

当以net6.0编译运行时,输出结果为:

we're
weird
were

请问是否可以配置或确定默认应用的排序规则?


补充说明:
排序行为差异源于.NET框架使用的全球化组件不同——.NET Framework(如net472)默认使用NLS(国家语言支持),而.NET Core/.NET 5+(如net6.0)默认使用ICU(Unicode国际组件)。

通过在项目文件中添加以下配置,可让两个框架的排序行为保持一致:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
</ItemGroup>

内容的提问来源于stack exchange,提问作者Dee J. Doena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 06:32:06