Tuesday, 10 December 2019

C# Session 4: Variable in C#

Variable in C#

A variable in C# is a given name to a storage space in a program as well as a given name to a data value. Each variable has its type and size w.r.t a value.

Defining A Variable:

A variable can be declared using following ways:
  • <data type> <variable name>;     i.e.     string name;
  • <data type> <variable name> = <value>;    i.e.     string name=“skmdotnet”;

Multiline Variable:

Multiple variables of same type can be declared as well as assigned using following way:
  • int a, b, c = 1; 
  • int a = 10, b = 20, c = 10;
  • int a = 0; string name = a; // Invalid assignment and will cause an error

Variable Assignment:

A value of one variable can be assigned to another variable of same type but it must be assigned before.

    int a = 1;
    int j = a;
    
    int a;
    int j=a; // Invalid assignment and will cause an error


Program We Discussed:

using System;
namespace CSharp.Tutorial
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Welcome to SKMDotNet");
            int i;      // decleration without assignement
            int j = 1;  // decleration with assignement

            int a = 10, b = 20, c = 10;   // Multiline declaration
            Console.WriteLine($"a: {a.ToString()}, b: {b.ToString()}, c: {c.ToString()}");
            string name = "abc";
            string name2 = name;    // Variable assignement
            Console.WriteLine(name2);
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Angular 8 Session 21 - ngFor Directive In Angular

ngFor Directive Content will be resume soon. Stay tuned!