image

C# Tutorial 6 | C# Syntax - C# 7.0 Tutorials at EdwinLangley.co.uk

12 Jan 2024

C# Syntax

The syntax of C# was inspired by C and C++, two older languages.

If we look at the following example:

using System;

namespace HelloWorldApp { class Program { static void Main(string args) { //adding 42 to 21 int addingResult = 42 + 21; Console.WriteLine(result); } } } We can already see a few elements that define the core syntax of C#.

Literals, Punctuators and Operators

Literals are primitive pieces of data that are directly represented. An example of a literal is the number 42 and can be directly used in a statement such as:

int intTotal = 42 + 12; Punctuators are characters that help define the structure of the code, they might denote where a code block starts and end.

{ } ; //Examples of Punctuators

int SquaredNumber(int input) { // start of function return input * input ; } //end of function As you can see in the above example the “; ” character denotes the end of a statement.

An operator symbol transforms the operands that are used in conjunction with them. There are many operators in the C# Language that can be split up into groups. An example of one of these groups is the arithmetic operators and contain the following:

  • + The addition operator to add two operands e.g. (a = b + c)
  • - The subtraction operator to subtract operands e.g. (a = b - c)
  • * The multiplication operator to multiply operands e.g. (a = b * c)
  • / The division operator to divide operands e.g. (a = b / c)
  • % Modulus operator for int remainder after division e.g. ( 3 % 2 == 1)
  • ++ The increment operator to increase value by one e.g. ( a++ == 10 )
  • -- The decrement operator to decrease value by one e.g. ( a-- == 8 )

Identifiers and Keywords

An identifier is a meaningful word used to represent variables, classes or methods. This word is chosen by the programmer.

For example, in the above code, the integer value uses an identifier called ‘addingResult’ – it was named this as it describes the data it holds. In this case, it holds the summation of the values 42 and 2.

int addingResult = 42 + 2; C# is a case sensitive language, therefore AddingResult and addingResult would not point to the same values.

There are a number of words that are reserved by the language. These keywords cannot be used as identifiers.

abstract add as ascending async await base bool break by byte case catch char checked class const continue decimal default delegate descending do double dynamic else enum equals explicit extern false finally fixed float for foreach from get global goto group if implicit in int interface internal into is join let lock long namespace new null object on operator orderby out override params partial private protected public readonly ref remove return sbyte sealed select set short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using value var virtual void volatile where while yield If you really want to use an identifier that is the same as one of the keywords mentioned above then you can prepend the word with the @ character. The following would be a legal statement:

float @float = 1.2f; Contextual keywords are words that have meaning in the language but can still be used as an identifier:

add alias async await dynamic from get orderby ascending descending group into join let nameof global partial set remove select value var when Where yield

Indentation

Many would consider Indentation to be a core element of a programming languages Syntax. Indentation is not a requirement for the compiler to work but rather a means of making sure that the code is readable to developers.

using System; namespace HelloWorldApp {class Program{static void Main(string args) { int addingResult = 42 + 21; Console.WriteLine(result); } } } This is the same code that was written as before, this time it has not been indented. The compiler will not struggle to understand the code as C# does not rely on indentation unlike languages like Python.

Python and indentation

Comments

To help a programmer keep track of their method, many programming languages support code comments. Comments are ignored by the compiler and therefore are useful to document.

There are two main types of comment:

Single Line Comments

A single line comment will run from two forward slash characters until the end of the line

//This is an example of a single line comment

Multi Line Comments

A multiline comment will run from the start characters /* to the end characters.

/* This is an example of a multiline comment */