相关疑难解决方法(0)

javascript中是否存在null-coalescing(Elvis)运算符或安全导航运算符?

我将通过例子解释:

猫王运营商(?:)

"Elvis运算符"是Java三元运算符的缩写.这方面的一个实例是,如果表达式解析为false或null,则返回"合理的默认值".一个简单的例子可能如下所示:

def gender = user.male ? "male" : "female"  //traditional ternary operator usage

def displayName = user.name ?: "Anonymous"  //more compact Elvis operator
Run Code Online (Sandbox Code Playgroud)

安全导航操作员(?.)

安全导航操作符用于避免NullPointerException.通常,在引用对象时,可能需要在访问对象的方法或属性之前验证它是否为null.为了避免这种情况,安全导航操作符将只返回null而不是抛出异常,如下所示:

def user = User.find( "admin" )           //this might be null if 'admin' does not exist
def streetName = user?.address?.street    //streetName will be null if user or user.address is null - no NPE thrown
Run Code Online (Sandbox Code Playgroud)

javascript jquery groovy safe-navigation-operator

185
推荐指数
13
解决办法
8万
查看次数

更改 appsettings 键时删除配置文件注释

我正在尝试更改 App.Config 文件 appsettings 键值,一切正常,同时更改键值所有注释都在配置文件中删除(我也想要注释),任何人都可以帮助我我的代码有什么问题吗

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ConfigFilepath,
                ConfigurationUserLevel.None);
            config.AppSettings.Settings["IPAddress"].Value = "10.10.2.3";

            config.Save(ConfigurationSaveMode.Full);
Run Code Online (Sandbox Code Playgroud)

c# app-config

1
推荐指数
1
解决办法
3867
查看次数