使用大括号是一个“代码块”任何事情。当你定义一个class
你定义的代码块大括号内是这样的:
大括号内
public class MyClass
{
}
事情被认为是“在该范围内”。例如:
public class MyClass
{
// This method is in the scope of MyClass
public void MyMethod()
{
// This variable is in the scope of MyMethod.
// It is only accessible from within this method because that is where
// it is defined.
string myString = "Hello Method.";
}
// This variable is in the scope of MyClass.
// It is accessible within MyClass, including all methods that are also in scope
// of MyClass
public string myGlobalString = "Hello Global.";
}
在IF
声明的情况下,它们是一系列的语句块中,只能方法范围内包含的(你不能使用,例如一类的IF
语句)。代码只能通过语句提供的给定路径之一。例如:
public class MyClass
{
public void MyMethod(string myString)
{
if (myString == "Hello")
{
// Read like English. "If variable myString equals the value 'Hello', then do this code within this block."
}
else if (myString == "Goodbye")
{
// "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead.
}
else if (myString == "Good Morning")
{
// "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead.
}
else
{
// "...or else do this code if variable myString does not match any of the above code statements.
}
// You are not required to include "else" or even "else if". You could just do a single IF statement like this:
if (myString == "hi)
{
// "If variable myString equals the value 'hi' then do this, otherwise do nothing."
}
}
}
他改变了什么? – unknown 2011-03-24 01:59:16
哦标签!!!! – unknown 2011-03-24 01:59:34
我明白你是初学者,你正在学习。你想要嵌入你的想法的一件事是保持一致的格式。每当你用一个左花括号'{'开始一个新块时,你应该在每一行之后缩进一个额外的级别,并保持所有内容一致,直到你到达一个匹配的右花括号'}'。如果你这样做了,你可以很容易地看到你的代码存在问题。大括号不匹配,你有他们不应该的语句,等等。只要你遵循它,Visual Studio甚至会帮你缩进。 – 2011-03-24 02:03:48