C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, there are two main categories of data types: value types and reference types. In this tutorial, we'll cover the most commonly used data types in each category.
Value types store the data directly, and when you pass a value type variable to a method or assign it to another variable, a copy of the value is made. Value types are derived from the System.ValueType
class.
a. Basic Numeric Types:
C# has several built-in numeric types for both integers and floating-point numbers.
Integers:
byte
: 8-bit unsigned integer (0 to 255)sbyte
: 8-bit signed integer (-128 to 127)short
: 16-bit signed integer (-32,768 to 32,767)ushort
: 16-bit unsigned integer (0 to 65,535)int
: 32-bit signed integer (-2,147,483,648 to 2,147,483,647)uint
: 32-bit unsigned integer (0 to 4,294,967,295)long
: 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)ulong
: 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)Floating-Point Numbers:
float
: 32-bit single-precision floating-point numberdouble
: 64-bit double-precision floating-point numberOther Numeric Types:
decimal
: 128-bit high-precision decimal number, suitable for financial calculationsb. Character and Boolean Types:
char
: Represents a single 16-bit Unicode characterbool
: Represents a boolean value (true or false)Reference types store a reference to the memory location where the data is stored. When you pass a reference type variable to a method or assign it to another variable, the reference is copied, not the actual data.
a. String Type:
string
: Represents a sequence of characters. Strings are immutable in C#.b. Array Types:
c. Object Type:
object
: The base class for all types in C#. All other types, including value types, inherit from the object
class.Here's a simple example demonstrating the use of various data types in C#:
using System; namespace DataTypesTutorial { class Program { static void Main(string[] args) { // Numeric Types int myInt = 42; double myDouble = 3.14; // Character and Boolean Types char myChar = 'A'; bool myBool = true; // String Type string myString = "Hello, World!"; // Array Types int[] myIntArray = new int[] { 1, 2, 3, 4, 5 }; string[,] my2DStringArray = new string[,] { { "A1", "B1" }, { "A2", "B2" } }; // Object Type object myObject = new object(); }
Primitive data types in C#:
int
, float
, char
, and bool
.int integerNumber = 42; float floatingPointNumber = 3.14f; char character = 'A'; bool isTrue = true;
Numeric data types in C#:
int
, long
), floating-point numbers (float
, double
), and more.int integerNumber = 42; long longNumber = 1234567890123456789L; float floatingPointNumber = 3.14f; double doubleNumber = 3.1415926535;
String data type in C#:
string
data type is used to represent sequences of characters.string text = "Hello, C#!";
C# char data type:
char
data type represents a single Unicode character.char singleCharacter = 'A';
Boolean data type in C#:
bool
data type represents Boolean values (true
or false
).bool isTrue = true; bool isFalse = false;
Reference types in C#:
class MyClass { // Class members } MyClass myObject = new MyClass();
Data type conversion in C#:
int intValue = 42; double doubleValue = intValue; // Implicit conversion
User-defined data types in C#:
public class Person { public string Name { get; set; } public int Age { get; set; } } Person myPerson = new Person { Name = "John", Age = 30 };