샘플코드로 보는 C#기능들

Constraints on Type Parameters

public static void OpTest<T>(T s, T t) where T : class
{
   System.Console.WriteLine(s == t);
}
static void Main()
{
   string s1 = "target";
   System.Text.StringBuilder sb =
               new System.Text.StringBuilder("target");
   string s2 = sb.ToString();
   OpTest(s1, s2);
}

Anonymous Types

var v = new { Amount = 108, Message = "Hello" };
Console.WriteLine(v.Amount + v.Message);

Lambda Expressions

delegate int del(int i);
static void Main(string[] args)
{
   del myDelegate = x => x * x;
   int j = myDelegate(5); //j = 25
}

LINQ Query Expressions

class LINQQueryExpressions
{
   static void Main()
   {

      // Specify the data source.
      int[] scores = new int[] { 97, 92, 81, 60 };

      // Define the query expression.
      IEnumerable scoreQuery =
         from score in scores
         where score > 80
         select score;

      // Execute the query.
      foreach (int i in scoreQuery)
      {
         Console.Write(i + " ");
      }
   }
}
// Output: 97 92 81

yield

using System.Collections;

public class PowersOf2
{
   static void Main()
   {
      // Display powers of 2 up to the exponent of 8:
      foreach (int i in Power(2, 8))
      {
         Console.Write("{0} ", i);
      }
   }

   static IEnumerable Power(int number, int exponent)
   {
      int result = 1;

      for (int i = 0; i < exponent; i++)
      {
         result = result * number;
         yield return result;
      }
   }

   // Output: 2 4 8 16 32 64 128 256
}

var

var i = 10; // implicitly typed
int i = 10; //explicitly typed

using statement

using ASimpleName = Dictionary<string, Dictionary<string, List>>;

using block

using (DbTransaction tran = new DbTransaction())
{
   DoQuery("...");
   DoQuery("...");
}

volatile

class IfYouThinkYouUnderstandVolatile
{
   volatile int x, y;

   void Test1() // Executed on one thread
   {
      x = 1; // Volatile write (release-fence)
      int a = y; // Volatile read (acquire-fence)
      ...
   }

   void Test2() // Executed on another thread
   {
      y = 1; // Volatile write (release-fence)
      int b = x; // Volatile read (acquire-fence)
      ...
   }
}

?? operator

class NullCoalesce
{
   static int? GetNullableInt()
   {
      return null;
   }

   static string GetStringValue()
   {
      return null;
   }

   static void Main()
   {
      // ?? operator example.
      int? x = null;

      // y = x, unless x is null, in which case y = -1.
      int y = x ?? -1;

      // Assign i to return value of method, unless
      // return value is null, in which case assign
      // default value of int to i.
      int i = GetNullableInt() ?? default(int);

      string s = GetStringValue();
      // ?? also works with reference types.
      // Display contents of s, unless s is null,
      // in which case display "Unspecified".
      Console.WriteLine(s ?? "Unspecified");
   }
}

Verbatim string literals

"c:\\program files\\oldway"
@"c:\program files\newway"

object initializers

Employee emp = new Employee();
emp.Name = "John Smith";
emp.StartDate = DateTime.Now();

=>

Employee emp = new Employee {
      Name="John Smith", StartDate=DateTime.Now()
   }

#if DEBUG

[Conditional("DEBUG")]

static constructors

public class Example
{
   static Example()
   {
      // Code to execute during type initialization
   }

   public Example()
   {
      // Code to execute during object initialization
   }
}

Conditional string Format

string s = string.Format("{0:positive;negative;zero}", i);

string format = "000;-#;(0)";

string pos = 1.ToString(format); // 001

string neg = (-1).ToString(format); // -1

string zer = 0.ToString(format); // (0)

checked and unchecked

short x = 32767; // 32767 is the max value for short
short y = 32767;
int z1 = checked((short)(x + y)); // OverflowException
int z2 = unchecked((short)(x + y)); // will return -2
int z3 = (short)(x + y); // will return -2
이 글은 language 카테고리에 분류되었고 태그가 있습니다. 고유주소 북마크.

댓글 남기기