[技術類 - C# 學習誌 - C#面試考題] 變數、資料型態、運算子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = 32;
long b = a; //隱含轉換 – 不是很多 (有data loss 時,不會自動幫你做隱含轉換)
long c = 32;
int d = (int)c; //明確轉換 (有data loss 時,一定要自己做明確轉換)
}
}
}
——————————————————————————————————————–
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write(“請輸入您的名字");
string userName = Console.ReadLine();
Console.WriteLine(userName+"您好!!");
Console.WriteLine(“我猜 " + userName + " 您一定是個男生!!" );
Console.ReadLine();
}
}
}
——————————————————————————————————————–
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write(“輸入一個整數 輸入一個整數:");
string c = Console.ReadLine(); //讀進來是字串
Console.WriteLine(c + " 加 1 後為 " + (int.Parse(c) + 1).ToString()); //先把讀進來的字串轉為INT, 計算完後,再變成string
Console.ReadLine();
}
}
}
——————————————————————————————————————–
//單純練習型別轉換
//請使用者輸入四個整數
//將前二個整數及後二個整數個別相加
//再將這二個相加後的整數一起列印出來
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“請輸入四個整數");
string a = Console.ReadLine(); //讀進來是字串
string b = Console.ReadLine(); //讀進來是字串
string c = Console.ReadLine(); //讀進來是字串
string d = Console.ReadLine(); //讀進來是字串
string all_1 = (int.Parse(a) + int.Parse(b)).ToString(); //先把讀進來的字串轉為INT, 計算完後,再變成string
string all_2 = (int.Parse(c) + int.Parse(d)).ToString(); //先把讀進來的字串轉為INT, 計算完後,再變成string
Console.WriteLine(“二個相加後的整數為:" + all_1 + “和" + all_2);
Console.ReadLine();
}
}
}
——————————————————————————————————————–
儘量不要做 "字串加法", 很佔記憶體 !!!

用LINE分享給朋友:
這篇文章的QR CODE:(用手機掃我)