Types of conversions in C #

1

What is the difference between these types of conversions?

    string nombre = "1234";
    int i = (int)nombre;

    string nombre = "1234";
    int i = Convert.ToInt32(nombre);

    string nombre = "1234";
    int i = Int32.Parse(nombre);

Is there any type of rule established to use one or the other? Sometimes I find that if I try to make a conversion with any of them it does not leave me and instead trying with any of the others if ... It has never been clear to me when one has to use one or the other. The information I get from inquiring is quite confusing.

    
asked by Edulon 26.08.2017 в 02:42
source

3 answers

2

First of all, these are not comparisons but conversions (casting) of data.

  

Is there any type of rule established to use one or the other?

Yes. Here I explain them to you:

string nombre = "1234";
int i = (int)nombre;

This is called Conversion Explicita (explicit casting ). This type of conversion is used when you specify explicitly to what type of data you want to convert an object. In your example, you are clearly expressing that you want to convert an object System.String to System.Int32 .

To be able to make an explicit conversion, 2 conditions have to be fulfilled: that there is some kind of relationship between the object and the type of data to be converted, either by inheritance or by the implementation of a common interface or by loading the < a href="https://www.google.com.do/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjcq-yN2_PVAhXEv1QKHbJtB7UQFggpMAA&url= https% 3A% 2F% 2Fmsdn.microsoft.com% 2Fes-en% 2Flibrary% 2Fms228498 (v% 3Dvs.90) .aspx & usg = AFQjCNEMtSzoecjZ_LXsW7p2EHhi3TmYEA "> explicit conversion operator or predict .

The structure System.Int32(int) is an example of the implicit operator overload with the data type char since you can convert a char to int without having to specify the type of data to convert:

char n = 'n'
int letra = n; // valido

While to convert from int to char you have to specify it explicitly:

int nNumber= 110;
char n = (char)nNumber;

In your example, the operation is not valid because none of the 2 conditions are met.

string nombre = "1234";
int i = Convert.ToInt32(nombre);

Convert.ToInt32 converts an object int , long , decimal , bool , char , float , byte , DateTime a System.Int32 . If the data type can not be converted to int an exception FormatException is thrown.

string nombre = "1234";
int i = Int32.Parse(nombre);

Convert a System.String to Int32 . If the data type can not be converted to int an exception FormatException is thrown.

The only difference between these last 2 is the variety of overloads that Convert.Int32 offers.

    
answered by 26.08.2017 / 03:24
source
2

In the case of your example, the short answer is: they are the same, but depending on the argument, it will be their behavior.

These are the definitions and differences that I found. It would be interesting if you published some example where you can not compare:

Unboxing conversion

  

It is an explicit conversion of the type object to a type of value or a   type of interface to a type of value that implements the interface. Conversion unboxing .

string nombre = "1234";
    int i = (int)nombre;
  • It is expensive in performance
  • Applying to a null argument or an incompatible value will result in InvalidCastException.

Casting (Parsing) Int32.Parse

  

Convert the representation in the form of a string to a number in the 32-bit integer with an equivalent sign. Int32.Parse (String) method

  • Returns ArgumentNullException when the argument is null
  • It is recommended instead to use Int32.TryParse to avoid exceptions, returns a Boolean that indicates whether the conversion was made correctly or not.

Conversion Convert.ToInt32 ()

  

Converts a specified value to a signed 32-bit integer. Convert.ToInt32 Method ()

  • Returns zero when the argument is null
  • Convert.ToInt32 (string) internally implements int.Parse ()
  • Convert.ToInt32 (object) implements ((IConvertible) value) .ToInt32

Other Sources:

Whats is the difference between int parse and convert toint32 Is casting the same as converting What is the difference between boxing unboxing and type casting

    
answered by 26.08.2017 в 03:39
1

The difference is the possibility of data loss, for accuracy.

If a conversion can not be performed without risk of losing information, the compiler requires an explicit conversion, which is called conversion. A type conversion is a way to explicitly inform the compiler that the conversion is intended and is aware that data loss may occur.

Implicit conversions do not require any special syntax because the conversion has type security and no data will be lost.

Explicit conversions require a conversion operator. The conversion is necessary if it is possible that information is lost in the conversion, or when it is possible that the conversion is not correct for other reasons.

    
answered by 26.08.2017 в 03:01