Day1 目标(6-8小时)
- 掌握 C# 面试高频语法与 OOP 核心
- 能写一个"学生管理系统(控制台版)"
- 能回答 10 个基础面试问题
第一部分:30分钟速通(只讲会考的)
1) 基础类型与可空
- 值类型:
int,double,bool,struct,enum - 引用类型:
string,class,array,object - 可空值类型:
int? age = null; - 可空引用(C# 8+):
string? name = null;
2) var、dynamic、object 区别
var:编译期推断,类型一旦确定不可变object:所有类型基类,调用成员前要转换dynamic:运行期绑定,灵活但更易出错(面试喜欢问风险)
3) 面向对象四件套
- 封装:字段私有 + 属性公开
- 继承:
class Dog : Animal - 多态:
virtual/override或接口实现 - 抽象:
abstract class+abstract method
4) 接口 vs 抽象类
- 接口:行为规范("能做什么")
- 抽象类:模板与共性实现("是什么"+部分实现)
5) 异常处理
try-catch-finally- 不要吞异常;要保留上下文(真实项目很重要)
6) 集合(高频)
List<T>:顺序、可重复Dictionary<TKey, TValue>:键值查找快HashSet<T>:去重
第二部分:手写3题(先自己写,再看参考)
题1:找数组最大值与最小值
输入 int[] nums = { 3, 9, -1, 6, 0 },输出 max/min。
cs
int[] nums = { 3, 9, -1, 6, 0 };
int max = nums[0], min = nums[0];
for (int i = 1; i < nums.Length; i++)
{
if (nums[i] > max) max = nums[i];
if (nums[i] < min) min = nums[i];
}
Console.WriteLine($"max={max}, min={min}");
题2:统计字符串中每个字符出现次数
如 "abcaab" 输出 a:3 b:2 c:1
cs
string s = "abcaab";
var dict = new Dictionary<char, int>();
foreach (var ch in s)
{
if (!dict.ContainsKey(ch)) dict[ch] = 0;
dict[ch]++;
}
foreach (var kv in dict)
{
Console.WriteLine($"{kv.Key}:{kv.Value}");
}
题3:定义一个 Person,支持多态打印信息
要求:基类 Person,子类 Student 重写方法。
cs
public class Person
{
public string Name { get; set; } = "";
public virtual void PrintInfo()
{
Console.WriteLine($"Person: {Name}");
}
}
public class Student : Person
{
public int Grade { get; set; }
public override void PrintInfo()
{
Console.WriteLine($"Student: {Name}, Grade: {Grade}");
}
}
第三部分:今日小项目(必须做)
做一个控制台 StudentManager,功能:
- 新增学生(Id, Name, Age)
- 查询全部学生
- 按 Id 删除学生
- 按 Name 模糊搜索
- 输入
0退出
项目要求(面试加分点)
- 用
List<Student>存数据 - 拆分成类:
Student、StudentService、Program - 有基本异常处理(如输入数字失败)
第四部分:今天必背10问(面试话术版)
-
值类型和引用类型区别?
值类型存储实际值,引用类型存储对象地址;值类型通常在栈上,引用类型对象通常在堆上。
-
string是值类型还是引用类型?引用类型,但有不可变特性。
-
var是动态类型吗?不是,
var是编译期类型推断。 -
ref和out区别?
ref传入前必须初始化;out不要求,但方法内部必须赋值。 -
重载和重写区别?
重载同名不同参数(编译期);重写是子类改父类虚方法(运行期多态)。
-
接口和抽象类什么时候用?
接口定义能力,抽象类抽公共实现;多继承能力靠接口。
-
try-catch-finally中 finally 一定执行吗?基本会执行,除非进程崩溃或强制终止。
-
List和Dictionary怎么选?顺序遍历用
List;按键高效查找用Dictionary。 -
什么是封装?
隐藏内部细节,对外暴露稳定接口,降低耦合。
-
什么是多态?
同一调用入口在不同对象上表现不同行为(如
override)。
你现在要交付的作业(按顺序)
- 完成上面3道手写题
- 完成
StudentManager小项目 - 把你的代码贴给我(至少贴
StudentService和Program)
作业代码
cs
public class Student
{
// 学号
public int Id { get; set; }
// 姓名
public string Name { get; set; }
// 年龄
public int Age { get; set; }
}
cs
using System;
using System.Collections.Generic;
using System.Linq;
public class StudentService
{
// 使用 List 存储学生
private List<Student> students = new List<Student>();
// 新增学生
public void AddStudent()
{
try
{
Console.Write("请输入学生ID:");
int id = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入学生姓名:");
string name = Console.ReadLine();
Console.Write("请输入学生年龄:");
int age = Convert.ToInt32(Console.ReadLine());
Student stu = new Student
{
Id = id,
Name = name,
Age = age
};
students.Add(stu);
Console.WriteLine("添加成功!");
}
catch (Exception)
{
Console.WriteLine("输入格式错误!");
}
}
// 查询全部学生
public void ShowAllStudents()
{
if (students.Count == 0)
{
Console.WriteLine("暂无学生数据!");
return;
}
Console.WriteLine("====== 学生列表 ======");
foreach (Student stu in students)
{
Console.WriteLine($"ID:{stu.Id} 姓名:{stu.Name} 年龄:{stu.Age}");
}
}
// 删除学生
public void DeleteStudentById()
{
try
{
Console.Write("请输入要删除的ID:");
int id = Convert.ToInt32(Console.ReadLine());
Student stu = students.Find(s => s.Id == id);
if (stu != null)
{
students.Remove(stu);
Console.WriteLine("删除成功!");
}
else
{
Console.WriteLine("未找到该学生!");
}
}
catch (Exception)
{
Console.WriteLine("输入错误!");
}
}
// 模糊搜索
public void SearchByName()
{
Console.Write("请输入姓名关键字:");
string keyword = Console.ReadLine();
List<Student> result = students.FindAll(
s => s.Name.Contains(keyword)
);
if (result.Count == 0)
{
Console.WriteLine("未找到学生!");
return;
}
Console.WriteLine("====== 搜索结果 ======");
foreach (Student stu in result)
{
Console.WriteLine($"ID:{stu.Id} 姓名:{stu.Name} 年龄:{stu.Age}");
}
}
}
cs
using System;
class Program
{
static void Main(string[] args)
{
StudentService service = new StudentService();
while (true)
{
Console.WriteLine();
Console.WriteLine("====== 学生管理系统 ======");
Console.WriteLine("1. 新增学生");
Console.WriteLine("2. 查看全部学生");
Console.WriteLine("3. 删除学生");
Console.WriteLine("4. 按姓名搜索");
Console.WriteLine("0. 退出");
Console.Write("请选择功能:");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
service.AddStudent();
break;
case "2":
service.ShowAllStudents();
break;
case "3":
service.DeleteStudentById();
break;
case "4":
service.SearchByName();
break;
case "0":
Console.WriteLine("程序退出!");
return;
default:
Console.WriteLine("输入错误,请重新输入!");
break;
}
}
}
}