ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#InvariantCulture|CurrentCulture|Ordinal 详细解说

2021-09-12 19:01:10  阅读:358  来源: 互联网

标签:Ordinal 文化 CultureInfo C# DateTime date InvariantCulture 字符串


InvariantCulture:程序间、程序数据库、程序网络交互用InvariantCulture

invariant是基于en-US文化,但是与en-US还是有区别的。

例如:不变文化使用国际符号作为货币:"\"与美元符号:"$",用于格式化货币。

然而,在大多数情况下,它们非常相似。

CurrentCulture:程序和用户之间使用CurrentCulture

 InvariantCulture  

原文:使用不变文化属性|微软文档 (microsoft.com)

文化信息. InvariantCulture属性既不是中性文化,也不是特定文化。它是第三种对文化麻木不仁的文化。它与英语相关联,但与一个国家或地区无关。您可以在系统中几乎任何方法中使用不变文化。需要文化的全球化命名空间。但是,您应该仅针对需要文化独立结果(如系统服务)的流程使用不变文化。在其他情况下,它产生的结果可能是语言不正确或文化上不恰当。

当安全决策将基于字符串比较或案例更改的结果时,您还应使用不变文化。字符串.比较、字符串、托普和字符串等方法的默认实现。如果文化信息。当前文化被更改,或者代码运行的计算机上的文化与开发人员用于测试代码的文化不同,执行文化敏感字符串操作的代码可能会导致安全漏洞。编写字符串操作的开发人员预期的行为将不同于操作运行计算机上的代码的实际行为。为了消除文化差异,确保无论文化信息的价值如何,当前文化,请使用超载的字符串.比较,字符串.ToUpper和字符串.ToLower方法,接受文化信息参数,指定文化信息.文化信息参数的不变文化属性。 有关使用不变文化属性执行文化不敏感字符串操作的更多信息,请参阅"文化-不敏感字符串操作"。

 InvariantCulture可用于存储不会直接显示给最终用户的数据。以独立于文化的格式存储数据可保证已知格式不会改变。当来自不同文化文化的用户访问数据时,可以根据用户对数据进行适当的格式化。例如,如果您将DateTime类型存储在文本文件中(为 InvariantCulture格式格式化)中,则在调用DateTime. ToString方法以存储字符串和日期时使用变体文化属性。这将确保DateTime类型的基本值在来自不同文化的用户读取或编写数据时不会发生变化。

以下代码示例说明使用日期时间.ToString方法将日期时间写入文件,作为为不变文化格式的字符串。然后,字符串以不变文化格式从文件中读取,然后使用Date.Parse方法解析为日期时间。然后,对日期时间进行格式化并显示为文化"fr-FR"和"ja-JP"。

 

using System;
using System.IO;
using System.Globalization;

public class TextToFile 
{
   private const string FILE_NAME = "MyDateFile.txt";
   public static void Main(String[] args) 
   {
      if (File.Exists(FILE_NAME)) 
      {
         Console.WriteLine("{0} already exists!", FILE_NAME);
         return;
      }
      StreamWriter sw = File.CreateText(FILE_NAME);
      
      // Creates a DateTime.      
      DateTime dtIn = DateTime.Now;
      // Creates a CultureInfo set to InvariantCulture.
      CultureInfo InvC = new CultureInfo("");
      // Converts dt to a string formatted for InvariantCulture,
      // and writes it to a file.
      sw.WriteLine (dtIn.ToString("d",InvC));
      sw.Close();

      if (!File.Exists(FILE_NAME)) 
      {
         Console.WriteLine("{0} does not exist!", FILE_NAME);
         return;
      }
      StreamReader sr = File.OpenText(FILE_NAME);
      String date;
      while ((date=sr.ReadLine())!=null) 
      {
         Console.WriteLine("\nThe date stored in the file formatted for 
               the invariant culture is:\n{0}" , date);    

         // Parses the string stored in the file,
         // and stores it in a DateTime.
         DateTime dtout = DateTime.Parse(date, InvC);

         // Creates a CultureInfo set to "fr-FR".
         CultureInfo frc = new CultureInfo("fr-FR");
         // Displays the date formatted for the "fr-FR" culture.
         Console.WriteLine("\nThe date read from the file and formatted 
               for the culture \"fr-FR\" is:\n{0}" , dtout.ToString("d", 
               frc));

         // Creates a CultureInfo set to "ja-JP".
         CultureInfo jpn= new CultureInfo("ja-JP");
         // Displays the date formatted for the "ja-JP" culture.
         Console.WriteLine("\nThe date read from the file and formatted 
               for the culture \"ja-JP\" is:\n{0}" , dtout.ToString("d", 
               jpn));
      }
      Console.WriteLine ("\nThe end of the stream has been reached.");
      sr.Close();
   }
}

此代码生成以下输出:

The date stored in the file formatted for the invariant culture is:
07/24/2001

The date read from the file and formatted for the culture "fr-FR" is:
24/07/2001

The date read from the file and formatted for the culture "ja-JP" is:
2001/07/24

The end of the stream has been reached.

 

标签:Ordinal,文化,CultureInfo,C#,DateTime,date,InvariantCulture,字符串
来源: https://www.cnblogs.com/cdaniu/p/15259547.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有