(二).net面试(static/equals/property/IQueryable)
·
文章目录
项目地址
- 教程作者:
- 教程地址:
- 代码仓库地址:
- 所用到的框架和插件:
dbt
airflow
一、基础50
1.1 new keyword
- new operator
- new modifier
- new constraint
1.2 static class vs. static method
1. static class
- 只能包含静态成员(字段、方法、属性、事件)
- 不能实例化(new 不出来)
- 不能继承或被继承(sealed + abstract 的结合体)
- 默认是 sealed(防止继承)
- 在程序第一次使用时,CLR 会初始化一次(静态构造函数)
2. static method
- 属于类的本身,而不是对象成员
- 不能访问实例成员
3. static constructor 静态构造函数
- 没有访问修饰符,没有参数
- CLR 在 第一次访问类 时调用一次
- 用于初始化静态字段
- 不能手动调用
public class Logger
{
public static string FilePath;
static Logger()
{
FilePath = "log.txt";
Console.WriteLine("Static constructor called");
}
}
4. 静态成员的生命周期
静态字段在 应用程序域(AppDomain)级别唯一
1.3 LinQ
1.what is LinQ
- LINQ is a set of technologies that allow simple and efficient querying over different kinds of data
- There are two ways of querying data: ①query syntax ;② method syntax
2. List、IEnumerable、IQueryable
- List → 具体集合类,数据已经在内存。
- IEnumerable → 内存中逐个枚举,延迟执行,适合集合操作。
- IQueryable → 针对数据库等远程源,查询会被翻译成 SQL 执行,更高效。
3. 在数据库里用 IEnumerable
- 如果你在数据库中使用 IEnumerable,查询会在内存中执行,导致先把整张表的数据拉到本地再过滤,非常低效;
- 而 IQueryable 会把查询翻译成 SQL 在数据库端执行,性能更好。
1.4 ==和equals
1. ==
- == 默认比较对象引用,值类型比较值
- 用途:检查 两个对象的引用是否相同(默认情况下)。
- 可被重载:像 string、int 等类型重载了 ==,会比较值而不是引用。
- 适合:值类型或经过重载的引用类型。
2. Equals
- Equals 默认也比较引用,但可以重写来比较对象的逻辑内容
class Person
{
public string Name { get; set; }
public override bool Equals(object obj)
{
return obj is Person p && this.Name == p.Name;
}
}
1.5 综合
1. Property vs. Field
- A property is a way to encapsulate a field and provide controlled access to it through get and set methods, allowing you to enforce validation, read-only or write-only behavior, and maintain encapsulation.
2. const和readonly
- const is a compile-time constant that’s implicitly static, while readonly is a runtime constant that can be assigned at declaration or in a constructor but not changed afterward.
1.6 Convariance(out) vs. Contravariance(in)
1. out
- 泛型参数只能作为输出(返回值),可以把子类当父类用
- 允许你在泛型返回类型上使用更具体的类型。
- 子类可以当作父类返回。
2. in
- 泛型参数只能当作输入参数
- 允许你在泛型输入参数上使用更泛化的类型。
- 父类可以当作子类输入。
- 通用处理器就能处理所有继承自父的子事件
二、Meadium 50
2.1 is vs. as
1. is
- is is used to check if an object is of a given type, it returns a boolean type. Both the value type and the reference type can use it
2. as
- as is used to cast a variable to a given type; if the cast fails, the value will be null; as can only be used for casting to reference types or nullable value types
difference between regular cast and as
- Regular casting fails, and an invalidCastException will be thrown.
- If casting with as fails, the result will be null
2.2 using keyword
1. using namespaces
- it is used to import namespaces
2. Dispose Resources
- Ensures that objects implementing IDisposable (like Stream, DbConnection, etc.) are cleaned up automatically, even if an exception occurs.
using (var file = new StreamWriter("test.txt"))
{
file.WriteLine("Hello world");
} // file.Dispose() is called automatically here
3. Using Alias
- You can give a namespace or class a shorter alias name.
using Project = MyCompany.Project.Very.Long.Namespace;
class Program
{
static void Main()
{
Project.MyClass obj = new Project.MyClass();
}
}
2.3 throw vs. throw Ex
- throw preservers the stack trace, while throw ex not
- throw helps us to find the original source of the problem
1. what is the stack trace
- Stack trace is a trace of all methods that have been called and it allows us to locate the exact line in the code
2.4 typeof vs. GetType
- Both of them return Type object
- typeof(Person) → tells you what properties exist on the type at compile time, but not their values.
- tells you the runtime type of p, and with reflection (prop.GetValue§), you can also get property values.
2.5 float vs. double vs. decimal
Feature | float (Single) |
double (Double) |
decimal (Decimal) |
---|---|---|---|
Size | 4 bytes (32-bit) | 8 bytes (64-bit) | 16 bytes (128-bit) |
Precision | ~7 decimal digits | ~15–16 decimal digits | ~28–29 decimal digits |
Representation | Binary floating-point (base-2) | Binary floating-point (base-2) | Decimal floating-point (base-10) |
Performance | Fastest, least memory | Fast, hardware-optimized | Slowest, software-based arithmetic |
Default literal | Needs f suffix (3.14f ) |
Default for floating literals (3.14 ) |
Needs m suffix (3.14m ) |
Typical Usage | Graphics, games, sensors, 3D math | Science, engineering, general purpose | Finance, money, currency, accounting |
2.6 GetHashCode()
1. 什么是GetHashCode()
- 引用类型 → 默认返回的是 对象引用的内部哈希值,和内存地址相关。
- 值类型 → 会基于字段值生成哈希。
2. 和 Equals() 的关系
3. 重写
更多推荐
所有评论(0)