Posts

Showing posts from August, 2021

C#--取得類型的方法--object.GetType() v.s. typeof(object) ...

 C#--取得類型的方法--object.GetType() v.s. typeof(object)  ... 在這篇文章,我要介紹有關取得類型的關鍵字。  這些事我上網查到的我盡量用簡單敘述來詳細地描述複雜的事。 目錄:  C#--取得類型的方法  C#--比較相同類型的方法  C#--判斷為某種類型的方法  C#--typeof(object)  C#--object.GetType()  C#--is keyword  C#--not keyword  C#--in keyword  NOTE  C#--取得類型的方法 way1:typeof(object) way2:object.GetType()  C#--比較相同類型的方法 way1:typeof(object1)==typeof(object2) way2:typeof(object1)==object2.GetType()  C#--判斷為某種類型的方法 way1:typeof(object1)==typeof(int) way2:typeof(int)==object1.GetType() way3:int x=object1 as int; if(x!=null)  C#--typeof(object) 獲取在編譯時指定的類型名,object 必須為static class,不能為nonstatic class(被具體化的類別)。 C#--object.GetType() 獲得一個實例在運行時的類型,不一定為nonstatic class。 C#--as keyword B=A as int  A會試著被轉成 int ,當A無法被轉成int,B會是null。  C#--is keyword object1 is object2 判斷object1 是object2 的子類別,判斷object1 是object2 的孩子。 more details on: compiler construction - Why isn't there "is not" keyword in c#? - Stack Overflow ...

Format a string -- String.Format()

Format a string -- System.String.Format() System.String.Format() can format a string with variables. syntax: {idx,alignment:formatString} parameter: idx: the index of the variable which is matched. alignment: alignment for the string, it can determine the string will be left-aligned or right-aligned ...etc. formatString: target, what string want to be formatted. t e.g.1 //C#  static void Main(string[] args)         {             int age = 20;             int score = 100;             int height = 160;             Console.WriteLine("{0} {1}",age,score);         } /* output: 20 100 */ e.g.2 //C#  static void Main(string[] args)         {             int age = 20;             int score = 100;             int height ...

verbatim identifier character(逐字字串標識符字元)-@

verbatim identifier character(逐字字串標識符字元)-@ sign:'@'   @  special character serves as a verbatim identifier. usage: The  @  character prefixes a code element that the compiler is to interpret as an identifier rather than a C# keyword resources: more details on @ - C# Reference | Microsoft Docs

String Interpolation(字串內插)

String Interpolation(字串內插) Q:What is string interpolation? A:string interpolation is a way that replaces the value with variable into string. So the string is not always same. the variable can determine it. Q:What is the form of string interpolation? A:dollar sign before quotation. use dollar sign  with curly bracket. Compiler will replace it with variable. syntax: such as the following code. //C# int age=20; string name="Jay"; //Compiler will replace {name} into variable name as Jay. Console.WriteLine($"my name is {name}, I am {age} years old."); /* output: my name is Jay, I am 20 years old." */ NOTE: Please pay a lot of attention on the notice. According to the website  $ - 字串內插 - C# 參考 | Microsoft Docs ,  version which is under C# 6.0  will NOT support this way. resources: more details on $ - 字串內插 - C# 參考 | Microsoft Docs String.Format Method (System) | Microsoft Docs

C# -- Console.Read() v.s. Console.ReadKey() v.s. Console.ReadLine()

C# -- Console.Read() v.s. Console.ReadKey() v.s. Console.ReadLine()  Console.ReadKey(): [syntax]   public static ConsoleKeyInfo   Console.ReadKey(bool key=false); [description] Read a character from your keyboard. [argument] key: By Default, key is set to be false. When arg. key was not given or key==false , Console.ReadKey() will NOT display in Console.  Otherwise, when key==true. It will display in Console. [return type] ConsoleKeyInfo [usage] (1)not continue the process until the user press any key. I can write the following code  Console.ReadKey(); (2)not continue the process until the user press special key. using while() loop. If I wanna to enter 'E' to continue the process. I can write the following code: while ( Console.ReadKey().Key!=ConsoleKey.E){} Console.Read(): [syntax] [ System.Runtime.Versioning.UnsupportedOSPlatform( "browser" ) ] public static int Read ( ) ; [description] return next character from instream (i.e. input buffer stream) [return...