Home To Know Codes C# Data Types

C# Data Types

316
0
SHARE
C# Data Types

Data Types are used everywhere in a programming language. It means, that you cannot use variable without data types. Data types are used to inform the compiler about what type value variable can store? This is a set of values and allowable operations on those values. Such as if you want to work with int value then you will have to assign int type variable to work with.

C-Sharp provides two types of data types

  • Value Types
  • Reference Types

Value Types

These data types contain the directly value. These types are created on the stack and derived from the class System.ValueType. Primitive types (Except strings), Enumeration, Structure are Value types.

Primitive types

  • Bool: Bool is one of the simple data types. It can contain only 2 values – “false” or “true”. The default value of Bool is “false”.
  • Int: Int is short for integer, a data type for storing number without decimals. When working with numbers, int is the most commonly used data type. Integers have several data types within C# depending on the size of the number they are supposed to store.
  • C# Data Types Table & Their Range
    C# Data Types Table & Their Range

    Example: 1

    C# Data Types Example Input
    C# Data Types Example Input

    C# Data Types Example Output
    C# Data Types Example Output
  • Char: Char is used for storing a single character.
  • Float: Float is one of the data types used to store the decimal value. In C# we have three floating point types: float, double, and decimal.
  • C# Float Data Types Table & Their Range
    C# Float Data Types Table & Their Range

    Example: 2

    C# Data Types Example Input
    C# Data Types Example Input

    C# Data Types Example Output
    C# Data Types Example Output

    Reference Types

    The Reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. Reference types are created on the Heap. The lifetime of the reference type is managed by the .NET framework. The default value for reference types is null reference. Assignment to a variable of a reference type creates a copy of the reference rather than a copy of the referenced value. Classes, strings, interfaces, arrays, and delegates are reference types.

Leave a Reply