1、對話框
```
using System;
namespace Myapp
{
class Myclass
{
static void Main(string[] args)
{
Console.WriteLine("請輸入一個數字:");
string input = Console.ReadLine(); //用戶可以輸入
double d = double.Parse(input); //將用戶輸入的轉化成double類型
if ( d % 2 == 0)
{ Console.WriteLine("這是偶數");}
else
{ Console.WrireLine("這是單數");}
}
}
}
```
2、屬性獲取 (c#(二)課時 5)
有 get+set 的就是屬性。
一般用get獲取屬性值,用set重置屬性值。
如下:
```
class Student
{
//姓名
string name;
public string Name
{
get { return this.name;}
set { this.name = value;}
}
//年齡
string age;
public string Age
{
get { return this.age;}
set { this.age = value;}
}
//地址
string address;
public string Address
{
get { return this.address;}
set { this.address = value;}
}
}
```
第二種改法:屬性封裝
```
class Student
{
//姓名
string name;
public string Name
{
get { return this.name; }
set
{
if ( value == "")
{
throw new ArgumentException("姓名字段不能為空");
this.name = value;
}
}
}
//年齡
int age;
public int Age
{
get { return this.age; }
set
{
if (value < 1 || value > 100)
{
throw new ArgumentException("年齡超出范圍");
this.age = value;
}
}
}
}
```
> 屬性也可以直接通過對象名后面加點 `.` 來調用或賦值
> `Student stu = new Student;`
> `stu.Age = -2;`
如果屬性不需要特殊處理,可以直接
` public string Name {get; set;}`
對于只讀屬性,只要在聲明時自動忽略就可以:
`public string ProductNo{get;}`
- 幫助文檔 microsoft helo viewer
- c#開發環境及visual studio安裝注意事項
- c#程序基本結構-基本語法
- Q1: public static void main(String[] args) 是什么意思
- Q2: c#命名空間+Main方法
- Q3:注釋+命名規則+代碼規則
- Q4: c#語句 system => console
- Q5: 數據類型 .net
- Q5: 常用名字、變量、運算符
- Q6: 對話窗輸入-屬性
- Q7: 遞歸
- Q8:決策分支、條件判斷語句 if 語句
- Q9:數組
- Q10:字符串
- Q11:對象、類、訪問權限、靜態動態函數
- Q12:方法及參數——繼承于類
- Q13:構造函數
- Q14:繼承——base 關鍵字
- Q15:多態、虛方法、接口
- Q16:創建窗體應用、控件
- Q17:Ado數據訪問、連接 sqlserver 數據庫
- Q18: 讀取數據command + DataRead( )、DataSet + DateAdapter
- Q19: Entity Framwork、entity 與 ADO.net的區別
- Q20: 對話框、文件、文件夾
- Q21: 導入excel數據、更新到 dbo 數據庫中
- Q26: 獲取 excel 中每個 sheet 的表名
- Q22: 兩個窗體之間數據+方法傳遞
- Q23: 數學對象
- Q24: c#網站編寫
- Q25: visual studio2017如何查看幫助
- Q27: c# dictionary 字典對象
- Q28: 數組與dataTable互相轉化