C#從零開始_自學C#、Visual Studio實境秀 43/ Getting Started with C# Lesson 14~16 Wo...



{

Lesson 14 of 20 Encapsulation and Object-Oriented Design http://bit.ly/2vcSNon

{

1:00 練習

6:40 雖然只有 get 沒有 set  但在同一個 class 裡頭,仍可以用程式碼設定屬性

53:00 static 就像 VBA 裡會記下原來的值,若不是 static 才會初始化歸零。 static 就是屬於 class 的東西,所以如果像這樣宣告,static IList<Order> Orders { get; set; } = new List<Order>(); 則叫出來的 Orders 會是全部依此 class 建構的 object 的總合,好像公司員工數這樣的性質,是屬於公司的總數,而不是員工個人

而用 ReadOnlyCollection 建構時,一定要是 static 的變量、欄位。所以 就是之前學的 靜態欄位 要在靜態建構子裡初始化才行,否則不會初始化歸零

{

using System;

using System.Collections.Generic;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(new Order("123"));

customer1.addOrder(new Order("234"));

customer1.addOrder(new Order("345"));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"));

customer2.addOrder(new Order("200"));

customer2.addOrder(new Order("300"));



var customers = new List<Customer>() { customer1, customer2 };



// print customers      

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

customer.printOrders();

}

Console.WriteLine($"Customer 1 Order Count: {customer1.OrdersCount}");

Console.WriteLine($"Customer 2 Order Count: {customer2.OrdersCount}");      

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder)

{

Orders.Add(addYourOrder);

}

List<Order> Orders { get; } = new List<Order>();//如果用 static 欄位 它提取的是整個 class,也就是所有 class 執行個體的總合集成,所以一定要非靜態 沒有 static 才行正確記錄或顯示每個 customer 的 orders

public void printOrders()

{

foreach (Order o in Orders)

{

Console.WriteLine(o.OrderNumber);

}

}

//public int OrdersCount{ get { return Orders.Count; } }  

public int OrdersCount => Orders.Count;//lambda // expression bodied member

//static IList<Order> Orders { get; set; } = new List<Order>(); //下式須靜態欄位來初始化,而靜態欄位卻無法在每個執行個體首次呼叫時歸零

//public ReadOnlyCollection<Order> yourHistoricOrders = new ReadOnlyCollection<Order>(Orders);

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

}



}

{ // 這是用 copy (new)的方法

using System;

using static System.Console;

using System.Collections.Generic;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(new Order("123"));

customer1.addOrder(new Order("234"));

customer1.addOrder(new Order("345"));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"));

customer2.addOrder(new Order("200"));

customer2.addOrder(new Order("300"));



var customers = new List<Customer>() { customer1, customer2 };



// print customers      

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

foreach (Order o in customer.Orders)

{

WriteLine(o.OrderNumber);

customer.Orders.Remove(o);//remove不掉的,因為 Orders=> new List<Order>(_Orders)

}

}

Console.WriteLine($"Customer 1 Order Count: {customer1.Orders.Count}");

Console.WriteLine($"Customer 2 Order Count: {customer2.Orders.Count}");      

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder)

{

_Orders.Add(addYourOrder);

}

List<Order> _Orders { get; } = new List<Order>();

public List<Order> Orders=> new List<Order>(_Orders);//複製一個複本

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

}



}

{// 這是用 ReadOnlyCollection 須 using System.Collections.ObjectModel;

using System;

using static System.Console;

using regpx = System.Text.RegularExpressions;

using System.Collections.Generic;

using System.Collections.ObjectModel;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(new Order("123"));

customer1.addOrder(new Order("234"));

customer1.addOrder(new Order("345"));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"));

customer2.addOrder(new Order("200"));

customer2.addOrder(new Order("300"));



var customers = new List<Customer>() { customer1, customer2 };



// print customers      

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

foreach (Order o in customer.yourHistoricOrders)

{

WriteLine(o.OrderNumber);

//customer.yourHistoricOrders.Remove(o);//沒有remove這個方法!

}

}

Console.WriteLine($"Customer 1 Order Count: {customer1.yourHistoricOrders.Count}");

