C# Beginner to advanced - Lesson 33 - Implicit & Explicit Conversion & Diff between Parse & TryParse

مجله خبری، آموزشی پرشین تک
منتشر شده در 11 خرداد 1397

Implicit and Explicit Conversion and Difference between Parse and TryParse


Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms:


Implicit Conversion : Implicit Conversion is done by the compiler :

When there is no loss of information if the conversion is done.

If there is no possibility of throwing exceptions during the conversion.

Example : Converting an int to float will not loose any data and no exception will be thrown, hence an implicit conversion can be done


Explicit Conversion : Explicit Conversion is done by the user :

For Explicit conversion we can use cast operator or the convert class in C#

Example : Converting an float in to int , we loose the fractional part and also a possibility of overflow exception.


If we want to convert number in string format to other numerical formats like int ,decimal, float then we have two options:

Parse()

TryParse().


Parse() Method throws an exception if it cannot parse the value.

TryParse() Method returns a bool indicating whether it succeeded or failed


Use parse() if you are sure the value will be valid, otherwise use TryParse()


ankpro

ankpro training

Asp.net MVC

C#

C sharp

Bangalore

Rajajinagar

Selenium

Coded UI

Mobile automation testing

Mobile testing

JQuery

JavaScript

.Net

Components of the .Net framework

Hello World

Literal

Keywords

Variable

Data types

Operators

Branching

Loops

Arrays

ArrayList

Strings

String Builder

Structures

Enums

Functions

Classes

Inheritance

Polymorphism

Properties

Indexers

Events

Nested Classes

Delegates

Anonymous methods

Labda expressions

Abstract classes

Exception Handling

Linq

Interfaces

Extension methods

Anonymous types

Generics

Collections

Garbage Collection

Reflection

Attributes

Input and output statements

Type casting

Boxing and Unboxing

Var vs Dynamic vs Object

دیدگاه کاربران