对象编程

Week 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
double a = 5.456;
int b = 15;
Console.WriteLine(a);
Console.WriteLine("Hello world {1} {0}", a, a + b); // 先填小的0再填大的1, 没有0只有1直接报错
Console.WriteLine("{0, 4:f2}", a);
Console.WriteLine($"Hello world {a} {a + b}");
//string str = Console.ReadLine(); // 只能用string
string str = "15 65 8 96";
Console.WriteLine(str);
//double ans = Convert.ToDouble(str); // 转换成double
String[] result = str.Split(' ',',');
foreach (string s in result) {
Console.WriteLine($"{Convert.ToInt32(s)}");
}
int[] sum = Array.ConvertAll(result, Convert.ToInt32);
foreach (int s in sum)
{
Console.WriteLine("{0}", s);
}

char flag = '5';
Console.WriteLine(char.IsNumber(flag));
string s1 = "Hello ";
string s2 = "World!";
string s3 = s1 + s2;
Console.WriteLine(s3[7] + s2[2]);//只读不能改
Console.WriteLine("a" + 300 + 5 + "b");
Console.WriteLine("a" + (300 + 5) + "b");

string s4 = "nihao\nnihao";
Console.WriteLine(s4);
s4 = @"nihao\nnihao"; //@符号表示逐字字符串,\n不再被解释为换行符,而是作为普通的文本输出
Console.WriteLine(s4);

string s5 = "World!";
Console.WriteLine(s2 == s5);//C#中字符串比较是比较内容而不是地址

double d1 = 1.5;
float d2 = 1.0f;//必须有f否则报错,因为double精度高, C#的默认浮点数类型是double且不支持隐式转换为float
decimal x = (decimal)d1;//显式转换

int k1= Convert.ToInt32("48");
int k2 = int.Parse("45"); // Convert.ToInt32和int.Parse的区别:Convert.ToInt32在转换失败时返回0,而int.Parse会抛出异常

int res;
bool q = int.TryParse("abc", out res); // TryParse方法尝试将字符串转换为整数,如果转换成功返回true,并将结果存储在res中;如果转换失败返回false,并将res设置为0
Console.WriteLine($"q is {q}, res is {res}");
}
}
public enum Title {
apple,
banana,
orange, //用逗号,默认是int
}
public class Student
{
public string name;
public int age;
public Title title;
public Student(string name, int age) {
this.name = name;
this.age = age;
}
public void sum() {
switch (title)
{
case Title.apple:
Console.WriteLine("apple");
break;
case Title.banana:
Console.WriteLine("banana");
break;
case Title.orange:
Console.WriteLine("orange");
break;
}
}
}
}


对象编程
https://miao62.github.io/2026/03/10/对象编程/
Author
Miao
Posted on
March 10, 2026
Licensed under