The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Sunday, 4 June 2023

How to Declaration and assign of variable in C#


Variable

Variable is a named storage location that holds a value of a particular data type. It is used to store and manipulate data during the execution of a program. Variables have a specific type, which determines the kind of data they can hold, such as integers, floating-point numbers, characters, Boolean values, or custom types.

In C#, variables can be of different types, each representing a specific kind of data. Here are some commonly used types of variables in C#:

Numeric Types:
    • int: Represents whole numbers (e.g., 42).
    • float: Represents single-precision floating-point numbers (e.g., 3.14f).
    • double: Represents double-precision floating-point numbers (e.g., 3.14).
    • decimal: Represents decimal numbers with higher precision (e.g., 3.14m).
    • byte: Represents unsigned integers (0 to 255).
    • short: Represents small integers (-32,768 to 32,767).
    • long: Represents large integers (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

Character Types:
    • char: Represents a single character (e.g., 'A', '5', '$').

Boolean Type:
    • bool: Represents a Boolean value (either true or false).

String Type:
    • string: Represents a sequence of characters (e.g., "Hello, World!").

Enumerations:
    • enum: Represents a set of named values (e.g., days of the week, months).

Arrays:
    • type[]: Represents a collection of elements of a specific type (e.g., int[], string[]).

Object Type:
    • object: Represents any type of data (a reference to an instance of a class).

Create Variables

You can declare variables using the following syntax:

<type> <variableName>;

Here's an example of declaring variables of different types in C#:

int age; // declaring an integer variable named "age"
float temperature; // declaring a float variable named "temperature"
bool isStudent; // declaring a boolean variable named "isStudent"
string name; // declaring a string variable named "name"


You can also declare and initialize a variable in a single statement:

<type> <variableName> = <value>;

Here's an example:

int count = 0; // declaring and initializing an integer variable named "count" with a value of 0
float pi = 3.14f; // declaring and initializing a float variable named "pi" with a value of 3.14
bool isActive = true; // declaring and initializing a boolean variable named "isActive" with a value of true
string message = "Hello, world!"; // declaring and initializing a string variable named "message" with a value of "Hello, world!"

It's important to note that the variable name should follow certain rules:

    • It must start with a letter or underscore (_).
    • It can contain letters, digits, and underscores.
    • It cannot contain spaces or special characters (except underscores).

It's also recommended to use meaningful names for variables to make your code more readable and maintainable.

A demonstration of how to declare variables of different types with a screenshot:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VariableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string Name = "Amit Kumar";
            int Age = 50;
            float num1 = 85.45f;
            double num2 = 524.2;
            char ch = 'A';
 
            Console.WriteLine("All information are :");
            Console.WriteLine(Name);
            Console.WriteLine(Age);
            Console.WriteLine(num1);
            Console.WriteLine(num2);
            Console.WriteLine(ch);
 
            Console.ReadLine();
 
        }
    }
}


Output: 


0 comments:

Post a Comment

Do not enter spam link

Popular Posts