Zum Fußzeileninhalt springen
.NET HILFE

C# Numerische Typen (Wie es für Entwickler funktioniert)

In C#, numeric types are a set of data types used to store numbers and can be divided into two main categories: integer types and floating-point types. Each category has its own unique characteristics and use cases.

Numeric types form the basis of numerous operations in C#, from financial and monetary calculations to complex algorithms. If you’re looking to master C#, you’ll need to master these value types.

Integer Data Types

Integer data types are utilized to store whole numbers and can be either signed (capable of holding both positive and negative numbers) or unsigned (capable of holding only positive numbers). Here's a breakdown of the integer types in C#:

byte

The byte is the smallest integer type. It's an unsigned type, with a default value of 0, capable of storing values from 0 to 255.

sbyte

The sbyte is a signed counterpart of the byte. It can store values from -128 to 127, with a default value of 0.

short

A short is a 16-bit signed integer. It has a larger range than the byte and sbyte, from -32,768 to 32,767, with a default value of 0.

ushort

ushort is the unsigned counterpart of the short. It can hold values from 0 to 65,535. Its default value is also 0.

int

An int type is a 32-bit signed integer type, with a range from -2,147,483,648 to 2,147,483,647. The default value of an integer variable of type int is 0.

uint

The uint, short for "unsigned integer," can hold values from 0 to 4,294,967,295. Its default value is 0.

long

Long is a 64-bit signed integer type, capable of storing values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The default value of a long is 0L.

ulong

The ulong, or unsigned long, can store values from 0 to 18,446,744,073,709,551,615. Its default value is 0UL.

Floating-Point Number Types

Floating-point types are used to store numbers with a decimal point. A floating-point number provides a much wider range of values than integer types, but with a trade-off in precision. Here are the floating-point types in C#:

float

A float is a 32-bit floating-point type. It can represent values from approximately 1.5 x 10^-45 to 3.4 x 10^38, with a precision of 7 digits. The default value of a floating-point variable of type float is 0.0f.

double

The double type is a 64-bit floating-point type. It can represent values from approximately 5.0 x 10^-324 to 1.7 x 10^308, with a precision of 15-16 digits. The default value of a double is 0.0d.

decimal

The decimal type is a 128-bit data type. It's often used when dealing with financial calculations that require high precision. It can represent values from approximately 1.0 x 10^-28 to 7.9 x 10^28, with a precision of 28-29 digits. The default value of a decimal type is 0.0m.

Understanding Native Sized Integer Types

Native sized integer types are special types in C# that have a size that varies depending on the platform the code is running on. These are designed to provide the most efficient use of memory for storing integer values.

IntPtr

IntPtr is a signed integer type that has the same width as a pointer. This means it's 32-bit on 32-bit platforms and 64-bit on 64-bit platforms. This is useful when dealing with pointer or memory-related tasks, and the default value of an IntPtr is 0.

UIntPtr

UIntPtr, the unsigned counterpart of IntPtr, also has the same size as a pointer. It provides the same value range as IntPtr on the same platform, but only for non-negative values. The default value of an UIntPtr is also 0.

Converting Between Integer and Floating-Point Types

Depending on the use case, it’s important to use the correct data type in your code. In many cases, you may have a value of one type and need to use it as another type.

Implicit conversions happen automatically when the value to be converted will fit into the new data type without losing any information. For example, you can implicitly convert an int to a long, as a long can store the same value as an int.

Explicit conversions, also known as casting, are required when there's a risk of data loss. For example, converting a long to an int may result in data loss if the long's value is larger than what an int can hold.

To explicitly cast a value from one type to another, you can use the casting operator ():

long myLong = 5000L;
int myInt = (int)myLong; // This is an explicit cast from long to int.
                         // Be cautious if myLong > 2,147,483,647 as it may cause data loss.
long myLong = 5000L;
int myInt = (int)myLong; // This is an explicit cast from long to int.
                         // Be cautious if myLong > 2,147,483,647 as it may cause data loss.
Dim myLong As Long = 5000L
Dim myInt As Integer = CInt(myLong) ' This is an explicit cast from long to int.
						 ' Be cautious if myLong > 2,147,483,647 as it may cause data loss.
$vbLabelText   $csharpLabel

Be careful when casting, as it can lead to unexpected results if the value is outside the range of the target type.

Applying Numeric Types Using IronPDF

IronPDF is a lightweight .NET PDF library designed specifically with web developers in mind. It makes reading, writing, and manipulating PDF files a breeze, able to convert all kinds of file types into PDF content, and you can use it in your .NET projects for both desktop and web. The best part - it’s free to try out in a development environment.

Let’s take a look at implementing types in C# using IronPDF.

Positioning and Sizing Elements

In this example, we’ll tweak the position and size of elements on a page. In IronPDF, coordinates are defined using a floating-point type.

using IronPdf;

