如果能回答下面的问题,你就掌握了本节内容
- C#中的项目类型主要有什么
- 命名空间(namespace)是什么
- 如何导入命名空间
- Console类的用途是什么
- 类是什么
- 声明类的关键字是什么
- Program是什么
- Main()方法是什么
- 使用Main()方法要注意什么
- 关键字是什么
- 标识符是什么
- 标识符的命名规则是什么
- 注释是什么
- 注释的两种写法是什么
- Console的输入方法的用法是什么
- Console的输出方法的用法是什么
- 常用的格式字符是什么
- C#是否区分大小写?
- [实操] 写一个控制台应用程序"Hello World"
- [实操] 写一个Windows窗体程序“Hello World”
- [实操] 写一个Web应用程序“Hello World”
一、C#中的项目类型主要有什么
- 控制台应用程序
- Windows窗体应用程序
- Web应用程序
命名空间(namespace)是什么
- 命名空间是一个命名的独立空间。
- 命名空间是一种组织代码单元的机制,避免命名冲突。
- 命名空间遵循帕斯卡命名法命名。
如何导入命名空间
使用using语句可导入命名空间
using 命名空间; //using是小写
Console类的用途是什么
-
Console
即控制台的意思。 -
Console
类是System命名空间下的一个内建类。 -
Console
类用于控制台交互,即向控制台输出数据或从控制台读取数据。
类是什么
- "类"是面向对象编程的基本单元。
- 类名通常与文件名一致(Program.cs)。
声明类的关键字是什么
class
是声明类的关键字。
Program是什么
Program是系统自动生成的类,是程序执行的入口类,花括号表示Program
类的代码块开始。
Main()方法是什么
Main()方法是程序的唯一入口,是程序启动时自动执行的方法。
使用Main()方法要注意什么
- Main()方法必须声明在类或结构体的内部;
- Main()方法必须是静态的;
- Main()方法不应该是
public
的; - Main()方法的返回值类型必须是void或int
- Main()方法的参数
string[] args
是可选的
关键字是什么
关键字是C#语言中已经被赋予特定意义的单词。
不可以把关键字作为命名空间、类、方法或属性来使用。
标识符是什么
标识符主要用来表示类名、变量名、方法名、属性名、数组名等成员。
标识符的命名规则是什么
- 合法字符:字符数字下划线
- 数字不能开头
- 不能是关键字。
注释是什么
注释是不会被编译器执行的代码或文字。
注释的功能是对某行或某段代码进行说明或调试。
注释的两种写法是什么
- 行注释:使用
//注释内容
表示。用于注释单行代码; - 块注释:使用
/*注释内容*/
表示。用于注释多行连续的代码段。
Console的输出方法的用法是什么
1. Console.Write()
用途:输出内容,不换行
// 输出字符串
Console.Write("Hello"); // 输出:Hello
Console.Write("World"); // 输出:HelloWorld
// 输出变量
string name = "张三";
int age = 25;
Console.Write("姓名:" + name);
Console.Write(",年龄:" + age);
// 使用字符串插值(C# 6.0+)
Console.Write($"姓名:{name},年龄:{age}");
// 格式化输出
Console.Write("姓名:{0},年龄:{1}", name, age);
2. Console.WriteLine()
用途:输出内容并换行
// 输出字符串并换行
Console.WriteLine("Hello World"); // 输出后换行
// 输出变量
Console.WriteLine($"姓名:{name}"); // 输出后换行
Console.WriteLine($"年龄:{age}"); // 输出后换行
// 空行
Console.WriteLine(); // 只输出换行
// 格式化输出
Console.WriteLine("姓名:{0},年龄:{1}", name, age);
3. Console输出示例组合
string product = "笔记本电脑";
double price = 5999.99;
int quantity = 3;
Console.WriteLine("=== 购物清单 ===");
Console.Write("商品:");
Console.WriteLine(product);
Console.Write("单价:");
Console.WriteLine($"¥{price:F2}");
Console.Write("数量:");
Console.WriteLine(quantity);
Console.Write("总价:");
Console.WriteLine($"¥{price * quantity:F2}");
Console.WriteLine("================");
4.格式化输出
// 数字格式化
double number = 1234.5678;
Console.WriteLine("默认:{0}", number); // 1234.5678
Console.WriteLine("货币:{0:C2}", number); // ¥1,234.57
Console.WriteLine("小数:{0:F2}", number); // 1234.57
Console.WriteLine("整数:{0:D6}", 123); // 000123
Console.WriteLine("科学计数:{0:E2}", number); // 1.23E+003
Console.WriteLine("百分比:{0:P1}", 0.456); // 45.6%
// 日期时间格式化
DateTime now = DateTime.Now;
Console.WriteLine("短日期:{0:d}", now); // 2023/12/15
Console.WriteLine("长日期:{0:D}", now); // 2023年12月15日
Console.WriteLine("时间:{0:T}", now); // 14:30:25
Console.WriteLine("完整:{0:F}", now); // 2023年12月15日 14:30:25
5. 颜色输出
// 设置前景色(文字颜色)
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("错误信息!");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("成功信息!");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("警告信息!");
// 重置颜色
Console.ResetColor();
// 设置背景色
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("特殊背景的文字");
Console.ResetColor();
6. 高级输出技巧
// 对齐输出
string[] names = { "张三", "李四", "王五" };
int[] ages = { 25, 30, 28 };
Console.WriteLine("{0,-10} {1,5}", "姓名", "年龄");
Console.WriteLine(new string('-', 16));
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("{0,-10} {1,5}", names[i], ages[i]);
}
// 输出结果:
// 姓名 年龄
// -----------------
// 张三 25
// 李四 30
// 王五 28
7. 控制台窗口操作
// 设置控制台标题
Console.Title = "我的控制台应用程序";
// 设置窗口大小
Console.SetWindowSize(80, 30);
// 设置缓冲区大小(可滚动区域)
Console.SetBufferSize(80, 100);
// 清屏
Console.Clear(); // 清除控制台所有内容
// 设置光标位置
Console.SetCursorPosition(10, 5); // 第10列,第5行
Console.Write("指定位置输出");
// 获取光标位置
int cursorLeft = Console.CursorLeft;
int cursorTop = Console.CursorTop;
8. 进度条示例
static void ShowProgressBar(int progress, int total)
{
Console.CursorVisible = false;
int barWidth = 50;
// 回到行首
Console.SetCursorPosition(0, Console.CursorTop);
// 绘制进度条
int progressWidth = (int)((double)progress / total * barWidth);
string bar = new string('█', progressWidth) + new string('░', barWidth - progressWidth);
Console.Write($"[{bar}] {progress}/{total} ({(double)progress/total:P0})");
}
// 使用示例
for (int i = 0; i <= 100; i++)
{
ShowProgressBar(i, 100);
Thread.Sleep(50);
}
Console.WriteLine(); // 换行
Console.CursorVisible = true;
9. 输出编码处理(中文支持)
// 确保中文正常显示
Console.OutputEncoding = System.Text.Encoding.UTF8;
// 或者使用系统默认编码
Console.OutputEncoding = System.Text.Encoding.Default;
Console.WriteLine("中文测试:你好世界!");
Console的输入方法的用法是什么
1. Console.ReadLine()
用途:读取整行输入,返回字符串。
// 基本用法
Console.Write("请输入您的姓名:");
string name = Console.ReadLine();
Console.WriteLine($"您好,{name}!");
// 读取数字并转换
Console.Write("请输入年龄:");
string ageInput = Console.ReadLine();
int age = int.Parse(ageInput); // 或使用 int.TryParse() 更安全
// 读取多个值
Console.Write("请输入城市:");
string city = Console.ReadLine();
2. Console.Read()
用途:读取单个字符,返回ASCII整数值。(Read()只读取一个字符,但会留下换行符在缓冲区)
Console.Write("请输入一个字符:");
int charValue = Console.Read(); // 返回ASCII码
char character = (char)charValue; // 转换为字符
Console.WriteLine($"您输入的字符是:{character},ASCII码:{charValue}");
3. Console.ReadKey()
读取单个按键,返回ConsoleKeyInfo对象.
// 基本用法
Console.Write("请按任意键继续...");
ConsoleKeyInfo keyInfo = Console.ReadKey();
Console.WriteLine($"\n您按下了:{keyInfo.Key} 键");
// 不显示按下的键
Console.Write("请输入密码(不显示):");
ConsoleKeyInfo key;
string password = "";
do {
key = Console.ReadKey(true); // true表示不显示按键
if (key.Key != ConsoleKey.Enter) {
password += key.KeyChar;
Console.Write("*"); // 显示星号代替实际字符
}
} while (key.Key != ConsoleKey.Enter);
4. 安全的数据类型转换
// 安全的数字输入处理
static int GetIntegerInput(string prompt)
{
int result;
while (true)
{
Console.Write(prompt);
string input = Console.ReadLine();
if (int.TryParse(input, out result))
{
return result;
}
Console.WriteLine("输入无效,请输入数字!");
}
}
// 使用示例
int number = GetIntegerInput("请输入一个数字:");
5. 读取多个值示例
Console.WriteLine("请输入个人信息:");
Console.Write("姓名:");
string name = Console.ReadLine();
Console.Write("年龄:");
int age = int.Parse(Console.ReadLine());
Console.Write("职业:");
string job = Console.ReadLine();
Console.WriteLine($"\n个人信息:\n姓名:{name}\n年龄:{age}\n职业:{job}");
6. 处理特殊输入场景
// 读取Yes/No选择
static bool GetYesNoInput(string prompt)
{
while (true)
{
Console.Write($"{prompt} (y/n): ");
ConsoleKeyInfo key = Console.ReadKey();
Console.WriteLine();
if (key.Key == ConsoleKey.Y)
return true;
if (key.Key == ConsoleKey.N)
return false;
Console.WriteLine("请输入 y 或 n");
}
}
// 使用示例
bool continueProgram = GetYesNoInput("是否继续?");
常用的格式字符是什么
格式符 | 名称 | 描述 | 示例代码 | 输出结果 |
---|---|---|---|---|
{0} |
占位符 | 基本的参数占位符 | Console.WriteLine("{0} {1}", "Hello", "World"); |
Hello World |
{0:C} |
货币格式 | 显示为货币格式 | Console.WriteLine("{0:C}", 1234.56); |
¥1,234.56 |
{0:C0} |
货币格式(无小数) | 货币格式,无小数位 | Console.WriteLine("{0:C0}", 1234.56); |
¥1,235 |
{0:D} |
十进制整数 | 整数格式显示 | Console.WriteLine("{0:D}", 123); |
123 |
{0:D6} |
十进制整数(补零) | 整数格式,指定位数 | Console.WriteLine("{0:D6}", 123); |
000123 |
{0:E} |
科学计数法 | 科学计数法格式 | Console.WriteLine("{0:E}", 1234.56); |
1.234560E+003 |
{0:E2} |
科学计数法(指定位数) | 科学计数法,指定小数位 | Console.WriteLine("{0:E2}", 1234.56); |
1.23E+003 |
{0:F} |
浮点数 | 浮点数格式 | Console.WriteLine("{0:F}", 1234.56); |
1234.56 |
{0:F2} |
浮点数(指定位数) | 浮点数,指定小数位 | Console.WriteLine("{0:F2}", 1234.5); |
1234.50 |
{0:F0} |
浮点数(无小数) | 浮点数,无小数位 | Console.WriteLine("{0:F0}", 1234.56); |
1235 |
{0:G} |
通用格式 | 最紧凑的格式 | Console.WriteLine("{0:G}", 1234.56); |
1234.56 |
{0:N} |
数字格式 | 带千位分隔符 | Console.WriteLine("{0:N}", 1234567.89); |
1,234,567.89 |
{0:N0} |
数字格式(无小数) | 带千位分隔符,无小数 | Console.WriteLine("{0:N0}", 1234567.89); |
1,234,568 |
{0:P} |
百分比格式 | 百分比格式 | Console.WriteLine("{0:P}", 0.456); |
45.60% |
{0:P1} |
百分比格式(指定位数) | 百分比,指定小数位 | Console.WriteLine("{0:P1}", 0.456); |
45.6% |
{0:X} |
十六进制 | 十六进制格式(大写) | Console.WriteLine("{0:X}", 255); |
FF |
{0:x} |
十六进制 | 十六进制格式(小写) | Console.WriteLine("{0:x}", 255); |
ff |
{0:0000} |
数字补零 | 数字补零到指定位数 | Console.WriteLine("{0:0000}", 123); |
0123 |
{0:####} |
数字格式 | 自定义数字格式 | Console.WriteLine("{0:####}", 123); |
123 |
{0:0.00} |
小数格式 | 固定小数位数 | Console.WriteLine("{0:0.00}", 123.4); |
123.40 |
{0:d} |
短日期 | 短日期格式 | Console.WriteLine("{0:d}", DateTime.Now); |
2023/12/15 |
{0:D} |
长日期 | 长日期格式 | Console.WriteLine("{0:D}", DateTime.Now); |
2023年12月15日 |
{0:t} |
短时间 | 短时间格式 | Console.WriteLine("{0:t}", DateTime.Now); |
14:30 |
{0:T} |
长时间 | 长时间格式 | Console.WriteLine("{0:T}", DateTime.Now); |
14:30:25 |
{0:yyyy-MM-dd} |
自定义日期 | 自定义日期格式 | Console.WriteLine("{0:yyyy-MM-dd}", DateTime.Now); |
2023-12-15 |
{0:HH:mm:ss} |
自定义时间 | 自定义时间格式 | Console.WriteLine("{0:HH:mm:ss}", DateTime.Now); |
14:30:25 |
{0,-10} |
左对齐 | 指定宽度并左对齐 | Console.WriteLine("{0,-10}", "Hello"); |
Hello |
{0,10} |
右对齐 | 指定宽度并右对齐 | Console.WriteLine("{0,10}", "Hello"); |
Hello |
使用示例
// 综合示例
double amount = 1234567.8912;
DateTime now = DateTime.Now;
int number = 42;
Console.WriteLine("货币格式: {0:C2}", amount); // ¥1,234,567.89
Console.WriteLine("数字格式: {0:N0}", amount); // 1,234,568
Console.WriteLine("百分比: {0:P1}", 0.456); // 45.6%
Console.WriteLine("日期: {0:yyyy-MM-dd}", now); // 2023-12-15
Console.WriteLine("十六进制: {0:X}", number); // 2A
Console.WriteLine("补零: {0:000000}", number); // 000042
Console.WriteLine("对齐: |{0,-10}|{1,10}|", "左", "右"); // |左 | 右|
自定义数字格式符
格式符 | 描述 | 示例 | 输出 |
---|---|---|---|
0 |
数字占位符,不足补零 | {0:000.00} |
012.30 |
# |
数字占位符,不补零 | {0:###.##} |
12.3 |
. |
小数点 | {0:0.00} |
12.30 |
, |
千位分隔符 | {0:0,0} |
12,345 |
% |
百分比占位符 | {0:0%} |
1230% |
; |
分段分隔符 | {0:正数;负数;零} |
根据值显示 |
这些格式符可以组合使用,提供灵活的格式化输出功能。
C#是否区分大小写?
区分大小写。