Both C# and Java inherit and develop based on the C and C++ platforms, so basically, the syntax of C# is quite similar to that of Java. C# is essentially an object-oriented programming language that demands a high-level structure. A C# program is defined by classes, and their behavior details are defined by their statements. To better understand the basic syntax of C#, readers can refer to the following article by Mytour.
Basic syntax of C#, essential conventions, and keywords to know
1. Basic syntax of C#.
2. Comments in C#.
2.1. Single-line comments in C#.
2.2. Multi-line comments in C#.
3. Identifiers (identifier) in C#.
4. Keywords in C#.
5. Important notes.
1. Basic syntax of C#
To understand the basic syntax of a C# program, refer to the example of the Rectangle object below. This object has properties such as length and width. All you need to do is dissect this C# program to understand the basic syntax of C#:
After compiling and running the above C# program, it will return output results as shown below:
2. Comments in C#
The C# compiler ignores comments, so comments are used to explain code, debug programs, and remind future code. There are 2 types of comments in C#:
+ Single-line comment.
+ Multi-line comment.
2.1 Single-line comment in C#
Single-line comments in C# begin with //. That means all words on the same line after // are considered single-line comments. Below is an example of a single-line comment in C#:
// single-line comment
2.2 Multi-line comments in C#
Multi-line comments in C# start with /* and end with */. This type of comment has no limit on the number of lines users can use. Below is an example of a multi-line comment in C#:
/* hello,
* i am multi-line
* comment */
3. Identifiers (identifier) in C#
Identifiers in C# are used to define variables, classes, functions, or any user-defined identifiers. Below are the general and basic rules for naming identifiers:
- The identifier name must start with a letter, followed by a sequence of letters, digits (from 0 to 9), or underscore.
- The identifier name must not be a keyword in C#.
4. Keywords in C#
Keywords in C# are reserved words with special meanings to the C# compiler.
Note: In case you intend to use a keyword as an identifier, you must prepend the prefix, the character @ before the keyword.
Below is a table listing the reserved keywords in C#:
In C# programming, certain identifiers have special meanings in the context of the code, such as set and get , which are referred to as contextual keywords.
Below is a table listing contextual keywords in C#:
5. Important Note
- C# distinguishes between uppercase and lowercase letters in variable and method names.
The variables myInteger and MyInteger of type int differ because C# is case-sensitive:
int myInteger = 3;
int MyInteger = 5;
In C#, the Console class is defined to handle operations on the console window. The code below will return a compiler error message unless an object named console has been defined previously:
// Compiler error!
console.writeline('Hello');
The correct code is as follows:
Console.WriteLine('Hello');
This article by Mytour introduces you to basic C# syntax. In the following articles, Mytour will introduce you to data types in C#. Additionally, readers can refer to other articles on Mytour to learn more about setting up the environment for C#.