Console.WriteLine($"Customer 2 Order Count: {customer2.yourHistoricOrders.Count}");

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder)

{

Orders.Add(addYourOrder);

}

List<Order> Orders { get; } = new List<Order>();

public ReadOnlyCollection<Order> yourHistoricOrders => new ReadOnlyCollection<Order>(Orders);//我們用 lambda expression bodied member //老師是用 get :

/* private List<Order> _orders = new List<Order>();//所以關鍵只是要用屬性,而非欄位。欄位初始化式 initializers 是不能用非靜態的欄位、屬性等來設定 ,可是屬性卻可以

private ReadOnlyCollection<Order> _ordersView;

public ReadOnlyCollection<Order> Orders

{

get

{

if (_ordersView == null)

{

_ordersView = new ReadOnlyCollection<Order>(_orders);

}

return _ordersView;

}

}

*/

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

}



}

{ 2:41:00 //還可以用 IReadOnlyCollection 這個 interface,就不須 using System.Collections.ObjectModel; 這是自己摸索來的,老師沒提到

using System;

using static System.Console;

using System.Collections.Generic;

using System.Linq;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(new Order("123"));

customer1.addOrder(new Order("234"));

customer1.addOrder(new Order("345"));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"));

customer2.addOrder(new Order("200"));

customer2.addOrder(new Order("300"));



var customers = new List<Customer>() { customer1, customer2 };



// print customers              

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

foreach (Order o in customer.Orders)

{

WriteLine(o.OrderNumber);

//o.Remove(o);//沒有remove這個方法!

}

}

Console.WriteLine($"Customer 1 Order Count: {customer1.Orders.Count()}");

Console.WriteLine($"Customer 2 Order Count: {customer2.Orders.Count()}");

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder)

{

_Orders.Add(addYourOrder);



}

List<Order> _Orders { get; } = new List<Order>();

public IReadOnlyCollection<Order> Orders => new List<Order>(_Orders);

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

}



}

{ 2:53:00 ToList() 須 using System.Linq;

using System;

using static System.Console;

using System.Collections.Generic;

using System.Linq;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(new Order("123"));

customer1.addOrder(new Order("234"));

customer1.addOrder(new Order("345"));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"));

customer2.addOrder(new Order("200"));

customer2.addOrder(new Order("300"));



var customers = new List<Customer>() { customer1, customer2 };



// print customers              

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

var orders = customer.Orders.ToList(); // ToList creates a new list and populates it

foreach (Order o in orders)

{

WriteLine(o.OrderNumber);

orders.Remove(o); //仍可以修改,但動不到原來的  Orders,而且修改後,再跑迴圈,會出現集合已被修改的錯誤

}

}

Console.WriteLine($"Customer 1 Order Count: {customer1.Orders.Count()}");

Console.WriteLine($"Customer 2 Order Count: {customer2.Orders.Count()}");

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder)

{

_Orders.Add(addYourOrder);      

}

List<Order> _Orders { get; } = new List<Order>();//_加底線可能就是為了表示是 private ,並不 expose 的!

public IReadOnlyCollection<Order> Orders => new List<Order>(_Orders);  

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

}



}

{// 這是用 IEnumerable AsEnumerable 須 using System.Linq;

using System;

using static System.Console;

using System.Collections.Generic;

using System.Linq;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(new Order("123"));

customer1.addOrder(new Order("234"));

customer1.addOrder(new Order("345"));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"));

customer2.addOrder(new Order("200"));

customer2.addOrder(new Order("300"));



var customers = new List<Customer>() { customer1, customer2 };



// print customers      

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

foreach (Order o in customer.yourHistoricOrders)

{

WriteLine(o.OrderNumber);

//customer.yourHistoricOrders.Remove(o);//沒有remove這個方法!

}

}

Console.WriteLine($"Customer 1 Order Count: {customer1.yourHistoricOrders.Count()}");

Console.WriteLine($"Customer 2 Order Count: {customer2.yourHistoricOrders.Count()}");

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder)

{

Orders.Add(addYourOrder);

}

List<Order> Orders { get; } = new List<Order>();

public IEnumerable<Order> yourHistoricOrders => Orders.AsEnumerable();

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

}



}

