revit二次开发中用到的C#语言的部分新语法

using

//可以使用  
using var xxx = new xxx(); 
xxxx;
//来代替原来的 
using (var xxx = new xxx())
{
    xxxx;
}
//两者的区别就是新的语法更简洁,using作用域是在函数结束的时候自动释放。

全局using

利用global using 语法可以进行全局using引用,这样就不用每个文件都有using一堆的引用。需要将所有的global using都放在一个文件里,文件名为GlobalUsings.cs。

global using System;
global using System.Collections;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Text;
global using System.Reflection;
global using System.Text.RegularExpressions;

namespace

文件范围限定namespace,将文件的缩进减少一级。

//可以使用
namespace sourcetest;

public class Command
{
    [CommandMethod("Test")]
    public void Test()
    {
        using var tr = new DBTrans();


    }
}
//代替原来的
namespace sourcetest
{
    public class Command
    {
        [CommandMethod("Test")]
        public void Test()
        {
            using var tr = new DBTrans();


        }
    }
}

可空类型

C# 新版本对于null做了很多的语法糖以及可空类型来帮助开发者减少因null处理不当导致的错误。
◦ 首先开启可空类型 enable
◦ 声明可空的变量

string? a = null;  // 声明可空的字符串类型
int? a = null; // 声明可空的整形
int b = null; // 报错
string c = null; // 报错
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值