Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

C# Syntax


Sure, let's break down the basic syntax of C# in simple language with examples:

1. Namespaces:

Namespaces are used to organize code into logical groups. They prevent naming conflicts and make code more maintainable.

Example

using System;
using System.Collections.Generic;

2. Classes:

Classes are the building blocks of C# programs. They contain data and methods (functions) that operate on that data.

Example

class MyClass
{
// Class members go here
}

3. Methods (Functions):

Methods contain the executable code in a program. They perform specific tasks when called.

Example

class MyClass
{
// Method definition
public void MyMethod()
{
Console.WriteLine("Hello, World!");
}
}

4. Variables and Data Types:

Variables are used to store data in memory. Data types specify the type of data that a variable can hold.

Example

int age = 25;
double pi = 3.14;
string name = "John";
bool isTrue = true;

Control Flow:

Control flow statements control the order in which instructions are executed in a program.

5. - If-Else Statements:

Example

int x = 10;

if (x > 5)
{
Console.WriteLine("x is greater than 5");
}
else
{
Console.WriteLine("x is less than or equal to 5");
}

- Loops:

For Loop:

Example

for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}

While Loop

Example

int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}

6. Arrays:

Arrays are used to store multiple values of the same type in a single variable.

Example

int[] numbers = { 1, 2, 3, 4, 5 };

7. Strings:

Strings are used to store text.

Example

string greeting = "Hello, World!";

8. Conclusion:

This is just a basic overview of the syntax of C#. As you continue learning, you'll explore more advanced topics such as object-oriented programming, exception handling, and working with external libraries. Practice writing code and experimenting with different syntax elements to solidify your understanding.

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java