var htmlToPdf = new ChromePdfRenderer();
htmlToPdf.RenderingOptions.MarginTop = 50;    // Set top margin in points.
htmlToPdf.RenderingOptions.MarginBottom = 50; // Set bottom margin in points.

var document = htmlToPdf.RenderHtmlAsPdf("<h1>Numeric Types in C# with IronPDF</h1>");
document.SaveAs("C:\\numericTypes.pdf");       // Save the generated PDF to the specified path.
using IronPdf;

var htmlToPdf = new ChromePdfRenderer();
htmlToPdf.RenderingOptions.MarginTop = 50;    // Set top margin in points.
htmlToPdf.RenderingOptions.MarginBottom = 50; // Set bottom margin in points.

var document = htmlToPdf.RenderHtmlAsPdf("<h1>Numeric Types in C# with IronPDF</h1>");
document.SaveAs("C:\\numericTypes.pdf");       // Save the generated PDF to the specified path.
Imports IronPdf

Private htmlToPdf = New ChromePdfRenderer()
htmlToPdf.RenderingOptions.MarginTop = 50 ' Set top margin in points.
htmlToPdf.RenderingOptions.MarginBottom = 50 ' Set bottom margin in points.

Dim document = htmlToPdf.RenderHtmlAsPdf("<h1>Numeric Types in C# with IronPDF</h1>")
document.SaveAs("C:\numericTypes.pdf") ' Save the generated PDF to the specified path.
$vbLabelText   $csharpLabel

Output PDF File

Understanding C# Numeric Types, Image #1

Conclusion

From basic calculations to customizing the layout of a generated PDF, number types are an important part of effective and efficient C# programming. They serve as the building blocks for data manipulation, algorithm design, and the creation of high-quality PDF documents.

Want to get your hands on IronPDF? You can start with our 30-day free trial. It’s also completely free to use for development purposes so you can really get to see what it’s made of. And if you like what you see, IronPDF starts as low as liteLicense. For even bigger savings, check out the Iron Suite where you can get all nine Iron Software tools for the price of two. Happy coding!

Understanding C# Numeric Types, Image #2

Häufig gestellte Fragen

Wie kann ich HTML-Inhalte mit C# in ein PDF konvertieren?

Sie können HTML-Inhalte in PDF in C# umwandeln, indem Sie die RenderHtmlAsPdf-Methode von IronPDF nutzen, die es Ihnen ermöglicht, HTML-Strings direkt in PDF-Dokumente zu transformieren. Zusätzlich können Sie RenderHtmlFileAsPdf verwenden, um HTML-Dateien zu konvertieren.

Was sind die Hauptunterschiede zwischen Ganzzahl- und Fließkommatypen in C#?

In C# werden Ganzzahltypen für ganze Zahlen verwendet und umfassen Typen wie byte, int und long. Fließkommatypen, wie float und double, verarbeiten Zahlen mit Dezimalpunkten und bieten einen größeren Bereich, aber geringere Genauigkeit.

Warum sollte ich in C# einen Dezimaltyp einem Double vorziehen?

Der Dezimaltyp bietet höhere Genauigkeit und ist ideal für finanzielle Berechnungen, bei denen eine genaue Darstellung der Dezimalstellen entscheidend ist. Er kann Werte mit einer Genauigkeit von 28-29 Stellen darstellen, verglichen mit den 15-16 Stellen eines Doubles.

Wie profitieren C#-Entwickler von nativen Ganzzahltypen?

Native Ganzzahltypen wie IntPtr und UIntPtr passen ihre Größe basierend auf der Plattform an, was Speicher effizient für Aufgaben macht, wie das Handling von Speicheradressen und Zeigern in plattform-spezifischen Szenarien.

Welche Rolle spielen numerische Typen bei der PDF-Erzeugung mit .NET?

Numerische Typen in der .NET PDF-Erzeugung sind entscheidend für die Definition des Layouts von PDF-Elementen. Fließkommazahlen beispielsweise können die genaue Größe und Position von Text und Bildern auf einer PDF-Seite mit IronPDF spezifizieren.

Was sind implizite und explizite Konvertierungen in C#?

Implizite Konvertierungen in C# treten auf, wenn der Zieltyp den Quellwert speichern kann, ohne dass Datenverlust auftritt. Explizite Konvertierungen, oder Castings, erfordern manuelles Eingreifen, oft mit einem Cast-Operator, aufgrund potenziellen Datenverlusts.

Wie kann ich eine .NET PDF-Bibliothek für mein Projekt evaluieren?

Sie können eine .NET PDF-Bibliothek wie IronPDF bewerten, indem Sie von deren 30-tägiger kostenloser Testversion profitieren. Dies ermöglicht es Ihnen, die Funktionen während der Entwicklung zu erkunden, bevor Sie sich für den Kauf einer Lizenz entscheiden.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen