目录
编写第一个C#程序
打开Visual Studio工具,创建一个项目
打开Program.cs编写代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
// 输出内容
Console.WriteLine("人因梦想而伟大");
Console.ReadLine();
}
}
}
启动输出
C#中的命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hello_Wrold;
namespace Demo
{
class Program
{
Test test = new Test();
static void Main(string[] args)
{
// 输出内容
Console.WriteLine("人因梦想而伟大");
Console.ReadLine();
}
}
}
namespace Hello_Wrold
{
class Test
{
}
}
C#中的类
程序的启动器-Main方法
Main方法的要求
基本要求
Main方法必须定义为static
Main的首字母必须大写
返回值可以是void或int
命令行参数可选
语法
static void Main (string[] args) {}
static void Main () {}
static int Main (string[] args) {}
static int Main () {}
Main方法的常见错误提示
错误一
原因:
(1)没有Main方法
(2)Main方法写成了main方法,Main首字母应该大写
(3)没有static
错误二
原因:
Main方法的返回值不是void或int
错误三
一个类里面有多个Main入口函数时需要指定
如
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hello_Wrold;
namespace Demo
{
class Program
{
Test test = new Test();
static int Main(string[] args)
{
// 输出内容
Console.WriteLine("Hello World");
Console.ReadLine();
return 0;
}
}
}
namespace Hello_Wrold
{
class Test
{
static int Main(string[] args)
{
// 输出内容
Console.WriteLine("Hello World");
Console.ReadLine();
return 0;
}
}
}
C#中的标识符
C#中的关键字
int | public | this | finally | boolean | abstract |
continue | float | long | short | throw | return |
break | for | foreach | static | new | interface |
if | goto | default | byte | do | case |
void | try | switch | else | catch | private |
double | protected | while | char | class | using |
C#中的语句
概念:C#语句实质上就是实现各种功能代码的C#代码。
Hello World关键代码回顾:
Console.WriteLine("Hello World");
Console.ReadLine();
Console为控制台类
C#中程序编写规范
代码编写规则
- 尽量使用借口编程
- 关键语句一定编写注释
- 局部变量随用随声明
- 尽量少用goto语句
- 如果参数多,建议使用结构
- 避免对大段代码使用try...catch
- 同一个文件中避免编写多个类
- 字符串多变时,用StringBuilder
- if语句块中使用"{}"
- switch语句中一定编写default
两种命名方法
Pascal命名法:所有单词第一个字母大写,其他字母小写。
如:User GetInfo
Camel命名法(驼峰命名法):除了第一个单词,所有单词第一个字母大写,其他字母小写。
如 userId userName
C#中的变量
在C#中一个变量就是储存区(内存)中的一个存储单元。
C#中变量的声明及初始化
使用变量的步骤
- 声明一个变量 根据类型分配空间
- 初始化变量 将数据存入内存空间
- 使用变量 取出数据使用
变量的声明
变量的命名规则
变量的初始化
变量的作用域
变量使用常见错误
C#中的数据类型
类型 | 描述 | 范围 | 默认值 |
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 |
整数类型
浮点类型
bool类型
引用类型
Object类
String类
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Consume c = new Consume();
c.Record();
Console.Read();
}
}
class Card
{
public string Money
{
get;set;
}
}
class Consume
{
public void Record()
{
Console.WriteLine("信用卡消费记录\n");
Card pCard = new Card() { Money = "8000" };
Console.WriteLine("信用卡的总额度:" + pCard.Money);
Card sCard = pCard;
sCard.Money = "3000";
Console.WriteLine("信用卡消费记录:" + pCard.Money);
}
}
}
运行结果:
值类型和引用类型的区别
一种特殊的字段-枚举
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
public enum MRKJ
{
CS = 1,
C = 2,
Java = 3
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MRKJ.Java);
Console.Read();
}
}
}
输出结果:
C#中的数据类型转换
隐式类型转换
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
double score = 81.5;
double score2 = score + 2;
Console.WriteLine("第二次的成績:" + score2);
Console.ReadLine();
}
}
}
显式类型转换
(类型名)表达式
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
double score = 81.5;
int score2 = (int)(score + 2 );
Console.WriteLine("第二次的成績:" + score2);
Console.ReadLine();
}
}
}
Convert.To类型名(表达式)
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
double score = 10.5;
int c = Convert.ToInt32(score);
Console.WriteLine("第二次的成績:" + c);
Console.ReadLine();
}
}
}
C#中的拆箱和装箱
C#中的常量
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
const int PRICE1 = PRICE2 * 2;
const int PRICE2 = 368;
static readonly int PRICE3 = PRICE4 * 2;
static readonly int PRICE4 = 368;
static void Main(string[] args)
{
Console.WriteLine("const常量輸出效果");
Console.WriteLine("PRICE1的值:" + PRICE1);
Console.WriteLine("PRICE2的值:" + PRICE2);
Console.ReadLine();
Console.WriteLine("readonly常量輸出效果");
Console.WriteLine("PRICE3的值:" + PRICE3);
Console.WriteLine("PRICE4的值:" + PRICE4);
Console.ReadLine();
}
}
}
输出结果:
常量和变量比较示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
const double PI = 3.14;
double R;
Console.WriteLine("請輸入圓的半徑:");
R = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("圓的面積:" + PI * R * R);
Console.ReadLine();
}
}
}
输出结果:
C#中的表达式
C #中的算术运算符