DEV Community

firdavs090
firdavs090

Posted on

C# {Data Types except Int}

Floating-Point Types:

1)float: 32-bit single-precision floating point type
2)double: 64-bit double-precision floating point type

float myFloat = 20.5f;
Enter fullscreen mode Exit fullscreen mode

Decimal Type:

1)decimal: 128-bit precise decimal values with 28-29 significant digits, suitable for financial and monetary calculations

decimal myDecimal = 100.5m;
Enter fullscreen mode Exit fullscreen mode

Other Value Types:

1)char: 16-bit Unicode character
2)bool: Represents Boolean values, true or false

bool myBool = true;
char myChar = 'A';
Enter fullscreen mode Exit fullscreen mode

Reference Types:
String Type:

1)string: Represents a sequence of Unicode characters

string myString = "Hello, World!";
Enter fullscreen mode Exit fullscreen mode

Object Type:

1)object: The base type from which all other types derive

object myObject = myString;
Enter fullscreen mode Exit fullscreen mode

Dynamic Type

1)dynamic: Type resolved at runtime, providing flexibility at the cost of some performance and type safety

dynamic myDynamic = 10; // dynamic type can change

Enter fullscreen mode Exit fullscreen mode

Nullable Types

1)Nullable: Allows value types to be assigned null (e.g., int?, double?)

int? myNullableInt = null;
Enter fullscreen mode Exit fullscreen mode

Arrays:

1)Array: Represents a fixed-size sequence of elements of the same type

int[] myArray = {1, 2, 3, 4, 5};
Enter fullscreen mode Exit fullscreen mode

Enum Type:

1)enum: Represents a set of named constants

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
Day today = Day.Monday;
Enter fullscreen mode Exit fullscreen mode

Struct Type:

1)struct: A value type that can encapsulate data and related functionality

struct Point 
{
    public int X;
    public int Y;
}
Point p = new Point();
p.X = 10;
p.Y = 20;
Enter fullscreen mode Exit fullscreen mode

Class Type:

1)class: A reference type that can encapsulate data and related functionality

class MyClass {
    public int Number;
    public void Display() {
        Console.WriteLine(Number);
    }
}
MyClass obj = new MyClass();
obj.Number = 5;
obj.Display();
Enter fullscreen mode Exit fullscreen mode

Interface Type:

1)interface: Defines a contract that can be implemented by classes and structs

class MyClassWithInterface : IMyInterface 
{
    public void MyMethod() 
{
        Console.WriteLine("Interface Method");
    }
}
IMyInterface myInterface = new MyClassWithInterface();
myInterface.MyMethod();
Enter fullscreen mode Exit fullscreen mode

Delegate Type:

1)delegate: Represents a reference to a method

delegate void MyDelegate(string message);
MyDelegate del = (msg) => Console.WriteLine(msg);
del("Hello, Delegate!");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)