目录
一 程序结构
C# 文件的后缀为 .cs。一个 C# 程序主要包括以下部分:
- 命名空间声明(Namespace declaration)
- 一个 class
- Class 方法
- Class 属性
- 一个 Main 方法
- 语句(Statements)& 表达式(Expressions)
- 注释
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CSharpControlAndSyntaxStudy
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
二 基本语法
using System; //使用using关键字来导入相应的模块
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//namespace是一个命名空间
namespace CSharpControlAndSyntaxStudy
{
//使用class申明一个类
public partial class jibenyufa : Form
{
//使用两个单斜杠 // 表示单行注释
/*
* 这是
多行
注释
*/
//这是成员变量,变量的定义格式是 修饰符+类型+变量名称=初始值 ;
private int count = 0;
//这是成员函数,不过这个函数特殊一些,因为名称与类名相同,它叫构造函数,用于初始化一些数据
public jibenyufa()
{
InitializeComponent();
}
//加载窗口
private void jibenyufa_Load(object sender, EventArgs e)
{
//隐藏完成图标及禁用按钮
pictureBox1.Visible = false;
pictureBox2.Visible = false;
nextPage.Enabled = false;
//从文件中读取关键字显示到图表中
using (StreamReader sr=new StreamReader("file/keyword.txt"))
{
string line;
int count = 0;
while ((line=sr.ReadLine())!=null)
{
count++;
dbkeyWord.Rows.Add(string.Format($"第{count}个关键字"),line);
}
}
}
//点击完成后,显示完成图标
private void btnfinishi_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox2.Visible = true;
nextPage.Enabled = true;
}
//走到上一个页面
private void upPage_Click(object sender, EventArgs e)
{
//页面跳转逻辑
Form1 jb = new Form1();
this.Hide();
jb.ShowDialog();
this.Dispose();
}
//走到下一个页面
private void nextPage_Click(object sender, EventArgs e)
{
//页面跳转逻辑
dataType dt = new dataType();
this.Hide();
dt.ShowDialog();
this.Dispose();
}
}
}
三 关键字
abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
enum
event
explicit
extern
false
finally
fixed
float
for
foreach
goto
if
implicit
in
in
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
out
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
uint
ulong
unchecked
unsafe
ushort
using
virtual
void
volatile
while
add
alias
ascending
descending
dynamic
from
get
global
group
into
join
let
orderby
partial
(type)
partial
(method)
remove
select
set
四 数据类型
在 C# 中,变量分为以下几种类型:
- 值类型(Value types)
- 引用类型(Reference types)
- 指针类型(Pointer types)
值类型(Value types)列表
类型 | 描述 | 范围 | 默认值 |
bool | 布尔值 | True 或 False | False |
byte | 8 位无符号整数 | 0 到 255 | 0 |
char | 16 位 Unicode 字符 | U +0000 到 U +ffff | '\0' |
decimal | 128 位精确的十进制值,28-29 有效位数 | (-7.9 x 1028 到 7.9 x 1028) / 100 到 28 | 0.0M |
double | 64 位双精度浮点型 | (+/-)5.0 x 10-324 到 (+/-)1.7 x 10308 | 0.0D |
float | 32 位单精度浮点型 | -3.4 x 1038 到 + 3.4 x 1038 | 0.0F |
int | 32 位有符号整数类型 | -2,147,483,648 到 2,147,483,647 | 0 |
long | 64 位有符号整数类型 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | 0L |
sbyte | 8 位有符号整数类型 | -128 到 127 | 0 |
short | 16 位有符号整数类型 | -32,768 到 32,767 | 0 |
uint | 32 位无符号整数类型 | 0 到 4,294,967,295 | 0 |
ulong | 64 位无符号整数类型 | 0 到 18,446,744,073,709,551,615 | 0 |
ushort | 16 位无符号整数类型 | 0 到 65,535 | 0 |