Sitemap
Ömer Faruk Didin

Ömer Faruk Didin

Hi, This is Ömer, I am a 3rd year student of Computer Engineering. I am interested in software and IT. I express my achievements.

C# Type Casting

2 min readSep 12, 2023

--

#8 Greetings, last time we wrote an article on data types, today I will write a short article on “type casting”.

Type Casting

Type casting is when you assign a value of one data type to another type.

Get Ömer Faruk Didin’s stories in your inbox

Join Medium for free to get updates from this writer.

In C#, there are two types of casting:

  • Implicit Casting (automatically) — converting a smaller type to a larger type size
    char -> int -> long -> float -> double
  • Explicit Casting (manually) — converting a larger type to a smaller size type
    double -> float -> long -> int -> char
Press enter or click to view image in full size
Implicit Type Conversion

Implicit Casting

Implicit casting is done automatically when passing a smaller size type to a larger size type:

Press enter or click to view image in full size
Implicit Casting

Explicit Casting

Explicit casting must be done manually by placing the type in parentheses in front of the value:

Press enter or click to view image in full size
Explicit Casting

Type Conversion Methods

It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):

Press enter or click to view image in full size
Type Conversion Methods

Why Conversion?

Many times, there’s no need for type conversion. But sometimes you have to. Take a look at the next chapter, when working with user input, to see an example of this.

That’s it for my article on “type casting” next article will be about “user input. Stay with code.

--

--

Ömer Faruk Didin
Ömer Faruk Didin

Hi, This is Ömer, I am a 3rd year student of Computer Engineering. I am interested in software and IT. I express my achievements.