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;
C# data types are divided into three categories:
There are two types of value data types in C#:
There are two types of reference data types in C#:
Symbols used in pointer:
Following are some examples of different data types in C#,
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:
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:
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:
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:
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:
The above is the detailed content of C# Data Types. For more information, please follow other related articles on the PHP Chinese website!