rspec/rules/S4057/csharp/rule.adoc

71 lines
1.9 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
When you create a ``++DataTable++`` or ``++DataSet++``, you should set the locale explicitly. By default, the locale for these types is the current culture. For data that is stored in a database or file and is shared globally, the locale should ordinarily be set to the invariant culture (``++CultureInfo.InvariantCulture++``).
This rule raises an issue when ``++System.Data.DataTable++`` or ``++System.Data.DataSet++`` instances are created without explicitly setting the locale property (``++DataTable.Locale++`` or ``++DataSet.Locale++``).
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
using System;
using System.Data;
namespace MyLibrary
{
public class Foo
{
public DataTable CreateTable()
{
DataTable table = new DataTable("Customers"); // Noncompliant table.Locale not set
DataColumn key = table.Columns.Add("ID", typeof(Int32));
key.AllowDBNull = false;
key.Unique = true;
table.Columns.Add("LastName", typeof(String));
table.Columns.Add("FirstName", typeof(String));
return table;
}
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
using System;
using System.Data;
using System.Globalization;
namespace MyLibrary
{
public class Foo
{
public DataTable CreateTable()
{
DataTable table = new DataTable("Customers");
table.Locale = CultureInfo.InvariantCulture;
DataColumn key = table.Columns.Add("ID", typeof(Int32));
key.AllowDBNull = false;
key.Unique = true;
table.Columns.Add("LastName", typeof(String));
table.Columns.Add("FirstName", typeof(String));
return table;
}
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]