.NET HELP

C# Types (How It Works For Developers)

Published June 20, 2023
Share:

In any programming language, understanding data types is crucial, and C# is no different. Whether it's the int type or the floating point type, each type holds a particular kind of information, also known as actual data.

The Basics of C#

C# is a strongly typed language. But what does strongly-typed language mean, you ask? It means that every variable and object must have a declared data type. And it's the data type that determines what kind of data you can store in that variable.

C# has two major data types: value types and reference types. These are also the user-defined types you might hear about.

Value Types in C#

In C#, value types are simple. When you declare a value type variable, the actual data stored is the value that you assign to the variable. If you change the value of one variable, it doesn't affect any other variable.

The most common value types in C# include

  • Integers (represented by the int type)
  • Floating-point numbers (represented by the "float and double types")
  • Boolean, which is either true or false

Let's talk about int type first. A(n) int type in C# is a 32-bit data type that can store whole numbers from -2,147,483,648 to 2,147,483,647. Here's an example

int myInteger = 15;
int myInteger = 15;
Dim myInteger As Integer = 15
VB   C#

Now, onto floating point type. In C#, there are two floating point types: float and double.

The float type is a 32-bit floating-point number with precision up to 7 digits. It's great for scientific calculations. A float type variable can be declared like this:

float myFloat = 12.34F;
float myFloat = 12.34F;
Dim myFloat As Single = 12.34F
VB   C#

The double type is a 64-bit floating point type with precision up to 15-16 digits. It's more accurate than the float type and is often used for financial and monetary calculations. Here's an example of a double type variable:

double myDouble = 12.3456789;
double myDouble = 12.3456789;
Dim myDouble As Double = 12.3456789
VB   C#

Predefined Data Types in C#

Alright, moving on. C# provides several "predefined data types" that you can use in your applications. These are the fundamental building blocks that you can use to define your own "user-defined types."

Here are a few examples:

  • byte: This is an 8-bit unsigned integer that ranges from 0 to 255.
  • char: It's a 16-bit Unicode character.
  • decimal: Ideal for "financial and monetary calculations" because of its high precision.
  • bool: Stores either 'true' or 'false', perfect for logical operations.

Each of these "predefined data types" has its own purpose and its own "default value," so it's important to choose the right one for your needs!

Default Values in C#

In C#, each data type comes with a default value. For value type variables, if you don't initialize them, they automatically take their default value.

For instance, the default value of an int type is 0, and the default value of a floating point value type (either float or double) is 0.0.

The default value for reference types is null, indicating that it's not referencing any object.

Type Conversion in C#

Sometimes, you may need to convert one data type to another. This is known as type conversion, and C# supports two kinds implicit conversion and explicit conversion.

Implicit conversion happens automatically when you assign a value of one type to a variable of a compatible type that can hold larger values. For instance, you can assign a(n) int type to a double type without losing information.

int myInt = 10;
double myDouble = myInt;                // Implicit conversion
int myInt = 10;
double myDouble = myInt;                // Implicit conversion
Dim myInt As Integer = 10
Dim myDouble As Double = myInt ' Implicit conversion
VB   C#

Explicit conversion, also known as casting, is required when you're trying to convert between incompatible types, or from larger types to smaller ones. It's done by placing the target type in parentheses in front of the value to be converted.

double myDouble = 10.5; 
int myInt = (int)myDouble;              // Explicit conversion, decimal part is lost
double myDouble = 10.5; 
int myInt = (int)myDouble;              // Explicit conversion, decimal part is lost
Imports System

Dim myDouble As Double = 10.5
Dim myInt As Integer = CInt(Math.Truncate(myDouble)) ' Explicit conversion, decimal part is lost
VB   C#

Reference Types in C#

The reference types in C# work a bit differently from value types. Instead of storing the actual data, a reference type variable stores the address where the value is being stored. In other words, it 'refers' to another location in memory.

So, if you change the reference type object, it affects the other variable as well. This is because the reference type variable automatically reflects changes made to the actual data stored at the memory address it points to.

Now, that might sound a little complex, but let's break it down with an example. Let's say we have a class called Person:

class Person
{
    public string Name { get; set; }
}
class Person
{
    public string Name { get; set; }
}
Friend Class Person
	Public Property Name() As String
End Class
VB   C#

And then we create two "reference type" variables of this class:

Person person1 = new Person { Name = "Alice" };
Person person2 = person1;
Person person1 = new Person { Name = "Alice" };
Person person2 = person1;
Dim person1 As New Person With {.Name = "Alice"}
Dim person2 As Person = person1
VB   C#

Here, both person1 and person2 are pointing to the same memory location. If we change person1, person2 will reflect that change:

person1.Name = "Bob"; 
Console.WriteLine(person2.Name);                // Outputs "Bob"
person1.Name = "Bob"; 
Console.WriteLine(person2.Name);                // Outputs "Bob"
person1.Name = "Bob"
Console.WriteLine(person2.Name) ' Outputs "Bob"
VB   C#

Arrays in C#

An array is a reference type that holds multiple values of the same data type. It's like a container where you can store a collection of values, all of which are of the same type.

To declare an array, you first specify the data type of its elements, followed by square brackets. Then you use the new keyword to create the array and specify its length.

int [] myIntArray = new int [5];                 // Array of int type, can hold 5 values
int [] myIntArray = new int [5];                 // Array of int type, can hold 5 values
Dim myIntArray(4) As Integer ' Array of int type, can hold 5 values
VB   C#

To access the elements of the array, you use an index, starting at 0 for the first element.

myIntArray [0] = 10;                           // Sets the first element of the array to 10
myIntArray [0] = 10;                           // Sets the first element of the array to 10
myIntArray (0) = 10 ' Sets the first element of the array to 10
VB   C#

Remember, arrays in C# are "reference types", so changes made in one variable will affect any "other variable" referencing the same array.

Generating PDFs with IronPDF in C#

IronPDF is a powerful library for C# that allows developers to create, edit, and extract content from PDFs. This can be a lifesaver for tasks like generating reports or creating dynamic invoices.

Getting Started with IronPDF

First, you'll need to install IronPDF. You can do this through NuGet, a popular package manager for .NET. Run the following command in your package manager console:

Install-Package IronPdf

Creating a PDF from HTML

IronPDF is able to create PDFs from HTML. It's pretty straightforward:

using IronPdf;
var Renderer = new ChromePdfRenderer();
Renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>").SaveAs("HelloWorld.pdf");
using IronPdf;
var Renderer = new ChromePdfRenderer();
Renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>").SaveAs("HelloWorld.pdf");
Imports IronPdf
Private Renderer = New ChromePdfRenderer()
Renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>").SaveAs("HelloWorld.pdf")
VB   C#

The above code will generate a PDF with the "Hello, World!" heading. Notice that the HTML code is simply a string. In C#, a string is a reference type, and it's one of the most used C# types.

C# Types (How It Works For Developers) Figure 1 - Hello World text

Conclusion

Congratulations! You've taken a deep dive into the world of C# Types, understanding value types, reference types, predefined data types, and how they shape the way you write code. You've also seen the power of using libraries like IronPDF, and how your grasp of C#

IronPDF offers free trial and license of IronPDF starts from $liteLicensing.

< PREVIOUS
C# Numeric Types (How It Works For Developers)
NEXT >
C# Find (How It Works For Developers)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >