Dealing With C# Data Types material and item suggestions are editorially independent. We might generate income when you click links to our partners. Find Out More

C# tutorials

Information key ins C# are fundamental programs principles that specify what kind of information a variable can hold. C# is a statistically-typed language, and, as such, every variable needs to be stated with a particular information type prior to being utilized. In this programs tutorial, we will talk about the various information types that C# needs to use, consisting of the integrated information types and referral types. We will likewise cover the principle of type conversion and display some code examples revealing their use.

Dive to:

What are Information Enters C#?

As kept in mind in our intro, information types signify the kind of info a variable can hold, such as a string or numerical worth. Even more, they likewise specify the variety of worths the variable can hold and the kinds of operations that can be carried out on the variable. For example, you would not carry out computations on a text worth, nor would you capitalize a number.

C# information types are approximately categorized into 2 primary classifications: worth types and referral types

We will talk about each of these information key ins the areas listed below. However initially, let’s talk about the advantages of utilizing information key ins our C# programs.

Check Out: JetBrains ReSharper IDE Evaluation

Advantages of Utilizing Information Types

Aside from being a needed component to any application a designer constructs, information types likewise use the following advantages:

  • Improves memory effectiveness: By assigning the correct memory for variables based upon their information type, we make our memory use more effective, leading to much better efficiency
  • Type security: Utilizing information types allows developers to capture type-related mistakes at compile-time versus runtime
  • Much better code clearness: Specifying our information types assists clarify their planned usage and function, reducing the possibilities of abuse.

Now that we comprehend a few of the core advantages of utilizing C# information types, we can talk about the various types readily available to utilize.

Integrated C# Data Types

C# has a number of types of integrated types, consisting of:

  • Numerical types
  • Character types
  • Boolean types
  • Integer types
  • Structs

Numeric Types in C#

C# uses a range of numerical types. They consist of:

  • byte and sbyte: Utilized for 8-bit signed and anonymous integers.
  • brief and ushort: Utilized for 16-bit signed and anonymous integers
  • int and uint: Utilized for 32-bit signed and anonymous integers. Holds worths from -2,147,483,648 to 2,147,483,647.
  • long and ulong: Utilized for 64-bit signed and anonymous integers. Holds worths from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
  • float: Utilized for 32-bit floating-point numbers. Holds worths approximately 7 decimals.
  • double: Utilized for 64-bit double-precision floating-point numbers. Holds worths approximately 15 decimals.
  • decimal: 128-bit decimal floating-point numbers

For beginners, integer information types are utilized to represent entire numbers. There are a number of kinds of integers, consisting of byte, brief, int,, and long Each kind of integer holds a specific kind of numerical worth, displayed in our list above. To specify a standard int and appoint it a worth, we utilize the following syntax:

 int age = 20;


If we wished to produce a long and appoint it a worth, we might utilize the following code example:

 long earthAge = 400000000L;


Floating-point types, on the other hand, represent numbers with decimal points or that are fractional. Drifting point types consist of float, double, and decimal If we wished to produce a floating point kind of float, we might do so in the following way


. float myHeight= 6.5 F; 

Character and String Types in C#

For single unicode characters, C# depends on the char The syntax to state a char variable can be seen in the following code example:

 char test;


To appoint a worth to a char, just confine the worth in quote marks, as revealed listed below:

 char test='A';


If you wish to produce a variable efficient in holding more than a single unicode character, you will wish to produce a string type. string types can hold anywhere from 1 to a billion characters. They are usually utilized to hold words, sentences, or perhaps paragraphs of text-based information. Here is the syntax for producing a string in C#:

 string name =" Nicholas";

You can likewise produce a string utilizing the System.String class. The syntax is a little various– you need to utilize an uppercase String versus the lowercase variation:

 String name2="Nick";


Which you select to utilize depends on the developer– neither option is incorrect. We will talk about strings in higher information in an approaching tutorial.

Check Out: Rider IDE Evaluation

Boolean Types

