C# 相對於C++ 真是人性化多了 (不知道是不是IDE的關係 沒有深入研究)

小白使用的是C#2010 Express

 

C# Hello World

檔案>開新專案>Windows Form 應用程式

左方工具列 直接拉一個按鈕 按鈕事件程式碼

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World", "Caption",
MessageBoxButtons.OK,
MessageBoxIcon.Question);
}
}
}


 

數字、字串

字串加數字 不需要轉換函數

但如果直接數字a+數字b 會變成 字串a+字串b

private void button1_Click(object sender, EventArgs e)
{
int a = 3;
int b = 5;
string s = "3+5=";
MessageBox.Show(s+a+b, "Caption",
MessageBoxButtons.OK,
MessageBoxIcon.Question);

MessageBox.Show(s + (a + b), "Caption",
MessageBoxButtons.OK,
MessageBoxIcon.Question);
}

以上 s+a+b的結果 會變成 "3+5=35"

而s+(a+b)的結果 會變成 "3+5=8"

 


 

for 迴圈用法 列出0-99的數字

private void button1_Click(object sender, EventArgs e)
{
string s = "";
for (int i = 0; i <100; i++)
{
s += i+",";
}
MessageBox.Show(s, "",
MessageBoxButtons.OK,
MessageBoxIcon.Question);
}

 

以後有用上甚麼其他情形再補充....

目前還沒玩到那裏

arrow
arrow
    文章標籤
    C#
    全站熱搜
    創作者介紹
    創作者 抓狂小白 的頭像
    抓狂小白

    抓狂小白的程式筆記

    抓狂小白 發表在 痞客邦 留言(0) 人氣()