# what is the difference between type "coercion" and "casting" ?

**URL:** <https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465>\
**Category:** PowerShell Help\
**Created:** [November 23, 2019, 11:25am UTC](https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465 "2019-11-23T11:25:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [November 23, 2019, 11:25am UTC](https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465/1 "2019-11-23T11:25:35Z")

</div>

Is type coercion and type casting the same thing? It sounds more violent, lol. Wny do powershell coders call it coercion?

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [November 23, 2019, 1:39pm UTC](https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465/2 "2019-11-23T13:39:58Z")

</div>

If I understand correctly, Casting is when you explicitly change data from one to type to another.

Coercion is when you leave the compiler to decide which type to use when different types are encountered. eg: with Tsql when there are varchar and nvarchar values, the nvarchar has a higher precedence and so all varchar values are converted to nvarchar to carryout a comparison. That’s coercion.

---

<div class="post-metadata">

**Author:** ![swsamwa](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/swsamwa/32/62_2.png) [@swsamwa](https://forums.powershell.org/u/swsamwa)\
**Post date:** [November 23, 2019, 5:45pm UTC](https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465/3 "2019-11-23T17:45:43Z")

</div>

As Iain said, casting is explicit type conversion. Coercion is implicit type conversion.

Here is an example of coercion in action. PowerShell evaluate the expression left-to-right. In the first case, the left-most value is of type [int] so the string is coerced to that type. In the second case, the the left-most value is of type [string].

`PS> 1 + “1”`  
`2`  
`PS> “1” + 1`  
`11`

Here is an example of type casting.

`PS> [string]1 + “1”`  
`11`

`PS> [int]“1” + 1`  
`2`

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [November 25, 2019, 11:39am UTC](https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465/4 "2019-11-25T11:39:17Z")

</div>

Cool. Thanks.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/what-is-the-difference-between-type-coercion-and-casting/13465/5 "2024-05-16T20:31:00Z")

</div>

Closed on May 16, 2024, 8:31pm UTC


