ArticleslgStudy

science

C Sharp syntax

C Sharp syntax is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand C Sharp syntax rather than just read about it. In short: This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

C Sharp syntax — main illustration
C Sharp syntax — illustration

Key takeaways

  • C Sharp syntax belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect C Sharp syntax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of C Sharp syntax from memory before moving on to harder problems.

Reference excerpt

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

Basics

Identifier An identifier is the name of a code element. It can contain letters, digits and underscores (_), and is case sensitive (FOO is different from foo). The language imposes the following restrictions on identifier names:

They cannot start with a digit; They cannot start with a symbol, except for the at sign (@), which can be used to prefix keywords to allow them as identifiers; They cannot contain more than 511 characters. Identifier names may be prefixed by @, which allows the use of reserved keywords as identifiers (for example, @class), but the @ is not considered part of the identifier's name for most purposes; @name and name refer to the same identifier. Microsoft offered naming conventions for identifiers in C#, recommending the use of PascalCase for the names of types and most type members, and camelCase for variables and for private or internal fields. These naming conventions are not enforced by the language.

Keywords Keywords are words with syntactic meaning. The language has two types of keyword — contextual and reserved. Reserved keywords such as false or byte may only be used as keywords. Contextual keywords such as where or from are treated as keywords only in certain situations. If an identifier is needed that matches a reserved keyword, it is prefixed by an @ sign to distinguish it. For example, @out is interpreted as an identifier, whereas out is a keyword. This syntax facilitates reuse of .NET code written in other languages. The following C# keywords are reserved words, of which there are 77.

Contextual keywords A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#. Some contextual keywords, such as partial and where, have meanings in multiple contexts. The following 42 C# keywords are contextual.

Preprocessor directives Although C# does not have a separate preprocessor, unlike C and C++ which use the C preprocessor, these 13 directives are processed as if there were one.

Literals

Digit separators Starting in C# 7.0, the underscore symbol can be used to separate digits in number values for readability purposes. The compiler ignores these underscores.

Generally, separators may be put only between digit characters. It cannot be put at the beginning (_121) or the end of the value (121_ or 121.05_), next to the decimal in floating point values (10_.0), next to the exponent character (1.1e_1), or next to the type specifier (10_f).

Variables Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement. Declare

Assigning

Initialize

Multiple variables of the same type can be declared and initialized in one statement.

Local variable type inference C# 3.0 introduced type inference, allowing the type specifier of a variable declaration to be replaced by the keyword var, if its actual type can be statically determined from the initializer. This reduces repetition, especially for types with multiple generic type-parameters, and adheres more closely to the DRY principle.

Constants Constants are immutable values.

const When declaring a local variable or a field with the const keyword as a prefix the value must be given when it is declared. Thereafter the value cannot change. They can either be declared in the context as a field or a local variable. Constants are implicitly static.

This shows both uses of the keyword.

readonly The readonly keyword does a similar thing to fields. Like fields marked as const their value cannot change once initialized. The difference is that one can choose to initialize them in a constructor, or to a value that is not known until run-time. This works only on fields. readonly fields can either be members of an instance or static class members.

Code blocks Curly braces { ... } are used to signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts. Inside of method bodies, braces can be used to create new scopes:

Program structure A C# application consists of classes and their members. Classes and other types exist in namespaces and can be nested inside other classes.

Main method Every program must have an entry point. The entry point of a C# application is the Main method. There can only be one declaration of this method, and it is a static method in a class. It usually returns void and is passed command-line arguments as an array of strings.

The main method may return an integer value if specified.

Async Main Asynchronous Tasks can be awaited in the Main method by declaring the method's return type as System.Threading.Tasks.Task.

All combinations of System.Collections.Generic.Task, or System.Collections.Generic.Task<int>, and with, or without, the string[] args parameter are supported.

Top-level statements Similar to in scripting languages, top-level statements remove the ceremony of having to declare the Program class with a Main method. Instead, statements can be written directly in one file, which is the program entry point. Code in other files must be defined in classes.

This was introduced to make C# less verbose, and thus more accessible.Types are declared after the statements, and are available from the statements above them.

Namespaces Namespaces are a part of a type name and are used to group and/or distinguish named entities from other ones.

A namespace is defined with the namespace keyword:

A namespace can be nested inside a namespace in three ways:

and:

In C# 10 and later, namespaces can be defined using file-scoped declarations by doing the following:

using directive The using directive loads a specific namespace from a referenced assembly. It is usually placed in the top (or header) of a code file but it can be placed elsewhere, e.g. inside a class.

The directive can also be used to define another name for an existing namespace or type. This is useful when names are long.

using static directive The using static directive loads the static members of a specified type into the current scope, making them accessible directly by the name of the member.

global using directive Since C# 10, it is legal to write global using directives to indicate a using directive once and apply it to all source files.

Operators

… excerpt ends here. Continue reading the full article.

Illustrations

C Sharp syntax: A snippet of C# code
A snippet of C# code

Worked examples

Example 1 — a first encounter with C Sharp syntax

Start with the simplest possible case. Write down what C Sharp syntax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to C Sharp syntax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about C Sharp syntax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of C Sharp syntax

In research
C Sharp syntax appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses C Sharp syntax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
C Sharp syntax is common in secondary-school and first-year university syllabi. It links to neighbouring topics C Sharp (programming language), Programming language syntax, so understanding it makes those chapters shorter.
In everyday life
Look for C Sharp syntax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “C Sharp syntax” →

How to study C Sharp syntax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what C Sharp syntax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain C Sharp syntax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is C Sharp syntax in simple terms?

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

Why does C Sharp syntax matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study C Sharp syntax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on C Sharp syntax.

Tags

  • C Sharp (programming language)
  • Programming language syntax

Keep exploring