C # Variables and Data Types

 

C # Variables 

M.SARULATHA
Assistant Professor
Dept. of CA, JJC.

General rules for word formation (variables) are:

• Names that can contain letters, numbers and lowercase letters (_)

• Words must start with a letter

• Words should start with a lower case letter and may not contain white

• Sensitive words ("myVar" and "myvar" have different meanings)

• Reserved words (like C # # key, like int or double) cannot be used as words

        Variable is not something other than the name given to a storage space that can be used by our systems. Each variation in C # has a specific type, which determines the size and memory structure of the variation the range of memory that can be stored within that memory and a working set of variables.The basic value types provided in C# can be categorized as −

Type

Example

Integral types

sbyte, byte, short, ushort, int, uint, long, ulong, and char

Floating point types

float and double

Decimal types

decimal

Boolean types

true or false values, as assigned

Nullable types

Nullable data types

C # also describes other types of value variants such as enum and reference types of class-like variables.

Defining Variables

Syntax for variable definition in C# is −

<data_type> <variable_list>;

           Here, data_type must be a valid type of C # data including char, int, float, duplicate, or any user-defined data type, and variable_list may have one or more identifiable words separated by comments.

int i, j, k;

char c, ch;

float f, salary;

double d;

You can initialize a variable at the time of definition as −

int i = 100;

Initializing Variables

The variable is started (given value) with the corresponding symbol to be followed by a common expression. Standard startup -

variable_name = value;

Variables can be started in their announcement. The launcher contains a symbol followed by a standard word as –

int d = 3, f = 5;    /* initializing d and f. */

byte z = 22;         /* initializes z. */

double pi = 3.14159; /* declares an approximation of pi. */

char x = 'x';        /* the variable x has the value 'x'. */

         It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result.

The following example uses various types of variables −

Live Demo

using System;

 

namespace VariableDefinition {

   class Program {

      static void Main(string[] args) {

         short a;

         int b ;

         double c;

 

         /* actual initialization */

         a = 10;

         b = 20;

         c = a + b;

         Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);

         Console.ReadLine();

      }

   }

}

When the above code is compiled and executed, it produces the following result −

a = 10, b = 20, c = 30

Accepting Values from User

The Console class in the System namespace provides a function ReadLine() for accepting input from the user and store it into a variable.

For example,

int num;

num = Convert.ToInt32(Console.ReadLine());

The function Convert.ToInt32() converts the data entered by the user to int data type, because Console.ReadLine() accepts the data in string format.

Lvalue and Rvalue Expressions in C#

Here are two types of expressions in C # -

·        lvalue - A maritime idiom can be seen as the left or right part of a distribution.

·         rvalue - The expression radiation may appear to the right of a particular assignment.

          The variables are functions so they can appear on the left side of the assignment. What is done with thousands of text numbers is rough so they may not be shared and may not appear on the left-hand side. The following is a valid C # statement -

int g = 20;

But the following is not a valid statement and may result in an error in the compilation time -

10 = 20;        

C # Data Types

  The type of data defines the size and type of different values. It is important to use the correct data type for the corresponding variable; to avoid mistakes, saving time and memory, but it will also keep your code running and readable. The most common types of data are:

Data Type Details

Int 4 byte Stores total values ​​from 2,147,483,648 from 2,147,483,647

8 long bytes whole numbers from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

4th sequence Enough to keep 6 to 7 numbers

8 numbers byte Stores combined numbers. It is enough to keep 15 decimal numbers

Bool 1 bit Stores true or false prices

Char 2 bytes Saves one character / character, surrounded by single quotes

String 2 bytes of each letter Keeps a sequence of letters, surrounded by double quotes

Numbers

Number types are divided into two groups:

          Integer Types

          Floating points Types

          Integer models keep whole values, good or bad (like 123 or -456), without decimals. Valid types are int and long. What type to use, depending on the number of numbers.

          The floating point types represent the numbers in the forced part, containing one or more cents. Valid species float and double.

 Integer Types

Int

          The int data type can store whole values ​​from 2147483648 to 2147483647. In general, and in our study, the int data type is the preferred data type when we create variables in numerical values.

Long

          A longer type of data can keep total values ​​from

-9223372036854775808 to 9223372036854775807. This is used when the int is not far enough to store the value. Note that you must end a value with an "L":

Floating points Types

          You should use a floating point type whenever you need a decimal number, such as 9.99 or 3.14515.

Floating

          Floating data type can store volatile numbers from 3.4e - 038 to 3.4e + 038. Note that you must end the value with an "F":

Double

          Duplicate data types can store mixed numbers from 1.7e - 308 to 1.7e + 308. Note that you can end the value with a "D" (although not required):

Use to float or double?

          Determining the value of a floating point indicates how many values ​​it can have after a decimal point. Floating precision with six- or seven-digit digits only, while dual variables have 15-digit accuracy. It is therefore safe to use double counting.

Comments

Popular posts from this blog

Operating System Structure

Asymptotic Notations

ASP.NET Events Handling