C# and it’s data types

C# and it’s data types

C# (pronounced ‘C sharp’) is a modern, multi-paradigm, object-oriented and type-safe programming language. Since the first introduction in 2000 is it being widely used for developing a variety of applications, including desktop software, web applications, games, and mobile apps. These applications are robust and secure and run in .NET.

The TIOBE index 2023 ranks C# as the 5th most popular language as of today 31st May, 2023. And according to the Stack Overflow survey of 2022, it is the 7th most commonly used language by professional developers.

C# syntax is close to Java and Scala or Kotlin. So one will find it quite easy to work with coming from those backgrounds.?

With that being said, let’s talk about variables and types in C#.

So, what are variables?

The definition of variables stays the same throughout all programming languages. In programming, a variable is like a container that holds a value or a piece of data. It allows you to store and manipulate data during the execution of a program. You can think of variables as named storage locations in the computer's memory. For example, you can have a variable called name to store a person's name or a variable called age to store their age.

Data Types inC#

There are six category of data types in C#. They are as followed:

  1. Numeric Data Types
  2. Textual Data Types
  3. Boolean Data Type
  4. Date and Time Data Types
  5. Collection Data Types
  6. Object Data Type

Numeric Data Types

Numeric data types in C# are used to store numeric values. They can represent integers, floating-point numbers, and decimal numbers. Here's an elaboration of the numeric data types in C#:

Integer Data Types:?

  • int: It is a 32-bit signed integer type, which can hold whole numbers in the range of approximately -2 billion to 2 billion.
  • long: It is a 64-bit signed integer type, capable of storing larger whole numbers in the range of approximately -9 quintillion to 9 quintillion.
  • byte: It is an 8-bit unsigned integer type, used to store small positive integers ranging from 0 to 255.
  • short: It is a 16-bit signed integer type, used to store relatively small whole numbers in the range of approximately -32,768 to 32,767.

Floating-Point Data Types:

  • float: It is a 32-bit single-precision floating-point type, capable of storing fractional and decimal numbers with lower precision.
  • double: It is a 64-bit double-precision floating-point type, offering higher precision compared to float. It is the default choice for representing floating-point numbers in C#.

Decimal Data Type:

  • decimal: It is a 128-bit decimal data type, designed for precise decimal calculations. It is commonly used in financial and monetary calculations where accuracy is critical.

Textual Data Types

Textual data types in C# are used to store and manipulate sequences of characters or individual characters. Detail on textual data types in C#:

  1. string: The string data type is used to represent a sequence of characters. It is one of the most commonly used data types in C#. Strings are immutable, meaning that once created, their values cannot be changed. You can perform various operations on strings, such as concatenation, searching, splitting, and replacing.
  2. char: The char data type is used to represent a single character. Characters are enclosed in single quotes (''). They can represent alphanumeric characters, symbols, or even escape sequences.

Boolean Data Type

The Boolean data type in C# is used to represent logical values, such as true or false. It is a fundamental data type used for conditions, decision-making, and controlling the flow of programs. The bool data type is used to represent Boolean values. It can only have two possible values: true or false. These values are keywords in C# and are not interchangeable with other numeric values like 0 or 1. Boolean variables are often used in conditional statements and logical operations.

Date and Time Data Types

Date and time data types in C# are used to represent and manipulate dates, times, and duration. They are essential for working with time-related information and performing various operations involving dates and time.

  1. DateTime: The DateTime data type represents a specific point in time. It includes both date and time components, allowing you to store and manipulate values for years, months, days, hours, minutes, seconds, and milliseconds. The DateTime type provides various properties and methods to perform common operations, such as adding or subtracting time intervals, comparing dates, formatting dates, and extracting specific components.
  2. TimeSpan: The TimeSpan data type represents a duration or a time interval. It allows you to measure the difference between two DateTime values or specify a specific time span using hours, minutes, seconds, and milliseconds. The TimeSpan type provides properties and methods to perform arithmetic operations, such as adding or subtracting time spans, comparing durations, and formatting time intervals.

C# also provides additional types and structures related to date and time, including DateTimeOffset (to represent dates and times with time zone information), TimeSpan.Ticks (to access the ticks component of a TimeSpan), and various formatting and parsing options to convert date and time values to and from strings.

Collection Data Types

Collection data types in C# are used to store and manipulate collections or groups of related data elements. They provide different structures and functionalities to organize, access, and modify data efficiently.

  1. Array: An array is a fixed-size collection that can store multiple elements of the same type. It provides random access to elements using an index and allows you to iterate over the elements. Arrays have a fixed length, which is defined at the time of creation.
  2. List: A list is a dynamic-size collection that can grow or shrink as needed. It provides similar functionality to an array but with additional methods to add, remove, or modify elements dynamically. Lists allow you to work with collections of objects and provide flexibility in managing data.
  3. Dictionary: A dictionary is a collection that stores key-value pairs. Each element in the dictionary is identified by a unique key, which is used to access its associated value. It provides fast lookup based on the key and allows efficient retrieval and modification of values.
  4. HashSet: A hash set is a collection that stores a unique set of elements. It ensures that each element in the set is unique, discarding duplicates automatically. HashSet provides efficient operations for adding, removing, and checking for the presence of elements.

Object Data Type

The object data type in C# is the base type for all other types. It is a reference type that can hold values of any other type, including value types and reference types. The object data type is primarily used when you need to work with heterogeneous collections or when the specific type of an object is not known at compile time.

  1. object: The object data type represents an instance of any type. It allows you to store values of different types in a single variable. Since all types in C# implicitly derive from object, you can assign any value to an object variable.

We’ll see more on objects in later discussions.?

要查看或添加评论,请登录

Khairul Bashar的更多文章

  • Setting up Tauri with Nuxt 3 using bun

    Setting up Tauri with Nuxt 3 using bun

    Setting up Tauri with Nuxt 3 using bun First, what Tauri actually is? Tauri is a framework for building lightweight…

    5 条评论
  • Prompt Engineering for Software Engineers.

    Prompt Engineering for Software Engineers.

    Prompting is a way to communicate with an AI system to get it to perform a specific task. It involves providing the AI…

社区洞察

其他会员也浏览了