使用{}块来提高可读性 [英] Using { } blocks to improve readability
问题描述
是否可以使用{}块来提高可读性和
代码的可维护性?
示例:
//添加复选框列
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText =" Include" ;;
column.Name =" IsUsedInNotification";
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}
谢谢
9月23日,12:31 * pm," GS" < gstoy ... @ gmail.comwrote:
是否可以使用{}块来提高可读性和
可维护性代码?
示例:
//添加复选框列
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
* * * * column.HeaderText =" Include" ;;
* * * * column.Name =" IsUsedInNotification" ;
* * * * column.FlatStyle = FlatStyle.Standard;
* * * * column.ThreeState = false;
* * * * column.ValueType = typeof(bool);
* * * * column.TrueValue = true;
* * * * column.FalseValue = false;
}
谢谢
它完全有效的代码......不过我个人更喜欢使用#regions
相同。
-Cnu
GS < gs ****** @ gmail.comwrote:
是否可以使用{}块来提高可读性和
代码的可维护性?
示例:
//添加复选框列
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn ();
{
column.HeaderText =" Include";
column.Name =" IsUsedInNotification" ;;
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}
It'a有点奇怪,但一般都没关系。如果你正在使用C#3,
,你可以用对象初始化器做得更好:
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn
{
HeaderText =" Include";
Name =" IsUsedInNotification";
FlatStyle = FlatStyle.Standard;
ThreeState = false;
ValueType = typeof(bool);
TrueValue = true;
FalseValue = false;
};
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps。 com / jon.skeet
C#深度: http:// csharpindepth .com
啊。不久前我在使用XmlTextWriter时做了这个。我将修改我的
使用它的最新代码并将其放入:-)
writer.WriteStartElement(" thing");
{
writer.WriteAttributeString(" name"," eggs");
writer.WriteAttributeString(" size"," 7") ;
writer.WriteStartElement(" innerThing");
{
等
}
writer.WriteEndElement();
}
writer.WriteEndElement();
不确定放置{和}的位置,但它看起来比这更好
writer.WriteStartElement(" thing");
writer.WriteAttributeString(" name"," eggs");
writer.WriteAttributeString(" size"," 7");
writer.WriteStartElement(" innerThing");
等
writer.WriteEndElement();
writer.WriteEndElement();
如果WriteStartElement返回一个IDisposable wouldn'会很棒t将其。
皮特
Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "Include";
column.Name = "IsUsedInNotification";
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}
thank you
On Sep 23, 12:31*pm, "G.S." <gstoy...@gmail.comwrote:Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
* * * * column.HeaderText = "Include";
* * * * column.Name = "IsUsedInNotification";
* * * * column.FlatStyle = FlatStyle.Standard;
* * * * column.ThreeState = false;
* * * * column.ValueType = typeof(bool);
* * * * column.TrueValue = true;
* * * * column.FalseValue = false;
}
thank youIts perfectly valid code.. however I personally prefer using #regions
for the same.
-Cnu
G.S. <gs******@gmail.comwrote:Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "Include";
column.Name = "IsUsedInNotification";
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}It''s a little bit odd, but generally okay. If you''re using C# 3,
however, you can do even better with an object initializer:
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn
{
HeaderText = "Include";
Name = "IsUsedInNotification";
FlatStyle = FlatStyle.Standard;
ThreeState = false;
ValueType = typeof(bool);
TrueValue = true;
FalseValue = false;
};
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Ah. I did this some time ago when using XmlTextWriter. I will revise my
newest code that uses it and put some in :-)
writer.WriteStartElement("thing");
{
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
writer.WriteStartElement("innerThing");
{
etc
}
writer.WriteEndElement();
}
writer.WriteEndElement();
Not sure where to put the { and }, but it looks better than this
writer.WriteStartElement("thing");
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
writer.WriteStartElement("innerThing");
etc
writer.WriteEndElement();
writer.WriteEndElement();
Would be great if WriteStartElement returned an IDisposable wouldn''t it.
Pete
这篇关于使用{}块来提高可读性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!