C# Type Conversion: Methods and Examples, Lecture notes of Calculus

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

2021/2022

Uploaded on 09/12/2022

jeny
jeny 🇺🇸

4.6

(14)

251 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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());
Numeric Type
Method
decimal
ToDecimal(String)
float
ToSingle(String)
double
ToDouble(String)
short
ToInt16(String)
int
ToInt32(String)
long
ToInt64(String)
ushort
ToUInt16(String)
uint
ToUInt32(String)
ulong
ToUInt64(String)
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: -105
Int edad;
string nombre;
Console.WriteLine (Introduzca su nombre: );
Nombre=Console.ReadLine();
Console.WriteLine (“Introduzca su edad: “);
edad= int.Parse((Console.ReadLine());
pf3
pf4

Partial preview of the text

Download C# Type Conversion: Methods and Examples and more Lecture notes Calculus in PDF only on Docsity!

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());

Numeric Type Method

decimal ToDecimal(String)

float ToSingle(String)

double ToDouble(String)

short ToInt16(String)

int ToInt32(String)

long ToInt64(String)

ushort ToUInt16(String)

uint ToUInt32(String)

ulong ToUInt64(String)

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();

C# Type Conversion Methods

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