練習成功{

using System;

using static System.Console;

using System.Collections.Generic;

using System.Linq;

public class Program

{

public static void Main()

{

var customer1 = new Customer("Steve");

customer1.addOrder(null,DateTime.Now);

customer1.addOrder(new Order("234"),DateTime.Now.AddSeconds(-1));

customer1.addOrder(new Order("345"),DateTime.Now.AddSeconds(-2));

customer1.addOrder(new Order("234"),DateTime.Now.AddSeconds(-3));

customer1.addOrder(new Order("234"),DateTime.Now.AddSeconds(-4));



var customer2 = new Customer("Eric");

customer2.addOrder(new Order("100"),DateTime.Now.AddSeconds(-5));

customer2.addOrder(new Order("200"), DateTime.Now.AddDays(6));

customer2.addOrder(new Order("300"), DateTime.Now.AddSeconds(-7));



var customers = new List<Customer>() { customer1, customer2 };



// print customers              

foreach (var customer in customers)

{

Console.WriteLine(customer.Name);

Console.WriteLine("Orders:");

var orders = customer.Orders.ToList(); // ToList creates a new list and populates it

foreach (Order o in orders)

{

if(o.OrderNumber== "234")  o.OrderDate = DateTime.Now.AddDays(10);//Orders should expose an OrderDate (which can be read/write)

WriteLine($"{o.OrderNumber},{o.OrderDate}");

}

}

Console.WriteLine($"Customer 1 Order Count: {customer1.Orders.Count()}");

Console.WriteLine($"Customer 2 Order Count: {customer2.Orders.Count()}");

}

}

public class Customer

{

public Customer(string name)

{

Name = name;

}

public string Name { get; }

public void addOrder(Order addYourOrder,DateTime orderdate)

{//Trying to add an Order with an existing OrderNumber should replace the existing Order (not add a duplicate)

if (addYourOrder == null) return;//Trying to add a null Order should do nothing

if (orderdate> DateTime.Now) return;  //Trying to add an order with an OrderDate in the future should do nothing

if (_Orders.Count>0)//Trying to add an Order with an existing OrderNumber should replace the existing Order (not add a duplicate)

{

foreach(Order o in _Orders)//集合被修改再讀取便會錯誤

{

if (addYourOrder.OrderNumber == o.OrderNumber)

{ _Orders.Remove(o); break; }

}

}

addYourOrder.OrderDate = orderdate;

_Orders.Add(addYourOrder);

}

List<Order> _Orders { get; } = new List<Order>();//_加底線可能就是為了表示是 private ,並不 expose 的!

public IReadOnlyCollection<Order> Orders

{//Customers have a property exposing their historic Orders

get { return new List<Order>(_Orders); }

}

}

public class Order

{

public Order(string orderNumber)

{

OrderNumber = orderNumber;

}

public string OrderNumber { get; }

public DateTime OrderDate { get; set; }

}



}

1:58:40 程式碼片段 http://bit.ly/2vaHX1N



}

Lesson 15 of 20 Understanding Namespaces http://bit.ly/2tQ5a5D

{

0:10 If more than one class of the same name is included in the same scope, you will need to disambiguate the scenario by specifying all or part of the namespace. In our example, NamespaceLesson.Models.Person would be enough.

You can specify a special name to use for one of your duplicately named classes in the current scope with a special type of using statement.

using NamespacePerson = GettingStartedTutorials.CSharp.NamespaceLesson.Models.Person; will let you use NamespacePerson in your program, so C# knows which Person class you intend to use.

0:30 類似 alias 的用法,但此是用在 class , alias 是用在 namespace

}

3:49:00 文件字頻。Word VBA 字頻改善,清除符號效能問題。 4:01:50 4:23:00 改用 find.execute

4:03:50 6:31:50 Lesson 16 of 20 Introducing LINQ http://bit.ly/2hhWy5C

{

Language-Integrated Query, or LINQ, is a way to query a set of data with the use of extension methods.

These extension methods can only be accessed by adding the using System.Linq; statement.

The following material builds upon the Working with Arrays and Collections lesson and the Extension Methods section of the Defining and Calling Methods lesson.



}

}


留言

熱門文章