C#.NET Data Types

 C# is a strongly typed language. It means we must declare the type of variable that indicates the kind of values it is going to store, such as integer, float, decimal, text, etc.

Example:

The following variables of different data types

string stringVar = "Hello World!!";

int intVar = 100;

float floatVar = 10.2f;

char charVar = 'A';

bool boolVar = true;


C# mainly categorized data types in to 3 types, They are

            Value data types

                     Reference Data type

            Pointer Data Type  

 

Value Data Type 

 The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literal.

Example: short, int,char,float, double,enum,struct types etc

Value type is again devided into 2 types

1.     Predefined Data Types:- such as integer, boolean and float etc.

2.     User defined Data Types:- such struct,enum etc 

 

DataType

Memory Size

Range

Char

1 byte

-128 to 127

signed char

1 byte

-128 to 127

unsigned char

1 byte

0 to 127

Short

2 bytes

-32,768 to 32,767

signed short

2 bytes

-32,768 to 32,767

unsigned short

2 bytes

0 to 65,535

Int

4 bytes

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

signed int

4 bytes

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

unsigned int

4 bytes

0 to 4,294,967,295

Long

8 bytes

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

signed long

8 bytes

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

unsigned long

8 bytes

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

Float

4 bytes

1.5 * 10-45 - 3.4 * 1038, 7-digit precision

Double

8 bytes

5.0 * 10-324 - 1.7 * 10308, 15-digit precision

Decimal

16 bytes

at least -7.9 * 10?28 - 7.9 * 1028, with at least 28-digit precision

 

Reference Data Types

 The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables.

If the data is changed by one of the variables, the other variable automatically reflects this change in value

Example: string, class, array types ,object and interface

 

Reference data types also divided into 2 types

1.     Pre-defined Types : object, string

2.     User defined types: classes , interfaces

 

Pointer Data Type

 The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value

Example: int * a;

 

Symbols used in pointer

& (ampersand sign)                Address operator                   Determine the address of a variable.

* (asterisk sign)                       Indirection operator               Access the value of an address.

 The pointer in C# language can be declared using * (asterisk symbol). 

Integer Types

Integer type data types are positive or negative whole numbers without decimal points.

int

The int data type can store whole numbers from -2147483648 to 2147483647. int data type is the preferred data type when we create variables with a numeric value

Example:

int intNum = 2000;

Console.WriteLine(intNum);

long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value

Example:

long lngNum = 20000000;

Console.WriteLine(lngNum);  

Float Types

The float and double data types can store fractional numbers. Note that you should end the value with an "F" for floats and "D" for doubles:

Example : 9.99 or 3.14515

 Use float or double?

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits,

while double variables have a precision of about 15 digits.Decimal , 28-29 significant digits (128 bit).

Therefore it is safer to use double for most calculations.

 Decimal

Decimals have much higher precision and are usually used within financial applications that require a high degree of accuracy. Decimals are much slower (up to 20X times in some tests)

than a double/float. 

Boolean data type

A boolean data type is declared with the bool keyword and can only take the values true or false:

 Example:

bool isCSharpFun = true;

bool isCSharpUnmanaged = false;

Console.WriteLine(isCSharpFun);   // Outputs True

Console.WriteLine(isCSharpUnmanaged);   // Outputs False

  

DateTime Data Type

We used the DateTime when there is a need to work with the dates and times in C#.

The DateTime has the Date and Time property. From DateTime, we can find the date and time. DateTime contains other properties as well, like Hour, Minute, Second, Millisecond, Year, Month, and Day 

Example:

DateTime dtCurrentDate= new DateTime();

Console.WriteLine($"CurrentDateTime: {dtCurrentDate}");

O/P:

It returns the default date i.e 01-01-0001 00:00:00

 

To get the current datetime

Console.WriteLine($"CurrentDateTime: {DateTime.Now.ToString()}");

 

The other properties of DateTime are:

·       We can get the name of the day from the week with the help of the DayOfWeek property.

·       To get the day of the year, we will use DayOfYear property.

·       To get time in a DateTime, we use TimeOfDay property.

·       Today property will return the object of the DateTime, which is having today's value. The value of the time is 12:00:00

·       The Now property will return the DateTime object, which is having the current date and time.

·       The Utc property of DateTime will return the Coordinated Universal Time (UTC).

·       The one tick represents the One hundred nanoseconds in DateTime. Ticks property of the DateTime returns the number of ticks in a DateTime.

·       The Kind property returns value where the representation of time is done by the instance, which is based on the local time, Coordinated Universal Time (UTC).

It also shows the unspecified default value.  

Example: 

            DateTime DateTimeProperty = new DateTime(2023, 7, 4, 8, 50, 24); 

            Console.WriteLine("Day:{0}", DateTimeProperty.Day); 

            Console.WriteLine("Month:{0}", DateTimeProperty.Month); 

            Console.WriteLine("Year:{0}", DateTimeProperty.Year); 

            Console.WriteLine("Hour:{0}", DateTimeProperty.Hour); 

            Console.WriteLine("Minute:{0}", DateTimeProperty.Minute); 

            Console.WriteLine("Second:{0}", DateTimeProperty.Second); 

            Console.WriteLine("Millisecond:{0}", DateTimeProperty.Millisecond); 

             Console.WriteLine("Day of Week:{0}", DateTimeProperty.DayOfWeek); 

            Console.WriteLine("Day of Year: {0}", DateTimeProperty.DayOfYear); 

            Console.WriteLine("Time of Day:{0}", DateTimeProperty.TimeOfDay); 

            Console.WriteLine("Tick:{0}", DateTimeProperty.Ticks); 

            Console.WriteLine("Kind:{0}", DateTimeProperty.Kind); 

 

O/P:





Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IISRESET vs App Pool Recycling ?

Deploy .Net6.0 Web api with docker