In this article I am going to discuss about different types of C# data types but before that if you have not read my previous article which is of C# basic structure and methods of console class in csharp langauge. As a developer, it is important to understand the C# data types because you need to decide which data type to use for a specific type of value.
What is DataTypes?
In programming, a data type is a classification that specifies which type of value a variable can hold. It defines the characteristics of the data that a variable can store, such as the range of values it can take and the operations that can be performed on it. Data types are essential in programming because they help ensure the proper use of variables and enable the compiler to allocate the right amount of memory for a variable.
Different Types of C# Data Types
Above is the simple image that showing the different types of C# data types used in programming. But most of the times you will only going to use the int, string , double, Object , class and interface. While if you have started the C# programming then in most of the programs you will only going to use predefined data types like int, float , double and string etc. But it does not mean that class and interfaces are not so much important. It is important but we will see this in our next articles.
Integer DataType
This type of data type is used to store the numbers. Usually there are different types of integer datatypes which is of 16-bit , 32-bit and 64-bit but most of the times we will going to use the int data type only. The 16-bit, 32-bit and 64-bit these are the other int datatypes that is used to store long integer data types. We can read the integer data type from input like below:
int firstNumber=1;
Console.WriteLine(firstNumber);
// How to take int as input
Console.WriteLine("Enter the secondNumber);
string input=Console.ReadLine();
int secondNumber= Convert.ToInt32(secondNumber); //Here we convert string to Int of 32-bit.
Console.WriteLine(secondNumber);
Floating DataType
A floating-point data type in programming is designed to represent real numbers (those with decimal points). In C#, the two primary floating-point data types are float
and double
. In C# Float data type takes 4 byte in memory and Double data type takes 8 byte in memory. Let see how we can read the float and double value from input:
float floatValue = 3.14f;
Console.WriteLine(floatValue);
double doubleValue = 3.14;
Console.WriteLine(doubleValue);
//How to take float and double as input
Console.WriteLine("Enter Float Number");
string input=Console.ReadLine();
float floatValue = float.Parse(input); //Here we convert the string to float
//If you want to convert the string to double
double doubleValue=double.Parse(input);
Char DataType
Char is a 2-Byte length data type that can contain Unicode data. Unicode is a standard for character encoding and decoding for computers. We can use various Unicode encodings formats such as UTF-8(8-bit), UTF-16(16-bit), and so on. char represents a character as a UTF-16 code unit. UTF-16 means 16-bit length which is nothing but 2-Bytes. Let see how we can read the character from user input:
char ch='B';
Console.WriteLine(ch);
//How to read Character from user input
char ch;
Console.Read(); //it is used to read single character
String DataType
In C# data types char is used to read the single character only. Therefore new datatype string is come into existence to read the whole sentence at once. A string is nothing but a series of char data types. The size of string datatype is the number of characters * char size. For example :
string str="WebTutorialStack";
//16 character then size of string will be 16*2=32 bytes
Console.ReadLine(); //It is used to read the whole string
Boolean DataType
In C#, the boolean data type is used to represent values that are either true or false. The bool
keyword is used to declare boolean variables or properties. Here’s a simple example:
using System;
class Program
{
static void Main()
{
// Declare boolean variables
bool isSunny = true;
bool isRaining = false;
// Display the boolean values
Console.WriteLine("Is it sunny "+ isSunny);
Console.WriteLine("Is it raining "+ isRaining);
// Perform conditional checks
if (isSunny)
{
Console.WriteLine("Wear sunglasses!");
}
else
{
Console.WriteLine("Bring an umbrella!");
}
}
}
Class
In C# DataTypes, a class is a user-defined reference type that serves as a blueprint for creating objects. Objects are instances of classes and can have fields (variables) and methods (functions). Classes are a fundamental concept in object-oriented programming (OOP) and are used for encapsulating data and behavior in a structured way. For example:
using System;
// Define a class named Person
class Person
{
// Fields (attributes) of the class
public string Name;
public int Age;
// Method (behavior) of the class
public void DisplayInfo()
{
Console.WriteLine("My Name is "+Name + " and I am "+Age+" years old");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the Person class
Person john = new Person();
// Set the values of the fields
john.Name = "John Doe";
john.Age = 30;
// Call the method to display information
john.DisplayInfo();
}
}
Interface
Interfaces are used to define a contract that multiple classes can adhere to, promoting a consistent API across those classes. It helps achieve abstraction and supports polymorphism by allowing objects of different classes to be treated uniformly if they implement the same interface. While interfaces don’t represent data types directly, they play a crucial role in defining the behavior (methods) that classes should have, promoting a structured and modular design in your code. For example :
using System;
// Define an interface named IDisplayable
public interface IDisplayable
{
void Display();
}
// Implement the interface in a class
public class DisplayableClass : IDisplayable
{
// Implement the method from the interface
public void Display()
{
Console.WriteLine("This is displayed by DisplayableClass");
}
}
class Program
{
static void Main()
{
// Create an instance of DisplayableClass
DisplayableClass displayableObject = new DisplayableClass();
// Call the Display method through the interface
displayableObject.Display();
}
}
Enum
In C#, an enumeration (enum) is a user-defined type that consists of a set of named integral constants. Enums provide a way to create named values for a set of related items, making the code more readable and maintainable.
using System;
// Define an enumeration named Days
public enum Days
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
class Program
{
static void Main()
{
// Declare a variable of type Days
Days today = Days.Wednesday;
// Switch statement based on the enum value
switch (today)
{
case Days.Sunday:
Console.WriteLine("It's a relaxing day!");
break;
case Days.Monday:
Console.WriteLine("Start of the workweek!");
break;
case Days.Wednesday:
Console.WriteLine("Halfway through the week!");
break;
default:
Console.WriteLine("It's just another day.");
break;
}
}
}
Conclusion
In above article we have discussed the various types of C# Datatypes which includes predefined datatypes and user defined dataTypes. I know at this point it is very difficult to understand all the user defined datatypes like classes, interfaces and enums but don’t worry we will going to discuss this user defined types in upcoming articles. Till the time you only have to understand about the predefined data types how it works and little overview about user defined datatypes. In our next article I am going to discuss about the typecasting in csharp.