关键词搜索

源码搜索 ×
×

C#-6 运算符和语句

发布2022-05-24浏览243次

详情内容

C#教程icon-default.png?t=M4ADhttps://www.jxasp.com/blog

运算符重载

可以重定义或重载 C# 中内置的运算符。
重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。

  1. public static Box operator+ (Box b, Box c)
  2. {
  3. Box box = new Box();
  4. box.length = b.length + c.length;
  5. box.breadth = b.breadth + c.breadth;
  6. box.height = b.height + c.height;
  7. return box;
  8. }
  9. var a = new Box();
  10. var b = new Box();
  11. var c = a + b ;

二 using语句

某些类型的非托管资源有数量限制或很耗费系统资源。使用完后应尽快释放。
资源是指一个实现了System.IDisposable接口的类或结构。IDisposable接口中有一个Dispose方法。
using是一种可确保正确使用 IDisposable 对象的方便语法。

  1. string manyLines = @"This is line one
  2. This is line two
  3. Here is line three
  4. The penultimate line is line four
  5. This is the final, fifth line.";
  6. using (var reader = new StringReader(manyLines))
  7. {
  8. string? item;
  9. do
  10. {
  11. item = reader.ReadLine();
  12. Console.WriteLine(item);
  13. } while (item != null);
  14. }

C# 复制 全屏

分类: .Net C#

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载