Skip to main content

C# - Data Type Conversion


Data Type Conversion 



In our previous tutorial we have seen various types of data and how to use them. In this session we are going to look into how to convert these data type from one to another. 

Implicit Conversions


In Implicit conversion can be made when the value to be stored can fit into the variable without being truncated or rounded off.

We can take example of int and long, float and double.

we can declare a variable int and assign it to a long type which is an implicit conversion as the scope of long is bigger than that of int so there is no worry of loosing data. 

Explicit Conversions


However, if a conversion cannot be made without a risk of losing information, the compiler requires that you perform an explicit conversion, which is called a cast. A cast is a way of explicitly informing the compiler that you intend to make the conversion and that you are aware that data loss might occur. To perform a cast, specify the type that you are casting to in parentheses in front of the value or variable to be converted. 

We can take the below example to convert data types from int to long and float to double at the same time we can see the example of long to int and double to float. It shows how both type of conversions work.





Conversion methods in C#


The following table lists some of the methods from the Convert class that you can use.



How to convert a String to a Number





Comments

Popular posts from this blog

C# - Decision Making

  <<PreviousPage     NextPage>> Decision Making If Else Statement If Else statement is used for decision making in c# . In the below example we will see how we can use if else statement in C# to great effect.   Nested If Else Statement We can improve upon the above example by adding nest if else statement. Nested if else statement helps to add extra if else block with in an if or else block. Switch Statement In place where we need multiple if else statements to satisfy a criteria we can add switch statement instead of if else statement.  We can create an application which would answer our question and let us know about the Date and Time. This will be a small AI sort of idea which would recognize your command and perform accordingly.  Code -    Console.WriteLine("................Welcome to Mini AI................");            str...

C - Overview

What is C? It is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It is designed and written by Dennis Ritchie. C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C. The evolution of C took place in the following way: Why should we use C? 1.C language is a building block for many other currently known languages. 2.There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs. 3.C language is a structured programming language. This makes user to think of a problem in terms of function modules or blocks. Collection of these modules makes a complete program. This modular structure makes program debugging, testing and maintenance easier. What is ANSI C? 1.ANSI C, ISO...