C# in Depth. Jon Skeet. ch. 1

Dynamic languages suck away fluff from your code, leaving just the essence … Right..

The interesting thing is that few of the features that tend to give dynamic lan- guages their lightweight feel have anything to do with being dynamic.

Statically typed languages don’t have to be clumsy and heavyweight.

C# 1 : Java 와 비슷.  추가적인 것 :: properties as a first-class feature in the language, delegates and events, foreach loops, using statements, explicit method overriding, operator overloading, and custom value types, to name a few

1.1 Starting with a simple data type

목적 :: impress rather than educate

1.1.1 The Product type in C# 1

using System.Collections;
public class Product
{

   string name;
   public string Name { get { return name; } } // getter 가 필요하면 정의해야 함 .. fluff ..

   decimal price;
   public decimal Price { get { return price; } }

   public Product(string name, decimal price)
   {
      this.name = name;
      this.price = price;
   }

   public static ArrayList GetSampleProducts()
   {
      ArrayList list = new ArrayList(); // 뭐가 들어가는 지 모른다.
      list.Add(new Product("West Side Story", 9.99m));
      list.Add(new Product("Assassins", 14.99m));
      list.Add(new Product("Frogs", 13.99m));
      list.Add(new Product("Sweeney Todd", 10.99m));
      return list;</pre>
}
public override string ToString()
 {
 return string.Format("{0}: {1}", name, price);
 }
}

1.1.2 Strongly typed collections in C# 2  __ generic ..

public class Product
{
   string name;
   public string Name
   {
      get { return name; }
      private set { name = value; } // ***
}
decimal price;
public decimal Price
{
      get { return price; }
      private set { price = value; }</pre>
}
public Product(string name, decimal price)
{
 Name = name;
 Price = price;
}

public static List GetSampleProducts() {
 List list = new List();
 // type 명시.. 리스트에서 가져올 때 캐스팅 필요 없음.
 list.Add(new Product("West Side Story", 9.99m));
 list.Add(new Product("Assassins", 14.99m));
 list.Add(new Product("Frogs", 13.99m));
 list.Add(new Product("Sweeney Todd", 10.99m));
 return list;
}
public override string ToString()
{
 return string.Format("{0}: {1}", name, price);
}
}

1.1.3 Automatically implemented properties in C# 3

using System.Collections.Generic;
class Product
{
  public string Name { get; private set; } // 코드, 변수도 없다. Name 은 함수명.
  public decimal Price { get; private set; }
  public Product(string name, decimal price)
  {
    Name = name;
    Price = price;
  }
  Product() {}
  public static List GetSampleProducts()
  {
    return new List
    {
      new Product { Name="West Side Story", Price = 9.99m },
      new Product { Name="Assassins", Price=14.99m },
      new Product { Name="Frogs", Price=13.99m },
      new Product { Name="Sweeney Todd", Price=10.99m}</pre>
 };
 }
 public override string ToString()
 {
 return string.Format("{0}: {1}", Name, Price); }
 }
}

1.1.4 Named arguments in C# 4

readonly decimal price;
public decimal Price { get { return price; } }
public Product(string name, decimal price)
{
  this.name = name;
  this.price = price;
}
new Product(name: "West Side Story", price: 9.99m);  // 이렇게 인수 이름을 붙여 초기화.

1.2 Sorting and filtering

1.2.1 Sorting products by name

ArrayList products = Product.GetSampleProducts(); // C# 1</pre>
List products = Product.GetSampleProducts(); // C# 2
List products = Product.GetSampleProducts(); // C# 3
products.Sort(delegate(Product x, Product y)
 { return x.Name.CompareTo(y.Name); }
);

1.2.2 Querying collections

List products = Product.GetSampleProducts();
Predicate test = delegate(Product p) { return p.Price > 10m; };
Listmatches = products.FindAll(test);
Action print = Console.WriteLine;  // method group conversion (C# 2)
matches.ForEach(print);
// Differently ...
List products = Product.GetSampleProducts();
products.FindAll(delegate(Product p) { return p.Price > 10;}) .ForEach(Console.WriteLine);  // 노이지 하다. { } 딜리깃.
// C# 3
List products = Product.GetSampleProducts();
foreach (Product product in products.Where(p => p.price > 10)) {
  Console.WriteLine(product);
}

1.3 Handling an absence of data

1.3.1 Representing an unknown price

Nullable
decimal? price;
public decimal? Price
{
  get { return price; }
  private set { prie = value; }
}
..
Price == null // 조건식..

1.3.2 Optional parameters and default values

public Product(string name, decimal? price = null)
{
  this.name = name;
  this.price = price;
}

1.4 Introducing LINQ

C# 3..

1.4.1 Query expressions and in-process queries

List products = Product.GetSampleProducts();
List suppliers = Supplier.GetSampleSuppliers();
var filtered = from p in products
               join s in suppliers
                   on p.SupplierID equals s.SupplierID
               where p.Price > 10
               orderby s.Name, p.Name
               select new { SupplierName = s.Name, ProductName = p.Name };
foreach (var v in filtered)
{
    Console.WriteLine("Supplier={0}; Product={1}",
                 v.SupplierName, v.ProductName);
}

1.4.2 Querying XML

1.4.3 LINQ to SQL

..

1.5 COM and dynamic typing

c# 4 : Com, DLR (Dynamic Language Runtime)

1.5.1 Simplifying COM interoperability

1.5.2 Interoperating with a dynamic language

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.ExecuteFile("FindProducts.py");
dynamic products = scope.GetVariable("products");
foreach (dynamic product in products)
{
  Console.WriteLine("{0}: {1}", product.ProductName, product.Price);
  // 여기서 에러가 날지도 모르지만.. 어떻게든 진행함.
}

1.6 Dissecting the .NET platform

1.6.1 C#, the language

IL -> JIT compiler.

1.6.2 Runtime

런타임 부분은 IL 의 작은 부분..  CLI : Common Language Infrastructure, CLI 의 런타임은 CLR ( ~~ ~~ 런타임) 으로 불림..  이 책에서는 마소 의 구현을 가리킴.
언어의 일부는 런타임 레벨에 보이지 않지만, 다른 것들은 구분을 넘는다.  … 어레이와 딜리깃은 런타임에 중요.

1.6.3 Framework libraries

 

__

Advertisements