One-Stop Solution to C# Variable: An Ultimate Guide With Examples

C# has been developed to handle new workloads and new programming paradigms. Before using a variable in C#, it must be declared. A variable's name and type must be known before assigning a value. Thus, C# is a statically typed language.

In this 'C# Variable' tutorial, you will learn the major technical aspects of variables and fundamentals involving data types.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Is a C# Variable?

A variable is simply a one-of-a-kind identifier for a storage location that our program can access. The varieties of C# Variables determine their memory size, value range, and set of operations.

Each variable's datatype must match the data being saved. When we create a variable, the system reserves a chunk of memory equal to the variable's size.

We can now go on to the next chapter to learn about the different sorts of variables now that we have a basic knowledge of the definition and complexity of C# variables.

What Are the Types of Data Types of C# Variable? 

C#-Variable-datatype-img1

There are mainly three types of C# datatype, as explained below.

1. Numeral Data Type

C#-Variable-numeral-img1

  • Signed

Data Type

Bit Representation

Value Range

sbyte

2⁸

-128 to 127

short

2¹⁶

-32768 to 32767

int

2³²

-2,147,483,648 to 2,147,483,647

long

2⁶⁴

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

  • Unsigned

Data Type

Bit Representation

Value Range

byte

2⁸

0 to 255

ushort

2¹⁶

0 to 65536

uint

2³²

0 to 4,294,967,296

ulong

2⁶⁴

0 to 18,446,744,073,709,551,615

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

  • Floating Point

Data Type

Approx value Range

Digit Precision

Float

-3.4 x 1038  to +3.4 x 1038 

7

Double

±5.0 x 10-324  to ±1.7 x 10308

15-16

Decimal

(-7.9 x 1028  to +7.9 x 1028)/(100 to 1028)

28-29

2. Special Type

C#-Variable-special-img1.

  • Character Datatype: A single character is stored in a char data type. 'A' or 'c' must be encased in single quotation marks.
  • String Datatype: A string is a sequence of characters stored in the string type (text). Double quotes must be used to delimit string values.
  • Boolean Datatype: True and false are the only possible values for a boolean data type, defined by the bool keyword.

So far, we have learned about the major data types and the c# variables. Now let’s discuss the practical implementation of data types.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Implementation of Data Types and C# Variables?

In C#, we will implement various data types and variables. 

Code: 

using System;

namespace C__Primitive_variable

{

    class Program

    {

        static void Main(string[] args)

        {

            //signed integer types

           sbyte sb =-120;

            short s = 30000;

            int i = 2000000000;

            long l = 1000000000000;

            Console.WriteLine("sb= "+sb);

            Console.WriteLine("s= "+s);

            Console.WriteLine("i= "+i);

            Console.WriteLine("l= "+l);

            //unsigned Integer types

            byte b = 122;

            ushort us = 60123;

            uint ui = 400000000;

            ulong ul= 18000000000000;

            Console.WriteLine("b= "+b);

            Console.WriteLine("us= "+us);

            Console.WriteLine("ui= "+ui);

            Console.WriteLine("ul= "+ul);

            // floating point type

            float f = 12.7f;

            double d = 23.67;

            decimal dm = 99.1079m;

            Console.WriteLine("f= "+f);

            Console.WriteLine("d= "+d);

            Console.WriteLine("dm= "+dm);      

      // other variable types

            char c = 't';

            bool flag = true;

            string str = "Welcome to Simplilearn";

            Console.WriteLine("c= "+c);

            Console.WriteLine("flag= "+!flag);

            Console.WriteLine("str= "+str);

        }

    }

}

C#-Variable-Implement-img1

By now, you have a good grip on the technical aspects of C# Variables. 

Accelerate your career as a skilled MERN Stack Developer by enrolling in a unique Full Stack Developer - MERN Stack Master's program. Get complete development and testing knowledge on the latest technologies by opting for the MERN Stack Developer Course. Contact us TODAY!

Next Steps

The next lesson in your C# training can be "Operators and Expressions in C#." There are a variety of symbols that can be used to do a variety of functions, including operators. Variables and values are nothing new to operators. The combination of operands and operators makes up an expression in C#. At the very least, an expression requires an operand; nonetheless, it can have no operators.

Everything from data types to properly declaring and using variables was covered in this guide. You now have a strong base upon which to erect the C# programs' building components. Remember that learning is best accomplished by doing, so take a moment to experiment with the examples we looked at.

Ready to advance your C# knowledge? Consider enrolling in a course called Full Stack Developer - MERN Stack. The road of creating dynamic web applications doesn't end with learning variables when you complete this training!

The comments section below is open for questions and comments about this 'C# Variable' tutorial. Happy learning!

About the Author

Vaibhav KhandelwalVaibhav Khandelwal

Vaibhav Khandelwal is a proactive tech geek who's always on the edge of learning new technologies. He is well versed in competitive programming and possesses sound knowledge of web development. He likes to read fictional and sci-fi novels and likes to play strategy games like chess

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.