Int32.parse vs Convert.ToInt32(string) vs Int32.TryParse(string, out int)
1. Int32.parse(string)
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
int result;
bool success;
result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException
2. Convert.ToInt32(string) When s is a null reference, it will return 0 rather than throw ArgumentNullException.
result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException
3. Int32.TryParse(string, out int)
success = Int32.TryParse(s1, out result); //-- success => true; result => 1234
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
Variable Scope in Lambda Expressions
- 캡쳐되는 변수를 reference 하는 delegate가 있다면 garbage-collect 되지 않는다.
- lambda expression에 사용되는 변수는 outer method에 표시되지 않는다.
- lambda expression에서는 ref, out parameter를 사용할 수 없다.
- A return statement in a lambda expression does not cause the enclosing method to return
- A lambda expression cannot contain a goto statement, break statement, or continue statement that is inside the lambda function if the jump statement’s target is outside the block. It is also an error to have a jump statement outside the lambda function block if the target is inside the block.
FileStream
- binary writer vs stream writer The binary writer writes the in-memory binary representation of the integer. The stream writer writes the ASCII representation. Generally speaking, the former can be more compact and efficient (consider writing the integer 23861398 - the binary writer would require 4 bytes, but the stream writer would require 8, 16, or even 32 depending on the encoding) but the latter results in plain old text.
short value = 32000;
Byte[] shortBytes = BitConverter.GetBytes(value);
foreach (var elem in shortBytes)
Console.Write(string.Format("{0:X} ", elem));
Console.WriteLine();
Console.WriteLine(BitConverter.ToString(shortBytes));
short shortValue = BitConverter.ToInt16(shortBytes, 0);
MemoryStream ms = new MemoryStream();
byte[] buf = Encoding.UTF8.GetBytes("APPLE");
ms.Write(buf, 0, buf.Length);
StreamWriter sw = new StreamWriter(ms, Encoding.UTF8);
sw.WriteLine("Hello World");
sw.WriteLine(32000);
using (FileStream fs = new FileStream("test.log", FileMode.Create))
{
StreamWriter fileSw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine("Hello World");
sw.WriteLine(32000);
sw.Flush();
}
FirstOrDefault & SingleOrDefault 예제 추가
string[] names = { "Hartono, Tommy", "Adams, Terry",
"Andersen, Henriette Thaulow",
"Hedlund, Magnus Thaulow", "Ito, Shu" };
string firstLongName = names.FirstOrDefault(name => name.Length > 20);
Console.WriteLine("The first long name is '{0}'.", firstLongName);
string firstVeryLongName = names.FirstOrDefault(name => name.Length > 30);
Console.WriteLine(String.IsNullOrEmpty(firstVeryLongName) ? "No such string!" : firstVeryLongName);
Console.WriteLine();
//length > 22인 element가 두 개 이상 있다면 InvalidOperationException throw!
//string singleLongName = names.SingleOrDefault(name => name.Length > 22);
string singleLongName = names.SingleOrDefault(name => name.Length > 23);
Console.WriteLine(singleLongName);
string singleVeryLongName = names.SingleOrDefault(name => name.Length > 30);
Console.WriteLine(String.IsNullOrEmpty(singleVeryLongName) ? "No such string!" : singleVeryLongName);
string Example
Console.WriteLine(string.Format("{0}", 5, 6));
// 5만 출력하고 정상동작
//Console.WriteLine(string.Format("{0}, {1}", 5));
// throw exception!
// Unhandled Exception: System.FormatException: Index(zero based) must be greater than or equal to zero and less than the size of the argument list.
// PTS CString example!
Console.WriteLine(string.Format("{0:N2} %", 15.253532532));
Console.WriteLine(string.Format("{0:N4} %", 15.253532532));
Console.WriteLine();
Console.WriteLine(string.Format("{0:P}", 0.15253532532));
Console.WriteLine(string.Format("{0:P6}", 0.15253532532));
double value = 1032.45678;
Console.WriteLine(string.Format("{0:000000.00}", value));
Console.WriteLine(string.Format("{0:F2}", value));
Context Switching
thread는 CPU의 명령어 실행과 관련된 정보를 보관하고 있는데, 이를 thread context라 한다. CPU는 현재 실행 중인 thread를 다음에 이어서 실행할 수 있게 CPU의 환경 정보를 thread context에 보관한다. 그리고 OS로부터 할당받은 thread context정보를 다시 CPU내부에 로드해서 마치 해당 thread가 실행되고 있었던 상태인 것처럼 복원 후 실행한다. 이를 context switching이라 한다.
##### 출처 Int32.parse vs Convert.ToInt32(string)(http://www.codeproject.com/Articles/32885/Difference-Between-Int-Parse-Convert-ToInt-and) string(https://msdn.microsoft.com/ko-kr/library/s8s7t687.aspx) Variable Scope in Lambda Expressions(https://msdn.microsoft.com/ko-kr/library/bb397687.aspx) FileStream(http://stackoverflow.com/questions/4614318/whats-the-difference-between-a-streamwriter-and-a-binarywriter)