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.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

  • 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.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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. 

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

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.

Simplilearn is the world's most popular online Bootcamp for learning digital economy skills, and it's here to help you do just that. Digital marketing and data science are just a few subjects we cover in-depth in our online courses.

You've come to the right place if you're interested in learning more about software development and working in the field. The Caltech CTME and the Indian Institute of Technology, Kanpur, has collaborated with Simplilearn to deliver their Software Development courses. In addition to more advanced topics like Competitive Programming, these courses teach the fundamentals of data structures and algorithms. As a software developer, data structures including trees, graphs, and queues will be taught to you.

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, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors