C#從零開始_自學C#、Visual Studio實境秀 33/ class Anonymous Types interface LINQ ge...
33/ class Anonymous Types interface LINQ generic delegate method signature{
3:00:00 class (C# Reference) http://bit.ly/2tkyrsI
class TestClass
{
// Methods, properties, fields, events, delegates
// and nested classes go here.
}
Only single inheritance is allowed in C#.In other words, a class can inherit implementation from one base class only. However, a class can implement more than one interface.
8:50 Type 和 class 怎麼分
12:40 Classes are internal by default. Members are private by default.
15:30 Generics (C# Programming Guide) http://bit.ly/2u34hr2
22:40 class 翻成 「族類」「型態」或許更達意、更恰當!
43:00 先抓動詞 九陽神功第七招!
9:42:00 KeyEventArgs.Modifiers 屬性 http://bit.ly/2tBNBoZ Gets the modifier flags for a KeyDown or KeyUp event. The flags indicate which combination of CTRL, SHIFT, and ALT keys was pressed.
46:00 Programming Concepts (C#) {//http://bit.ly/2tzKQEt
51:20 說明如何使用運算式樹狀結構來啟用可執行程式碼的動態修改。
52:20 字形結構兼音義 斷句 九陽神功第三招,第一招。
Threading (C#) Provides an overview of the .NET threading model and shows how to write code that performs multiple tasks at the same time to improve the performance and responsiveness of your applications.
Performance Tips Discusses several basic rules that may help you increase the performance of your application.
1:09:50 Object-Oriented Programming (C#) http://bit.ly/2vbUK10
{ 1:15:10 The terms class and object are sometimes used interchangeably, but in fact, classes describe the type of objects, while objects are usable instances of classes. So, the act of creating an object is called instantiation.
1:16:55 Using the blueprint analogy, a class is a blueprint, and an object is a building made from that blueprint.
1:18:00 struct and class :C# also provides a light version of classes called structures that are useful when you need to create large array of objects and do not want to consume too much memory for that.
1:20:00 struct (C# Reference) http://bit.ly/2uu4shI
1:28:00 Each class can have different class members that include properties that describe class data, methods that define class behavior, and events that provide communication between different classes and objects.
1:29:00 Fields and properties represent information that an object contains. Fields are like variables because they can be read or set directly.
1:29:40 Property and field的差別:欄位一如變數,可以直接不受遮攔地被讀取和設定:無遮;而屬性則是「有遮」,一如帶有「遮罩」(輸入遮罩):Properties have get and set procedures, which provide more control on how values are set or returned.
可以說欄位是沒有矜持的,人盡可夫,是口無遮攔的;屬性則是良家婦女,是口有遮攔的
欄位像小孩子;屬性像一個成熟的大人。
1:37:00 自動實作屬性 To define an auto-implemented property:
class SampleClass
{
public int SampleProperty { get; set; }
}
In C#, you can omit the get or set property method.
However, auto-implemented properties cannot be read-only or write-only.
1:43:00 get (C# Reference)
set (C# Reference)
1:52:10 A method is an action that an object can perform.
1:55:50 擴充方法(extension methods ) C# also supports extension methods that allow you to add methods to an existing class outside the actual definition of the class.
Finalizers 2:02:30 Garbage Collection
2:08:10 The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers. For more information about events, how they are raised and handled, see Events.
To declare an event in a class, use the event keyword.
2:11:50 To subscribe to an event, use the += operator; to unsubscribe from an event, use the -= operator.
2:13:30 class 預設為 internal 但是一個巢狀的 class 預設則是 private 因為它是一個 class 的 member:By default, the nested class is private.
2:15:00 To create an instance of the nested class, use the name of the container class followed by the dot and then followed by the name of the nested class:
Container.Nested nestedInstance = new Container.Nested()
2:31:00 Object Initializers with anonymous types Object and Collection Initializers (C# Programming Guide)
2:37:30 靜態的不能去存取、參照、訪問……非靜態的 Static members also cannot access non-static properties, fields or methods.
2:39:40 static (C# Reference) http://bit.ly/2szPGSw
2:45:00 If the static keyword is applied to a class, all the members of the class must be static.
2:50:20 Anonymous Types 匿名型別:
2:53:20 Anonymous Types (C# Programming Guide) http://bit.ly/2tVD6AJ
{Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.
3:24:00 匿名類型 使用時機 :就是不想用已有的 class 的全部成員 (members) 時,不想概括承受某一個 class 時。
The most common scenario is to initialize an anonymous type with properties from another type. In the following example, assume that a class exists that is named Product. Class Product includes Color and Price properties, together with other properties that you are not interested in. ……This causes a smaller amount of data to be returned in the query.
var productQuery =
from prod in products
select new { prod.Color, prod.Price };
foreach (var v in productQuery)
{
Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}
模擬試作成功:
using System;
using System.Collections.Generic;
using System.Linq;
class Product
{
string Color;
int Price;
bool style;
string us;
public Product(string c,int p,bool s,string u)
{
Color = c;Price = p;style = s; us = u;
}
static void Main()
{
var v = new { Amount = 108, Message = "Hello" };
// Rest the mouse pointer over v.Amount and v.Message in the following
// statement to verify that their inferred types are int and string.
Console.WriteLine(v.Amount + v.Message);
p.Products.Add(p.p1); p.Products.Add(p.p2); p.Products.Add(p.p3); p.Products.Add(p.p4);
var pq = from pc in p.Products select new { pc.style, pc.Color};
foreach (var i in pq)
{
Console.WriteLine("{0},{1}", i.Color, i.style);
}
Console.Read();
}
}
class p
{
internal static Product p1 = new Product("red", 1, true, "u");
internal static Product p2 = new Product("blu",2, false, "s");
internal static Product p3 = new Product("gre", 3, true, "u");
internal static Product p4 = new Product("org", 4, false, "s");
internal static List<Product> Products = new List<Product>();
}
4:00:00 匿名類型 不可用來宣告 若要當參數傳遞,則……you can declare the parameter as type object. (您可以將參數宣告為類型物件)
4:05:00 strong typing 強型別(強式類型) 就是型別要明確! http://bit.ly/2vwYolI Specifying data types for all your variables is known as strong typing. Using strong typing has several advantages: http://bit.ly/2uCV9fX
4:19:50 boundary 和 scope 翻成「結界」也很適當傳神! 「界限」http://bit.ly/2vwwlCL
}
4:25:40 Getting Started with LINQ in C# http://bit.ly/2tlf5Uu Language Integrated Query (LINQ)
{
4:33:00 Introduction to LINQ Queries (C#) 其實例子可以用在五子棋的黑子白子判斷上:
class IntroToLINQ
{
static void Main()
{
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
}
}
}
4:43:00
4:45:00 all classes in C# implicitly inherit from the Object class
4:49:00 To specify that a class can be used as a base class only and cannot be instantiated:
public abstract class B { }
4:51:00 virtual (overridable)
4:53:30 new Modifier Hides a member inherited from a base class 前面才學用 anonymous type 去選擇性地「繼承」它「基底」or 「參考」的 class ,這裡又要隱藏。
4:56:10 Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation.
{
4:59:00 To define an interface:
interface ISampleInterface
{
void doSomething();
}
5:01:00 To implement an interface in a class:
class SampleClass : ISampleInterface
{
void ISampleInterface.doSomething()
{
// Method implementation.
}
}
5:03:55 interface (C# Reference) http://bit.ly/2u4ntX7
6:19:00 When a base type list contains a base class and interfaces, the base class must come first in the list. 這句意思是說 class derivedclass : baseclass,Ia,Ib,Ic...
{這句意思是說:比如一個衍生類別DerivedClass的宣告如下:
class DerivedClass : baseclass,Ia,Ib,Ic...
這個冒號(:)後面的「,,,...」就是 base type list 。即此衍生類別的基底根源。
所以「當基底類型清單包含基底類別和介面時,基底類別一定會排在清單的第一個。」 http://bit.ly/2vwSIrF
應該寫成以下方式較明白:
「當一個衍生類別在宣告時,其冒號(:)後的基底類型清單,若包含它的基底類別和它所要實作的介面時,那麼那個基底類別一定會排在這個清單的第一個,即緊挨者冒號來寫。」
否則初學者誰知道什麼是「基底類型清單 a base type list」?
}
An interface contains only the signatures of methods, properties, events or indexers.
可見「signatures」 就是「define」( definition )
{
interface ISampleInterface
{
void SampleMethod();
}
class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
}
static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();
// Call the member.
obj.SampleMethod();
}
}
}
An interface can't contain constants, fields, operators, instance constructors, finalizers, or types. Interface members are automatically public, and they can't include any access modifiers. Members also can't be static. http://bit.ly/2uD8ipj Interfaces (C# Programming Guide)
5:25:00 interface 本身就是多重繼承: An interface can inherit from one or more base interfaces.
5:26:30 base type list
5:27:00 A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
也就是說如果是 explicit 的實作,就要用 interface 的 instance 來取得其 members 來用;若不是 explicit ,就可以不必用 interface 的 instance,而可以用實作它的 class 的 object/instance 來引用。
{ interface ISampleInterface
{
void SampleMethod();
}
class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
}
static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();
// Call the member.
obj.SampleMethod();
}
}}
5:51:00 Explicit Interface Implementation (C# Programming Guide) http://bit.ly/2uxrNPd
5:55:00 5:59:20 所以 interface 和 class 也存在 implicit override 的關係,只要在 class 實作(或繼承)自 interface 而其 members 的名稱又與其所實作(繼承)的 interface 的相同,則隱含 override 了。此時,若以 class 建構一個執行個體,即可調用此一名稱的 members了。此時就不知道,到底是 interface的,還是 class的成員。混同了。只有在依其實作(繼承)的 interface 構建一個 instance時,才能看到區別。
interface 在被 class 實作後 有點像李代桃僵或貍貓換太子。 interface 如只有軀殼,沒有靈魂的皮囊。
abstract vs implementation ; virtual vs override . polymorphism vs inheritance ;
原來就沒有內容的抽象,當然要填充、填實(充實)。原來有資料的,當然就要去覆寫。沒有內容怎麼覆寫。
interface 就是腦袋中的構想, class 是將其構想畫出來成藍圖, object/instance 就是依照這個藍圖去做出來的實物。
8:25:00 Interfaces (C# Programming Guide)
{8:19:00 8:33:40 The interface defines only the signature.In that way, an interface in C# is similar to an abstract class in which all the methods are abstract. http://bit.ly/2u450s9
8:36:00 Interfaces can contain methods, properties, events, indexers, or any combination of those four member types.
An interface can't contain constants, fields, operators, instance constructors, finalizers, or types.
Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.
To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.
8:57:30 An interface is like an abstract base class.
}
}
}
7:39:00 Language Integrated Query (LINQ) LINQ 縮語縮得多好! 九陽神功第一招!斷句 L IN Q 。 http://bit.ly/2vxen2P
9:11:11 Generics http://bit.ly/2vbUK10
{
The most common example of generics is a collection, where you can specify the type of objects to be stored in a collection.
To define a generic class: 9:12:40
Public class SampleGeneric<T> //關鍵在角括弧、尖括弧,且要用T ,表 type
{ //而且 class 預設是 internal,而 generic 則須為 public !
public T Field;
}
To create an instance of a generic class:
SampleGeneric<string> sampleObject = new SampleGeneric<string>(); // () 即建構子
sampleObject.Field = "Sample string";
}
9:21:29 Delegates http://bit.ly/2vbUK10
{
A delegate is a type that defines a method signature,
像 interface 一樣, delegate 只有 signature ,也是純抽象的。只不過 delegate是在 method 的層級,而 interface 是在 class的層級。
9:25:00 and can provide a reference to any method with a compatible signature. 所以一樣只是個 提貨券。
You can invoke (or call) the method through the delegate. 就是委託(委派)你 delegate 去調用(invoke ,call) method 。
Delegates are used to pass methods as arguments to other methods.
9:29:00 Event handlers are nothing more than methods that are invoked through delegates. For more information about using delegates in event handling, see Events.
事件處理常式就是透過委派叫用的方法。 http://bit.ly/2vxRigK 事件處理程序也不過就是方法。而這些方法是用 delegate 去呼叫(call,invoke)的,而不是如一般呼叫方法的方式來引用。
9:33:00 To create a delegate:
public delegate void SampleDelegate(string str);//關鍵字 public delegate void
這 public 不就像 interface 也是必然的 public 嗎?
Method Signatures 9:38:50 http://bit.ly/2vxSDEi
Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.
}
}
}
留言