Hey! If you love C# and building C# apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Syntax and Basic Constructs

Master the fundamentals of C# syntax and basic constructs to unlock the full potential of this powerful programming language. Learn about variables, data types, operators, control structures, and more - your journey to C# proficiency starts here!


Updated October 18, 2023

In this article, we’ll cover the basics of C# syntax and basic constructs that every C# developer should know. Whether you’re a beginner or an experienced developer looking to brush up on your skills, this guide will provide you with a solid foundation for building robust and efficient C# applications.

Basic Syntax

C# is a strongly-typed language that uses the curly brace syntax to define code blocks. Here’s an example of a basic C# program:

console.WriteLine("Hello, World!");

This code simply outputs “Hello, World!” to the console. Let’s take a closer look at the syntax:

  • console.WriteLine() is a method that writes a string to the console.
  • "Hello, World!" is the string being written to the console.

Variables and Data Types

In C#, variables are used to store values. There are several data types in C#, including:

  • int - for integers
  • double - for floating-point numbers
  • bool - for booleans
  • string - for strings

Here’s an example of declaring and using a variable:

int age = 25;
console.WriteLine("I am " + age + " years old.");

In this code, we declare an age variable and assign it the value 25. We then use the + operator to concatenate the string “I am " with the value of age, and output the result to the console.

Control Flow

C# provides several control flow statements that allow you to control the order in which your code is executed. Here are some examples:

  • if - checks a condition and executes a block of code if the condition is true
if (age > 18)
{
    console.WriteLine("I am an adult.");
}
else
{
    console.WriteLine("I am not yet an adult.");
}

In this code, we check if age is greater than 18, and if it is, we output “I am an adult.” to the console. Otherwise, we output “I am not yet an adult.”

  • while - executes a block of code repeatedly while a condition is true
int i = 0;
while (i < 5)
{
    console.WriteLine(i++ + " is my favorite number.");
}

In this code, we initialize an i variable to 0, and then use a while loop to execute the code inside the loop body repeatedly until i is greater than or equal to 5. Each iteration of the loop increases the value of i by 1.

  • for - executes a block of code once for each item in a collection
string[] colors = { "red", "green", "blue" };
foreach (string color in colors)
{
    console.WriteLine(color);
}

In this code, we define an array of strings colors containing the values “red”, “green”, and “blue”. We then use a foreach loop to iterate over each item in the collection, outputting each item to the console.

Classes and Objects

C# is an object-oriented language that supports classes and objects. Here’s an example of defining a class:

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

In this code, we define a Person class with two properties: Name and Age. These properties are declared as public, which means they can be accessed from outside the class.

Here’s an example of creating an instance of the Person class:

Person person = new Person();
person.Name = "John";
person.Age = 30;

In this code, we create a new instance of the Person class and set its Name and Age properties to “John” and 30, respectively.

Conclusion

In this article, we covered the basics of C# syntax and basic constructs. We discussed variables and data types, control flow, and classes and objects. These concepts are essential for building robust and efficient C# applications, and we hope this guide has provided you with a solid foundation for your future development endeavors. Happy coding!