c# 学习笔记

using(C# 参考)

using 关键字有两个主要用途:

using 语句定义一个范围,在此范围的末尾将释放对象:

string filePath = "example.txt";
string textToWrite = "Hello, this is a test message!";

// Use the using statement to ensure the StreamWriter is properly disposed of
using (StreamWriter writer = new StreamWriter(filePath))
{
    writer.WriteLine(textToWrite);
}

using 指令为命名空间创建别名,或导入在其他命名空间中定义的类型:

using System;
using System.IO;

C# 属性 (Get 和 Set)

属性和封装
在我们开始解释属性之前,您应该对封装"Encapsulation"有一个基本的了解。

封装的意义是确保对用户隐藏敏感数据。为此您必须:

将字段/变量声明为私有 private
通过属性提供公共字段public的值,通过get和set方法来访问和更新私有字段private。

class Person
{
  private string name; // 字段

  public string Name   // 属性
  {
    get { return name; }   // get 方法
    set { name = value; }  // set 方法
  }
}

C# where new 关键字的使用

约束

where T : struct T必须是一个结构类型
where T : class T必须是一个 Class类型
where T :new() T必须要有一个无参构造函数
whereT:NameOfBaseClass T必须继承名为NameOfBaseClass的类
where T:NameOflnterface T必须实现名为NameOfInterface的接

lambda 函数

static int GetElement(int[] drr,int index)=> arr[index]; //返回数组元素的一个引用

c# 关键字

https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/?redirectedfrom=MSDN

posted @ 2025-06-05 14:14  ChrainY  阅读(10)  评论(0)    收藏  举报