C#從零開始_自學C#、Visual Studio實境秀 39/ Getting Started with C# Lesson 3~8 of 20



{

9:17:10 Lesson 3 of 20 Working with Strings 字串處理 http://bit.ly/2tr7tf0

{

9:49:00 Null values are frequent sources of bugs even for experienced programmers.  人有失手,馬有亂蹄

Be wary of assuming that variables are not null, especially when working with parameters or other variables you're not responsible for directly instantiating.

5:00:00 7:00:00 If you try to work with the members of an object that isn't set (one that is null), your program will throw a NullReferenceException with a message "Object reference not set to an instance of an object". These are very common, and often very frustrating, errors in C# applications. You can mitigate them by ensuring you're always setting a default value to your instances rather than allowing them to remain null.

8:00:00 You can check to see if a string value is null by comparing it to null (for instance, (nullstring == null) would evaluate to true).  即 VBA.IsNull()

36:30 Both (String.IsNullOrEmpty(emptyString)) and (String.IsNullOrEmpty(nullString)) would return true as well.

37:30 pattern in C#

38:40 static extension methods

39:20 You will learn more about extension methods in Defining and Calling Methods lesson. 擴充方法

43:00 Note that if you need to build up a string by performing many dozens or more concatenations, the allocating and deallocating of so many instances can become a performance issue.

字串處理與效能的問題

44:30 In that case, there is a special type called StringBuilder that can be used more efficiently for this purpose.

文字字串格式化 Strings include many built-in formatting functions

1:01:00 To use string formatting, you specify a format string, which includes special placeholder values, and pass the format string and the replacement values to the Format method.

46:40 When accepting user input, it can be useful to trim it, so that it fits into a fixed amount of space,

The Trim methods remove whitespace characters from the start and/or end of a string:

51:00 Substring 53:15 SubString is a very useful String method that starts from a given position (where 0 is the start of the string)

51:40 很多 method 可以串聯在一起用 multiple methods are chained together,

56:40 $ string interpolation 插值 字串插值 you could construct such a string using $"Hello {name}! syntax. This is known as string interpolation, and is a new feature in C# 6. The same string can also be constructed using concatenation ("Hello " + name + "!").

http://bit.ly/2gVHwTa 字串插值還是比較推薦的寫法 Of all of the above, the string interpolation approach (greet1) is recommended.

It's clean, readable, and generally has better performance than the other approaches.However, using format strings or templates is a good option if you need to store the template separately from the scope of the variables that will be used during runtime value replacement.

}

1:13:00 Lesson 4 of 20 Working with Dates and Times http://bit.ly/2gVPrQd

{

1:14:00 C# has built-in support for dates and times in the form of the System.DateTime type.

1:16:40 var currentTime = DateTime.Now; // current time

var today = DateTime.Today; // current date - time is midnight

var someDate = new DateTime(2016,7,1); // 1 July 2016, midnight

var someMoment = new DateTime(2016,7,1,8,0,0); // 1 July 2016, 08:00.00

var tomorrow = DateTime.Today.AddDays(1);

var yesterday = DateTime.Today.AddDays(-1);

var someDay = DateTime.Parse("7/1/2016"); 可判斷一個字串是否為時間,且轉成其時間表示 測試 寫成 「"2016/7/1"」 也可以互相呼叫調用。

1:24:20 Each of the above methods creates an instance of a DateTime. 和字串類似,也是建立一個 instance

1:26:30 Formatting Dates and Times 日期時間的格式化 Learn more about custom date and time format strings. http://bit.ly/2vzuTjP

1:28:50 Getting to Parts of Dates and Times

var someTime = new DateTime(2016,7,1,11,10,9); // 1 July 2016 11:10:09 AM

int year = someTime.Year; // 2016

int month = someTime.Month; // 7

int day = someTime.Day; // 1

int hour = someTime.Hour; // 11

int minute = someTime.Minute; // 10

int second = someTime.Second; // 9

1:30:50 計算時間差、時間間隔、時間長 Calculating Durations Between DateTimes

計算今年還有幾天:

DateTime nextYear = new DateTime(DateTime.Today.Year+1, 1, 1);

TimeSpan duration = nextYear - DateTime.Today;

Console.WriteLine($"There are {duration.TotalDays} days left in the year");

1:38:40 TimeSpan, which is used to represent a span of time, or a duration

2:38:00 TimeSpan 時間差 的善用。想到時間差,一定要用 TimeSpan 就對了!

}

2:32:40 2:53:40 Lesson 5 of 20 Making Decisions in Your Program //http://bit.ly/2vPLqzh

{

2:56:00 if statement

3:11:10 It's a good idea to follow a standard code formatting convention when programming.

3:28:20 It's generally a good idea to try to limit how deeply you do such nesting, as complex hierarchies of conditional logic are common sources of bugs in software.

3:30:00 排序的演算法 sorting algorithms often use comparison logic to determine which of two values is greater, or if they're equal, like this:

3:36:00 If you find that you have more than 3 independent options in your conditional, you should consider using a different conditional structure: the switch statement.

Switch Statements

3:38:00 case 子句可以附加條件判斷式  要和 when 關鍵字一起使用 http://bit.ly/2tsH6Fw (C# 7.0 新功能介紹 http://bit.ly/2uUwmnO)

when () 要加括號,也可以不加

3:43:30 從 7 開始,case 標籤不再需要互斥,而且 case 標籤在 switch 陳述式中的出現順序可以判斷要執行的 switch 區塊。

4:08:00 swich case break; Each section must not allow execution to continue to the next section. This is usually achieved by using the break keyword to break out of the switch statement, but you can use other statements (like return) as well.

4:10:30 break 的作用 。所以,當有多條件要做同一件事時(執行同一個指令時),則可以省去 break ,然後把要做的事放在最後一個 case 末端,便能達成類似在 VBA 中的 case a,b,c…… (a,b,c……數種條件)這樣的語句功能。也就是說,在 C# 裡 swich case 沒有橫式的多條件寫法,而有直式的。

4:34:00 開始練習試作

}

5:09:40 Lesson 6 of 20 Implementing Logical Expressions http://bit.ly/2vAhMyN

{

Logical expressions are composed of operators and operands.You can define your own operators in C#, but that's a more advanced topic. C# provides many built-in operators that you can use to create logical expressions, which are useful when implementing conditional statements,

boolean expressions 邏輯運算式

To test if a value is between two numbers, you need to check the two conditions separately. The following is not a legal C# expression:

Comparison Operators

5:16:10 logical operators.

&&  // logical AND

||  // logical OR

!   // logical NOT (often read as 'bang')

^   // logical XOR (exclusive OR) 互斥的或 or 5:19:50

true ^ true     // false

true ^ false    // true

false ^ false   // false 類似兩個作對的 反對黨 黨爭

5:21:00  bitwise logical operators, & (AND), | (OR).

These are used to perform binary comparisons of numeric values, and generally aren't used directly for conditional expressions.

The ^ (XOR) operator can be used with both boolean and integral operands.

5:28:00 Flags

A boolean (bool) variable is often referred to as a flag.

Flags can be useful as a means giving a name to a particular condition.

Flags can be attached to objects as properties, such that you can test for their value as part of the object itself (example: if (x.IsPositive).

flags 對物件導向程式設計的封裝性是有破壞性的:However, when writing object-oriented programs, it's often better to avoid using flags, since they can lead to program designs that are more procedural and don't encapsulate behavior effectively within objects. You'll learn more about object design in the Encapsulation and Object-Oriented Design lesson.

5:34:50 It's a typical convention to name boolean variables with an "Is" or "is" prefix,

Avoid naming flags negatively (example: "IsNotPositive"), since this can become confusing, especially when the flag is negated with the ! operator.

5:41:00 練習 6:06:00 成功

}

6:06:50 Lesson 7 of 20 Looping Based on a Logical Expression http://bit.ly/2uP69Xt

{

Expression-Based Loops

the while loop,

 A while loop in C# looks identical to an if statement,

6:19:20 Exiting a Loop 離開迴圈

6:20:50 You can also exit a loop by using the break keyword.

6:22:10 Sometimes you want execution to get out of the loop now, and break is the best way to do that.

You can also use the return keyword to exit a loop.

6:23:10 也可以藉由 throw 來離開迴圈 You can also exit a loop by throwing an exception.

6:24:40 If you suspect that your console program has entered an infinite loop, you can force it to end by pressing [Ctrl]+c. 強制結束迴圈的方法 快速鍵

6:27:30 Sometimes, an infinite loop is intentional.

6:28:20 while (true) // this sets up an infinite loop, since true will always evaluate to true

6:33:00 6:34:40 Keep this relationship between if and while in mind as you write your C# programs.

6:37:30 if 和 while 之間的關係.如何寫一個條件循環式的思路

}

7:11:20 Lesson 8 of 20 Looping a Known Number of Times http://bit.ly/2up94VD

{

For Loops

7:16:30 The loop control variable used most often is an integer variable with the name i. This is a common convention used across languages in programming.

i 應就是 item 的縮寫

7:22:30 用不同的增量來遞增計數器 Counting Up By Different Increments

7:28:30  +=, is shown. This operator is shorthand for "add a value to this variable and assign the sum back to the variable itself", So i+=2 is equivalent to i = i + 2. And, of course, i+=1 would be equivalent to i++.

}

}


留言

熱門文章