1. Changing Thread Culture
[TestMethod] public void Example() { const string turkeyCultureString = "tr-TR"; const string australiaCultureString = "en-AU"; const double someNumber = 23.45; Debug.WriteLine("Setting English language - Australia country/region" ); var australiaCultureInfo = CultureInfo.GetCultureInfo(australiaCultureString); Thread.CurrentThread.CurrentCulture = australiaCultureInfo; var ausUppercaseI = "i".ToUpper(); Debug.WriteLine(ausUppercaseI); Debug.WriteLine(someNumber); Debug.WriteLine(DateTime.Now); Debug.WriteLine("Setting Turkish language - Turkey country/region"); var turkeyCultureInfo = CultureInfo.GetCultureInfo(turkeyCultureString); Thread.CurrentThread.CurrentCulture = turkeyCultureInfo; var turUppercaseI = "i".ToUpper(); Debug.WriteLine(turUppercaseI); Debug.WriteLine(someNumber); Debug.WriteLine(DateTime.Now); }
결과
2. Random Number Generating for Cryptography
[TestMethod]
public void HighSecurity()
{
var r = System.Security.Cryptography.RandomNumberGenerator.Create();
var randomBytes = new byte[4]; // 4 bytes so we can convert to an 32-bit int
r.GetBytes(randomBytes);
int rndInt = BitConverter.ToInt32(randomBytes, 0);
Debug.WriteLine(rndInt);
}
3. Careful for using Hashcode
특정 클래스의 객체의 hashcode를 사용할 때에는 해당 값을 사용중에 임의로 변경이 가능하도록 만들면, 아래의 코드에서는 문제가 발생할 수 있다.
<잘못된 구현>
public void BadGetHashCodeExample()
{
var p1 = new PersonIdBadHash {Id = 1};
var p2 = new PersonIdBadHash {Id = 2};
var d = new Dictionary();
d.Add(p1, "Sarah");
d.Add(p2, "John");
var john = d[p2];
p2.Id = 99;
john = d[p2]; // error 발생
}
Id 의 값을 변경함에 따라 key 값이 변경되어 element를 찾을 수 없어 오류가 발생함.
<좋은 예>
정의
internal class PersonIdBetterHash { public PersonIdBetterHash(int id) { Id = id; } public int Id { get; private set; } public override int GetHashCode() { return Id.GetHashCode(); } // Equals override omitted }
구현
public void BetterGetHashCodeExample()
{
var p1 = new PersonIdBetterHash(1);
var p2 = new PersonIdBetterHash(2);
var d = new Dictionary();
d.Add(p1, "Sarah");
d.Add(p2, "John");
var john = d[p2];
// p2.Id = 99; Id is now immutable, so our hashcode cannot change now
}
4. Combinable Enum using Flag Attribute
// Example of the FlagsAttribute attribute. using System; class FlagsAttributeDemo { // Define an Enum without FlagsAttribute. enum SingleHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; // Define an Enum with FlagsAttribute. [FlagsAttribute] enum MultiHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; static void Main( ) { Console.WriteLine( "This example of the FlagsAttribute attribute \n" + "generates the following output." ); Console.WriteLine( "\nAll possible combinations of values of an \n" + "Enum without FlagsAttribute:\n" ); // Display all possible combinations of values. for( int val = 0; val <= 8; val++ ) Console.WriteLine( "{0,3} - {1}", val, ( (SingleHue)val ).ToString( ) ); Console.WriteLine( "\nAll possible combinations of values of an \n" + "Enum with FlagsAttribute:\n" ); // Display all possible combinations of values. // Also display an invalid value. for( int val = 0; val <= 8; val++ ) Console.WriteLine( "{0,3} - {1}", val, ( (MultiHue)val ).ToString( ) ); } }
/*
This example of the FlagsAttribute attribute
generates the following output.
All possible combinations of values of an
Enum without FlagsAttribute:
0 - Black
1 - Red
2 - Green
3 - 3
4 - Blue
5 - 5
6 - 6
7 - 7
8 - 8
All possible combinations of values of an
Enum with FlagsAttribute:
0 - Black
1 - Red
2 - Green
3 - Red, Green
4 - Blue
5 - Red, Blue
6 - Green, Blue
7 - Red, Green, Blue
8 - 8
*/