C # data type conversion methods

Source: Internet
Author: User

Implicit conversions:

You don't need to do any work, and you don't need to write code.
Cases:
VAR1=VAR2;

Type can be implicitly converted to
Byte Short,ushort,int,uint,long,ulong,float,double,decimal
SByte Short,int,long,float,double,decimal
Short Int,long,float,double,decimal
UShort Int,uint,long,ulong,float,double,decimal
Int Long,ulong,double,decimal
UInt Long,ulong,float,double,decimal
Long Float,double,decimal
ULong Float,double,decimal
Float Double
Char Ushort,int,uint,long,ulong,flaot,double,decimal











Explicit Conversions:

is to force the data to be converted from one type to another.
Grammar:
(destinationtype) Sourcevar
Note: If the target data type of the data conversion is not appropriate, then there will be data loss or inability to convert.


Display the conversion method in convert:

If you use convert to convert a data type, a data overflow phenomenon cannot be converted.

Command results

Convert.toboolean (Val) Val converts to bool

Convert.tobyte (Val) Val converted to byte

Convert.tochar (Val) Val converts to Char

Convert.todecimal (Val) Val converts to decimal

Convert Convert.todouble (Val) Val to double

Convert.ToInt16 (Val) Val converts to short

Convert.ToInt32 (Val) Val converts to int

Convert.toint64 (Val) Val converts to Long

Convert.tosbyte (Val) Val converts to SByte

Convert.tosingle (Val) Val converted to float

Convert Convert.ToString (Val) Val to string

Convert.touint16 (Val) Val converts to UShort

Convert.touint32 (Val) Val converts to UINT

Convert.touint64 (Val) Val converts to ulong


Ways to handle strings:
String.     ToCharArray (); To output a string as a character array

String.            ToLower (); Output string to uppercase

String.            ToUpper (); To output a string as a lowercase letter

String.                   Trim (); Remove a space before and after a string

String.               PadLeft (Int32); Right-aligns the characters in this instance, padding on the left with spaces to reach the specified total length.

String.    PadLeft (Int32, Char); Aligns the characters in this instance and fills the left with the specified Unicode character to reach the specified total length.

String.            PadRight (Int32); Similarly

String.Split (char[]); The returned string array contains the substrings in this instance
String.Split (Char[],int32);

Two examples of the split method:

Using System;

public class Splittest {public
    static void Main () {

        string words = ' This is a list of words, with:a bit of punct Uation "+
                       " \tand a tab character. "

        string [] split = words. Split (New Char [] {', ', ', ', ', ', ', ', ', '. ', ': ', ' \ t '});

        foreach (string s in Split) {

            if (S.trim ()!= "")
                Console.WriteLine (s);
}}} The example displays the following output to the console:
//       This
/is
//       a
//< C19/>list
/Of
//       words
//with
//       a
//       bit
//       of
//       punctuation
//       and
//       a
//       tab
//       character
Using System;
	   public class StringSplit2 {public static void Main () {String delimstr = ",.:";
      char [] delimiter = Delimstr.tochararray ();
      String words = "one two,three:four.";

	   string [] split = null;
	   Console.WriteLine ("The Delimiters are-{0}-", delimstr); for (int x = 1; x <= 5 x + +) {split = words.
         Split (delimiter, x);
	       Console.WriteLine ("\ncount = {0,2} ... ...", x); and.
  	       foreach (string s in Split) {Console.WriteLine ("-{0}-", s); }}//The example displays the following output://The delimiters are-,.:-//Count = 1 ...       ...//-one two,three:four.-//Count = 2 ....//-one-//-two,three:four.-//* * * *./       Count = 3 .....//-one-//-two-//-three:four.-//Count = 4 ...//....//..// -one-//-two-//-three-//-four.-//Count = 5 ......./-one-//-two-//-three-//-four-//-
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.