


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Examples and explanations of various type conversion methods in c#. It covers conversion of different data types such as int, decimal, float, double, short, long, ushort, uint, ulong, char, bool, and string. The document also includes the use of console.readline() method and the need for type conversion when reading user input.
Typology: Lecture notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Consola en C#
El método Console.ReadLine() acepta datos en formato string por lo que hay que hacer una conversión de tipos dependiendo de las variables que queremos leer.
Os dejo algunos ejemplos en los que podéis ver los métodos que se necesiten en cada caso.
int num;
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Escriba la longitud:"); string l = Console.ReadLine(); double L = double.Parse(l);
Console.WriteLine("Ingrese primer valor: "); int valor1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("El primer valor es: " + valor1);
int numVal = Int32.Parse("-105"); Console.WriteLine(numVal); // Output: -
Int edad;
string nombre;
Console.WriteLine (“Introduzca su nombre: “);
Nombre=Console.ReadLine();
Console.WriteLine (“Introduzca su edad: “);
edad= int.Parse((Console.ReadLine());
char respuesta=’s’;
Console.WriteLine(“Estás prestando atención? [S/N]:” );
Respuesta =Console.ReadLine()[0];
double d = 5673.74;
int i;
// cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
Sr.No. Methods & Description
(^1) ToBoolean
Converts a type to a Boolean value, where possible.
(^2) ToByte
Converts a type to a byte.
(^3) ToChar
Converts a type to a single Unicode character, where possible.
(^4) ToDateTime
Converts a type (integer or string type) to date-time structures.
(^5) ToDecimal
(^16) ToUInt
Converts a type to an unsigned big integer.
using System;
namespace TypeConversionApplication
{
class StringConversion
{
static void Main(string[] args)
{
int i = 75;
float f = 53.005f;
double d = 2345.7652;
bool b = true;
Console.WriteLine(i.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(b.ToString());
Console.ReadKey();
}
}
}
Output
75
True