使用多变量声明 [英] using statement with multiple variables

查看:119
本文介绍了使用多变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过某种方式宣布与使用块内的2个可变让这个code稍微更紧凑?

使用(VAR SR =新StringReader(内容))
{
    使用(VAR XTR = XmlTextReader的新(SR))
    {
        OBJ = XmlSerializer.Deserialize(XTR)作为的TModel;
    }
}


解决方案

被接受的方法是把链条中的语句:

 使用(VAR SR =新StringReader(内容))
使用(VAR XTR = XmlTextReader的新(SR))
{
    OBJ = XmlSerializer.Deserialize(XTR)作为的TModel;
}

请注意,该IDE也将支持这一缺口,即其有意不会试图缩进第二使用语句。

Is it possible to make this code a little more compact by somehow declaring the 2 variable inside the same using block?

using (var sr = new StringReader(content))
{
    using (var xtr = new XmlTextReader(sr))
    {
        obj = XmlSerializer.Deserialize(xtr) as TModel;
    }
}

解决方案

The accepted way is just to chain the statements:

using (var sr = new StringReader(content))
using (var xtr = new XmlTextReader(sr))
{
    obj = XmlSerializer.Deserialize(xtr) as TModel;
}

Note that the IDE will also support this indentation, i.e. it intentionally won’t try to indent the second using statement.

这篇关于使用多变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