DEV Community

mohamed Tayel
mohamed Tayel

Posted on • Edited on

3

Mastering C# Fundamentals: C# Statements

Meta Description: Learn the essential C# syntax, covering topics like statements, identifiers, keywords, and variables. This guide simplifies the foundational concepts of C# programming for beginners.

In this guide, we’ll explore one of the fundamental aspects of C# programming—statements—along with related concepts like identifiers, comments, and variables. These building blocks are critical to understanding how C# programs operate and will help you start coding effectively in C#.


Understanding the Essential C# Building Blocks

C# syntax has certain rules and building blocks that make up any program. Let’s explore these essential elements starting with statements.


C# Statements

A statement in C# is an instruction that tells the computer to perform a specific action, such as printing text, performing a calculation, or taking user input. All statements must end with a semicolon (;).

Here’s a basic example of a C# statement:

Console.WriteLine("Hello, World!");
Enter fullscreen mode Exit fullscreen mode

Statements are executed in the order they appear unless they are modified by control structures like loops or conditionals.

Image:

C# Statements Example

In this image, we can see a simple C# statement printing "Hello, World!" to the console.

Another important point about statements is the flow of execution—statements typically run in sequence unless changed by user input or control structures.

Image:

C# Statements Flow
This image illustrates the flow of actions in a C# program, ending with a semicolon as part of the syntax.


C# Identifiers

An identifier is the name you give to elements in your code, such as variables, methods, and classes. C# identifiers have a few important rules:

  • They can only contain letters, digits, and underscores.
  • They must start with a letter or an underscore, but cannot start with a digit.
  • Keywords (reserved words) cannot be used as identifiers.

Here’s an example:

string input = Console.ReadLine();  // Valid identifier
string 2_input = Console.ReadLine();  // Invalid identifier - starts with a digit
Enter fullscreen mode Exit fullscreen mode

Image:

C# Identifiers

In this image, we see a comparison between a valid identifier and an invalid identifier in C#. Identifiers must start with a letter or underscore.


Comments in C#

Comments in C# allow you to annotate your code, making it easier for others (or yourself) to understand the logic. Comments are not executed by the compiler. C# supports two types of comments:

  1. Single-line comments, which start with //:
   // This is a single-line comment
   Console.WriteLine("This line prints to the console.");
Enter fullscreen mode Exit fullscreen mode

In this example, we see a single-line comment that describes what the next line of code will do

  1. Multi-line comments, which are enclosed between /* and */:
   /*
   This is a multi-line comment.
   It can span multiple lines.
   */
   Console.WriteLine("This is another statement.");
Enter fullscreen mode Exit fullscreen mode

This example demonstrates the use of multi-line comments, often used for longer explanations.


C# Variables: Storing Data

Variables are used to store data in a C# program. A variable is a storage location that holds a value and is declared with a specific type (e.g., int, string). For example, an integer variable can only store integer values, and a string variable can only store text.

Here’s how to declare and assign a variable:

int age;  // Declaring a variable of type int
age = 25;  // Assigning a value to the variable
Enter fullscreen mode Exit fullscreen mode

You can also declare and assign a variable in a single line:

int age = 25;
Enter fullscreen mode Exit fullscreen mode

Variables are case-sensitive, meaning that age and Age are two different variables.

Image:

C# Variables
This image describes how a variable holds a value in C#, such as integers, strings, or dates. Variables are declared in a statement and can hold specific data types.


C# Keywords

C# comes with a set of reserved keywords that cannot be used as identifiers because they have special meanings in the language. These include words like int, string, if, class, and void.

Image:

C# Keywords
Here is a list of commonly used C# keywords, which are reserved words with predefined functions in the language.


Type-Safety in C#

C# is a type-safe language, meaning that each variable can only hold values of its declared type. For example, if you declare a variable as int, it cannot hold a string value. This helps prevent common programming errors.

Example of type-safety:

int age = 25;  // Valid assignment
age = "John";  // Invalid - cannot assign a string to an int
Enter fullscreen mode Exit fullscreen mode

Conclusion

By understanding C# statements, identifiers, comments, and variables, you now have a strong foundation in the syntax of the C# language. This is crucial for writing well-structured, functional programs. As you progress, you will encounter more advanced concepts, but these basics will always remain relevant.

Practice writing C# programs using variables, comments, and statements to reinforce your knowledge. Soon, you’ll be building more complex and powerful applications with ease.


Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more