Posts

How to Read from a Text File in C#? -- C# -- System.IO.StreamReader

 How to Read from a Text File in C#? -- C# -- System.IO.StreamReader  [Ans] using System.IO.StreamReader() [Syntax]  StreamReader StreamReader(string fileName); where fileName is the name of file that you read [NOTE] Great Importance!!! Please pay a lot attention!!! (1) Compiler reads file under subfiolder of the path \bin\Debug\netcoreapp3.1 in your project. When you use StreamReader, the argument will be given as  <yourProject>\bin\Debug\netcoreapp3.1\<fileName> e.g. When I create a new C# project with project name StreamReader_All in the path C:\Users\jayw7\OneDrive\桌面\CSharp\StreamReader . I should write  C:\Users\jayw7\OneDrive\桌面\CSharp\StreamReader\StreamReader_All\StreamReader_All\bin\Debug\netcoreapp3.1 (2) In C#, "\" means escape character. If you would like to present a directory, you must use "\\" instead of "\" or  add "@" before your directory. How to read all lines from text file in C#? step1: read file from text file w...

Why does compiler throw CS0563 error when I overload == in C# ?-- resolved

 Why does compiler throw CS0563 error when I overload  == in C# ?-- resolved Let's take a look at my code? Before solved: using System; namespace Operator_Overloaded_Comparsion {     class Program     {         public class Base         {             public int num1;             public int num2;             public Base(int n1,int n2)             {                 num1 = n1;                 num2 = n2;             }                      }         static void Main(string[] args)         {             Base b1 = new Base(2,3);             Base b...

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