Logo
.NET Core
.NET CoreBasic Syntax

Basic Syntax

In this section, we'll explore the fundamental syntax and structure of a .NET Core application. Understanding these basics is essential for writing clean, efficient, and maintainable code.

Program Structure

A typical .NET Core application consists of the following components:

  • Namespace: A namespace is a container that holds a set of identifiers and allows you to organize your code.

  • Class: A class is a blueprint for creating objects. It encapsulates data and methods that operate on the data.

  • Main Method: The Main method serves as the entry point for the application. Execution starts here.

Here's a simple example:

using System;
 
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Key Syntax Elements

  1. Comments: Comments are used for code documentation. Single-line comments start with //, and multi-line comments are enclosed between /* and */.

    // This is a single-line comment
    /* This is a
       multi-line comment */
  2. Variables: Variables are used to store data. You must declare a variable's type when you define it.

    int x = 10;
    string name = "John";
  3. Data Types: .NET Core supports various data types like int, float, double, char, string, and bool.

  4. Operators: Operators like +, -, *, /, and % are used for mathematical operations.

  5. Control Statements: if, else, and switch are used for conditional operations, while loops like for, while, and do-while are used for repeated execution.

  6. Functions: Functions are blocks of code designed to perform specific tasks. They can take parameters and return values.

    int Add(int a, int b)
    {
        return a + b;
    }
  7. Access Modifiers: public, private, protected, and internal are used to set the accessibility of classes, methods, and other members.

By understanding these basic syntax elements, you'll be well-equipped to read and write .NET Core applications effectively.

Book a conversation with us for personalize training today!

Was this helpful?
Logo