The bool type is utilized to represent a boolean worth, either real or incorrect in C#. They are typically utilized when you require to carry out an action relying on whether one declaration or another is real or incorrect

For instance, let’s state we wish to determine if one number is higher than another. We can do so utilizing a bool expression. Initially, we produce 2 integers, then usage Console.WriteLine to print the outcome of our assessment to the screen. Here is some sample code showing this:

 int age1 = 20;

int age2= 50; 
. Console.Writeline( age1 > > age2);// returns False since ag1 IS NOT higher than age 2


In this example, we appoint the age1 variable a worth of 20 and our age2(* )variable the worth of 50 We then ask if age1 is higher than age2 Given that it is not, our bool(* )expression will return a incorrect worth. Integer Data Types and Drifting Point Types We quickly saw the integer and drifting point key ins our list of the various numerical types. Here, we discuss them in higher information. For beginners, integer information types are utilized to represent entire numbers. There are a number of kinds of integers, consisting of


, brief, int,, and long Enumerations and Enums In C#,


specifies called constants, which are worths that can not be altered or that are read-only. Designers utilize continuous when they plan for their information to never ever alter and stay constant. Enum worths remain in a list format and start with the very first entry beginning at worth 0 by default. The 2nd product in the list would be worth 1, etc. For instance, let’s state you have an enum that holds the days of week. You would specify it in this way: enum Days { . Sunday,// index 0 . Monday,// index 1 . Tuesday,// index 2 . Wednesday, index 3 . Thursday, index 4 . Friday, index 5 . Saturday index 6 .
} .(* )Once the worths are appointed to an Enum, we can then appoint the specific worths to a variable. To do so, we would utilize code comparable to the following:
Days today = Days.Monday; . Console.WriteLine ( today); .

 This would print" Monday "to the screen.


 Structs can be considered information structures that groups information. They resemble classes however are worth types that can hold builders, constants, fields, approaches, operators, indexers, and more. They work if you require a structure to hold information worths that do not depend on inheritance, such as key-value sets. For instance, let's state we wished to hold the place of a structure based upon its longitude and latitude. We might do so in a Struct in the following way: 

struct Place .
{ .
public int x; .
public int y; .} .

Recommendation Types in C#

Referrals types keep a “referral” to real information versus the real worth itself. The referral shops the address where a worth is kept, which can be considered a


to a memory place. Referrals types have the ability to keep recommendations to classes, strings, selections, and delegates

Recommendation types keep recommendations to the real information on the load. They consist of classes, strings, selections, and delegates. Type Conversion in C# There are 2 types of type conversion in C#:


specific Implicit type conversion lets you immediately transform from suitable information types, such as transforming an int worth to a long Specific type conversion is required when you wish to transform incompatible information types. For instance, let’s state we wish to pass a smaller sized worth to a worth type that holds a bigger worth. In our example listed below, we will utilize implicit type conversion (or casting) to pass an

int to a double: int myWeight = 200; . double myDoubleWeight =myWeight ;
.(* )Given that both information types work, the conversion from int to


is automated and we do not require to compose any additional code to require the conversion. Nevertheless, if we wished to carry out the reverse operation and appoint a double to an int

, we would require to manual cast the worth, as an int does decline decimal worths. When this conversion occurs, the numbers after the decimal point are gotten rid of and the drifting point worth ends up being an integer worth or entire number rather. Here is how that searches in code: double myDoubleWeight = 200.58; . int myWeight =( int) myDoubleWeight; . Console.WriteLine( myDoubleWeight); . Console.WriteLine( myWeight); . Notification how we position ( int) prior to


This code is where we require the conversion from a double to an int If we run this program, we get the list below output: 200.58 .
200 .
Last Ideas on C# Data Types Comprehending the various information types and how they work is important for producing effective and error-free C# applications. This programs tutorial covered the fundamental integrated information types C# needs to use, in addition to its referral types. We likewise went over both implicit and specific type conversions and type casting. Check Out:

 Finest Tools for Remote Developers

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: