c sharp Logo

C# Syntax


C# syntax is the set of rules that govern how C# code is written. These rules define the structure of C# programs, the keywords and identifiers that can be used, and the operators and expressions that can be formed.

Basic Syntax

A C# program is made up of one or more classes. A class is a blueprint for creating objects. Each class contains data members and methods. Data members store data about the object, while methods define the behavior of the object.

Here is an example of a simple C# class:

C#

public

 class

 Person

{

    private

 string name;

    private

 int

 age;

 

    public

 string Name

    {

        get

        {

            return name;

        }

        set

        {

            name = value;

        }

    }

 

    public

 int Age

    {

        get

        {

            return age;

        }

        set

        {

            age = value;

        }

    }

 

    public

 void

 PrintInfo()

    {

        Console.WriteLine("Name: " + name);

        Console.WriteLine("Age: " + age);

    }

}

 

This class defines a Person object with two data members, name and age, and two methods, PrintInfo() and GetName(). The PrintInfo() method prints the name and age of the person, while the GetName() method returns the name of the person.

Keywords

C# has a number of keywords that are used to define the structure of a program. These keywords include class, public, private, void, int, string, if, else, for, while, and return.

Identifiers

Identifiers are names that are used to identify classes, data members, methods, and variables. Identifiers must start with a letter or an underscore (_) and can contain letters, digits, underscores, and hyphens (-).

Operators

C# has a number of operators that are used to perform operations on data. These operators include arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >, <=, >=), logical operators (&&, ||, !), and assignment operators (=, +=, -=, *=, /=).

Expressions

Expressions are combinations of operands and operators that evaluate to a value. Operands can be variables, literals, or other expressions.

Statements

Statements are units of code that perform an action. Statements include variable declarations, assignments, method calls, and control flow statements (if, else, for, while).

Code Blocks

Code blocks are groups of statements that are enclosed in curly braces ({ }). Code blocks are often used to define the bodies of methods and loops.

Comments

Comments are non-executable code that is used to document a program. Comments are ignored by the compiler and are not part of the executable code. There are two types of comments in C#: single-line comments and multi-line comments. Single-line comments start with two forward slashes (//) and end with the end of the line. Multi-line comments start with a slash and an asterisk (/) and end with an asterisk and a slash (/).

Examples

Here are some examples of C# code:

// Declare a variable

int number = 10;

 

// Print a message to the console

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

 

// Create a new instance of the Person class

Person person = new Person();

 

// Set the name and age of the person

person.Name = "John Doe";

person.Age = 30;

 

// Print the information about the person

person.PrintInfo();

 

This code declares a variable, prints a message to the console, creates a new instance of the Person class, sets the name and age of the person, and then prints the information about the person.