Home  >  Article  >  Backend Development  >  C# Data Types

C# Data Types

WBOY
WBOYOriginal
2024-09-03 15:02:381021browse

As the name suggests, the data type is the type of data you are going to store in your variable. The data type is used to suggest the compiler or interpreter which kind of data it is going to process and how much memory will be required for that data.

For Ex: int is a data type that stores numeric values and requires four bytes.

As C# is a strongly typed language, it is necessary to declare the type of variable or constant before using it. Appropriate use of data types saves memory and improves the performance of the application.

Syntax:

datatype <variable_name> = value;

Examples of C# Data Types:

1. int intVal = 55; In this example: int is a datatype, intVal is a variable_name, 55 is a value.

2. char charVal = ‘A’;

3. string strVal = “Hello World!”;

4. float floatVal = 15.5f;

5. bool boolVal = true;

Top 3 C# Data Types

C# data types are divided into three categories:

C# Data Types

1. Value Types

  • Directly stores the value of a variable in memory.
  • Accepts both signed and unsigned literals.

There are two types of value data types in C#:

  1. Predefined data types like int, char, bool, etc.
  2. User-defined data types like enum, struct, etc.

2. Reference Types

  • They store the address of variable i.e. they contain the reference to a variable.
  • If the data is changed by one variable, the other variable will automatically get the updated value.

There are two types of reference data types in C#:

  1. Predefined types like Object, String
  2. User-defined types like Class, Interface

3. Pointer Types

  • They contain the memory address of the variable.

Symbols used in pointer:

  1. &(ampersand): Address operator, determines the address of a variable
  2. *(asterisk): Indirection operator, access the value of an address

Examples of Different Data Types

Following are some examples of different data types in C#,

Example #1: Some Value Types

using System;
public class ValueDataTypes
{
public static void Main()
{
//int - 32-bit signed integer type
int i = 55;
//char - 16-bit Unicode character
char ch = 'A';
//short - 16-bit signed integer type
short s = 56;
//long - 64-bit signed integer type
long l = 5564;
//uint - 32-bit unsigned integer type
uint ui = 100;
//ushort - 16-bit unsigned integer type
ushort us = 80;
//ulong - 64-bit unsigned integer type
ulong ul = 3625573;
//double - 64-bit double precision floating point type
double d = 6.358674532;
//float - 32-bit single-precision floating point type
//float needs 'f' or 'F' as suffix
float f = 2.7330645f;
//decimal - 128-bit precise decimal values with 28-29 significant digits
//decimal needs 'm' or 'M' as suffix
decimal dec = 339.5m;
Console.WriteLine("Integer: " + i);
Console.WriteLine("Char: " + ch);
Console.WriteLine("Short: " + s);
Console.WriteLine("Long: " + l);
Console.WriteLine("Unsinged integer: " + ui);
Console.WriteLine("Unsinged short: " + us);
Console.WriteLine("Unsinged long: " + ul);
Console.WriteLine("Double: " + d);
Console.WriteLine("Float: " + f);
Console.WriteLine("Decimal: " + dec);
}
}

Output:

C# Data Types

Example #2: Bool, Enum, and Struct Data Type

The structure is a composite type used to store related data with different data types. Enum is used to assign names to integral constants.

using System;
public class BoolEnumStruct
{
//declaring enum
enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
//declaring structure
struct Student
{
public int Id;
public string FirstName;
public string LastName;
public Student(int id, string fname, string lname)
{
Id = id;
FirstName = fname;
LastName = lname;
}
}
public static void Main()
{
//boolean data type
bool flag = true;
if(flag)
{
Console.WriteLine("Bool value: "+flag);
Console.WriteLine();
}
//Accessing enum value for Friday
Console.WriteLine("Enumeration:");
Console.WriteLine(Days.Friday);
Console.WriteLine((int)Days.Friday);
Console.WriteLine();
//passing values to structure members using constructor
Student student = new Student(1, "Riya", "Sen");
Console.WriteLine("Structure Members:");
Console.WriteLine(student.Id);
Console.WriteLine(student.FirstName);
Console.WriteLine(student.LastName);
}
}

Output:

C# Data Types

Example #3: Reference Data Types

using System;
public class StrObjDynamic
{
public static void Main()
{
string str = "C# ";
str += "Data Types";
Console.WriteLine("String: "+str);
Console.WriteLine();
//declaring object
object obj;
obj = 100;
Console.WriteLine("Object: "+obj);
//displaying type of object using GetType()
Console.WriteLine(obj.GetType());
Console.WriteLine();
//declaring dynamic variables
dynamic value1 = "Hello World!";
dynamic value2 = 5296;
dynamic value3 = 6.5;
//displaying actual type of dynamic variables using GetType()
Console.WriteLine("Dynamic:");
Console.WriteLine("Type of value1: {0}", value1.GetType().ToString());
Console.WriteLine("Type of value2: {0}", value2.GetType().ToString());
Console.WriteLine("Type of value3: {0}", value3.GetType().ToString());
}
}

Output:

C# Data Types

Example #4: Interface

An interface can have properties, methods, events, and indexers as its members. It only contains the declaration of its members. The implementation of its members is provided by the class implementing it implicitly or explicitly.

using System;
interface Shape
{
void rectangle();
}
public class Area : Shape
{
//implementing interface method
public void rectangle()
{
Console.WriteLine("Area of rectangle is Length * Breadth");
}
public static void Main(String[] args)
{
Area area = new Area();
area.rectangle();
}
}

Output:

C# Data Types

Example #5: Delegate

A delegate is an object that holds the reference to the method.

using System;
public class DelegateDemo
{
// Declaring delegate
public delegate void Sum(int a, int b);
public void SumVal(int a, int b)
{
Console.WriteLine(a +"+"+ b+ " = {0}", a + b);
}
public static void Main(String[] args)
{
DelegateDemo delegateDemo = new DelegateDemo();
// Creating object of delegate
Sum sum = new Sum(delegateDemo.SumVal);
//Passing values to the method using delegate object
sum(100, 100);
}
}

Output:

C# Data Types

Conclusion

  • The value type is stored in the stack.
  • A reference type is stored in heap.
  • When a value type is converted to a reference type it is called boxing(implicit conversion process).
  • When a reference type is converted to a value type it is called unboxing(explicit conversion process).

The above is the detailed content of C# Data Types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# VersionsNext article:C# Versions