using System; //使用System的命名空間
/*--------------------------------------------------------------------------------------------------*/
namespace First
{
class MyClass
{
}
}
namespace Second
{
class MyClass
{
}
}
/*--------------------------------------------------------------------------------------------------*/
public static void Main(string[] args) //主程序
{
}
/*--------------------------------------------------------------------------------------------------*/
//變數(Variable),儲存數據的名稱
int userAge; //宣告整數
byte userAge; //宣告位元組
float pi = 3.142f; //宣告32位元浮點值
double pi = 3.142; //宣告64位元的浮點值
decimal pi = 3.142m; //宣告較高精準度的浮點
char c = 'A'; 宣告字元
bool b = true; //宣告布林值 true 或者 false
/*--------------------------------------------------------------------------------------------------*/
//Camel Casing
string thisIsAVariableName; // 駝峰式大小寫
//underscores to separate words
string this_is_a_variable_name; //將不同單字用下底線隔開
/*--------------------------------------------------------------------------------------------------*/
//初始化變數
byte = userAge = 20;
int numberOfEmployees = 510;
double numberOfHours = 5120;
float hourlyRate = 60.0f;
decimal income = 25399.65m;
char grade = '#';
bool isLoggedIn = true;
byte level = 2, userExperience = 5;
byte year;
year = 20;
/*--------------------------------------------------------------------------------------------------*/
int x = 5; //一個等於是將等號右邊的值複製給等號左邊
int y = 10;
x = y; //將 y 的值複製到 x
Console.WriteLine(x); //顯示 10
/*--------------------------------------------------------------------------------------------------*/
//基本的運算符號
int x = 7, y = 2;
Console.WriteLine( x + y ); // 9
Console.WriteLine( x - y ); // 5
Console.WriteLine( x * y ); // 14
Console.WriteLine( x / y ); // 3
Console.WriteLine( x % y ); // 1
double x = 7;
int y = 2;
Console.WriteLine( x / y ); // 3.5
/*--------------------------------------------------------------------------------------------------*/
int x = 10;
x = x + 2; // 12
x += 2;
x -= 2;
x++; //先賦值在運算
++x; //先運算在賦值
/*--------------------------------------------------------------------------------------------------*/
//類型的轉換
int x = (int)20.9;
float num1 = (float)20.9;
decimal num2 = (decimal)20.9;
/*--------------------------------------------------------------------------------------------------*/
//數組(Arrary)
int [] userAge = {21, 22, 23, 24, 25};
int [] userAge2;
userAge2 = new[] {21, 22, 23, 24, 25};
int [] userAge3 = new int[5]; // {0, 0, 0, 0, 0}
//index
userAge3[0] = 31; // {31, 0, 0, 0, 0}
userAge3[2] = userAge3[2] + 20; // {31, 0, 20, 0, 0}
/*--------------------------------------------------------------------------------------------------*/
int [] userAge = {22, 21, 23, 18, 25};
Console.WriteLine(userAge.Length); //取得數組內有多少筆資料
Array.Sort(userAge); //自動排序數組 {18, 21, 22, 23, 25}
//IndexOf
int [] numbers = {10, 30, 44, 21, 51, 21, 61, 24, 14};
int ans = Array.IndexOf(numbers, 21);
// 3, 尋找是否有此筆資料, 並返回Index, 如果找不到為-1
/*--------------------------------------------------------------------------------------------------*/
//字串
string message = "Hello World";
string message2 = "Hello World, " + " my name is Kent";
int ans = message.Length; //取得字串裡面有多少字元
Console.WriteLine(message.Substring(2)); // "llo World"
Console.WriteLine(message.Substring(2, 5)); // "llo W"
// Substring(2, 5) 起始位置, 字元的長度
/*--------------------------------------------------------------------------------------------------*/
string FirstString = "This is John";
string SecondString = "Hello";
// Equals 比較字符串是否相同
FirstString.Equals("This is John"); // true
FirstString.Equals(SecondString); // false
/*--------------------------------------------------------------------------------------------------*/
//將字串分開
char [] separator = { ' , ' , ' ; ' };
string fruits = "Apple, Banana; Dureian, , Mango";
string [] substrings = fruits.Split(separator);
Console, WriteLine(substrings);
/*--------------------------------------------------------------------------------------------------*/
//列表(Lists)
List<int> userAgeList = new List<int>();
List<int> userAgeList = new List<int>{11, 21, 31, 41};
//Add, 將資料加入List的最後
userAgeList.Add(51);
userAgeList.Add(61);
//Insert, 將資料加入指定的Index
userAgeList.Insert(2, 51);
//Remove, 將第一次找到的指定資料移除
userAgeList.Remove(51);
//RemoveAt, 將指定的Index移除
userAgeList.Remove(2);
//Contains, 檢查資料是否在List裡,並回傳布林值
userAgeList.Contains(51);
//Clear, 將List裡的資料全部清除
userAgeList.Clear();
/*--------------------------------------------------------------------------------------------------*/
// value type
int myNumber = 5;
//reference
string message = "Hello";
沒有留言:
張貼留言