C Sharp Programming/Syntax: Difference between revisions - Wikibooks, open books for an open world


Article Images

Line 1:

<noinclude>{{C sharp/Navigation}}</noinclude>

C# syntax looks quite similar to the syntax of Java because both inherit much of their syntax from C and C++. The '''The object-oriented''' nature of C# requires the high-level structure of a C# program to be defined in terms of [[C Sharp Programming/Classes|classes]], whose detailed behaviors are defined by their ''statements''.

==Statements==

Line 9:

Examples:

<sourcesyntaxhighlight lang="csharp">

int sampleVariable; // declaring a variable

sampleVariable = 5; // assigning a value

Method(); // calling an instance method

SampleClass sampleObject = new SampleClass(); // creating a new instance of ana objectclass

sampleObject.ObjectMethod(); // calling a member function of an object

// executing a "for" loop with an embedded "if" statement

for (int i = 0; i < upperLimit; i++)

{

if (SampleClass.SampleStaticMethodReturningBoolean(i))

Line 24:

}

}

</syntaxhighlight>

</source>

==Statement blocks==

A series of statements surrounded by curly braces form a ''block'' of code. Among other purposes, code blocks serve to limit '''scope''', or the scoperange ofin variableswhich defineda withinvariable themcan be used. A variable is only accessible in the block in which it is defined. Code blocks can be nested and often appear as the bodies of methods.

<sourcesyntaxhighlight lang="csharp">

private void MyMethod(int valueintegerValue)

{ // This block of code is the body of "MyMethod()"

// The 'valueintegerValue' integer parameter is accessible to everything in the method

int methodLevelVariable; // This variable is accessible to everything in the method

if (valueintegerValue == 2)

{

// methodLevelVariable is still accessible here

int limitedVariable; // vcvvvvvvThisThis variable is only accessible to code in the, if block

DoSomeWork(limitedVariable);

Line 49:

} // Here ends the code block for the body of "MyMethod()".

</syntaxhighlight>

</source>

==Comments==

''Comments'' allow inline documentation of source code. The C# compiler ignores comments. These styles of comments are allowed in C#:

;Single-line comments: The {{C sharp|//}} character sequence marks the following text as a single-line comment. Single-line comments, as one would expect, end at the first end-of-line following the {{C sharp|//}} comment marker.

;Multiple-line comments: Comments can span multiple lines by using the multiple-line comment style. Such comments start with {{C sharp|/*}} and end with {{C sharp|*/}}. The text between those multi-line comment markers is the comment.

<sourcesyntaxhighlight lang="csharp">

// This style of a comment is restricted to one line.

/*

This is another style of a comment.

It allows multiple lines.

*/

</syntaxhighlight>

</source>

;XML Documentation-line comments: These comments are used to generate XML documentation. Single-line and multiple-line styles can be used. The single-line style, where each line of the comment begins with {{C sharp|///}}, is more common than the multiple-line style delimited by {{C sharp|/**}} and {{C sharp|*/}}.

<sourcesyntaxhighlight lang="csharp">

/// <summary> documentation here </summary>

/// <remarks>

Line 78:

* </remarks>

*/

</syntaxhighlight>

</source>

==Case sensitivity==

C# is [[w:case-sensitive|case-sensitive]], including its variable and method names.

The variablesxxvariables {{C sharp|myInteger}} and {{C sharp|MyInteger}} of type {{C sharp/kw|int}} below are distinct because C# is case-sensitive:

<sourcesyntaxhighlight lang="csharp">

int myInteger = 3;

int MyInteger = 5;

</source>

</syntaxhighlight>

For example, C# defines a class {{C sharp|Console}} to handle most operations with the console window. Writing the following code would result in a compiler error unless an object named {{C sharp|console}} had been previously defined.

<sourcesyntaxhighlight lang="csharp">

// Compiler error!

console.writeline("Hello");

</syntaxhighlight>

</source>

The following corrected code compiles as expected because it uses the correct case:

<sourcesyntaxhighlight lang="csharp">

Console.WriteLine("Hello");

</syntaxhighlight>

</source>

{{BookCat}}

[[ko:C 샤프 프로그래밍/문법]]