DEMO[C#]TypeChange 关于类型转换的一个DEMO

好久没来写文章了,今天写了个DEMO,顺便发上来供大家一同探讨~~,反正写都写了,也就是复制粘贴一下了,好东西要和大家分享,欢迎大家使用噢~~废话不多说,帖代码,好迟了~
说明:
语言:C#.NET
模板类型:Console控制台应用程序。单文件(模板自带:Program.cs)
要点:是一篇关于常见显示类型转换Convert.ToXXXX()代码的应用。

using System;
using System.Collections.Generic;
using System.Text;

namespace CA_TypeChange
{
    
class Program
    
{
        
static void TypeChange()
        
{
            
bool isFinished = false;
            
do
            
{
                
try
                
{
                    
/*需要了解类型的具体信息,可以激活相应代码,将鼠标指向它,
                     * 将会有提示,或查询MSDN或相关教材
*/


                    Console.WriteLine(
"Please enter \"true\"or \"false\".");
                    
bool boolVals = Convert.ToBoolean(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", boolVals);

                    Console.WriteLine(
"Please enter a byte word(0-255),for example:1 or 3.");
                    
byte byteVals = Convert.ToByte(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", byteVals);

                    Console.WriteLine(
"Please enter a Char,for example:a or c.");
                    Char charVals 
= Convert.ToChar(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", charVals);

                    Console.WriteLine(
"Please enter a Decimal word,for example:10 or 33.");
                    Decimal decimalVals 
= Convert.ToDecimal(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", decimalVals);

                    Console.WriteLine(
"Please enter a double word,for example:1.44 .");
                    
double doubleVals = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", doubleVals);

                    Console.WriteLine(
"Please enter a Int(16) word.");
                    Int16 int16Vals 
= Convert.ToInt16(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", int16Vals);

                    Console.WriteLine(
"Please enter a Int(32) word.");
                    Int32 int32Vals 
= Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", int32Vals);

                    Console.WriteLine(
"Please enter a Int(64) word.");
                    Int64 int64Vals 
= Convert.ToInt64(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", int64Vals);

                    Console.WriteLine(
"Please enter a SByte word,for example: -29 or 23");
                    SByte sbyteVals 
= Convert.ToSByte(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", sbyteVals);

                    Console.WriteLine(
"Please enter a Single(float) word,for example: 3.5 ");
                    Single singleVals 
= Convert.ToSingle(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", singleVals);

                    Console.WriteLine(
"Please enter a UInt(16) word.");
                    UInt16 uInt16Vals 
= Convert.ToUInt16(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", uInt16Vals);

                    Console.WriteLine(
"Please enter a UInt(32) word.");
                    UInt32 uInt32Vals 
= Convert.ToUInt32(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", uInt32Vals);

                    Console.WriteLine(
"Please enter a UInt(64) word.");
                    UInt64 uInt64Vals 
= Convert.ToUInt64(Console.ReadLine());
                    Console.WriteLine(
"It's {0} .", uInt64Vals);

                    isFinished 
= true;
                }

                
catch (FormatException e)
                
{
                    Console.WriteLine(
"You write a error word.");
                }

            }
 while (isFinished == false);
        }

        
static int ReturnToContinue(string switchKey)
        
{
            
if (switchKey.ToLower() == "y")
                
return 1;
            
else if (switchKey.ToLower() == "n")
                
return 0;
            
else
            
{
                Console.WriteLine(
"Please only input \"y\" or \"n\".Try again (Y/N)?");
                
return 2;
            }

        }

        
static void Main(string[] args)
        
{
            
int iIsReturn = 0;
            Console.WriteLine(
"Hello,this is a program for testing type in CSharp Language."+
                
"\nPlease press any key to start your test:\n\n");
            Console.ReadKey();
            
do
            
{
                TypeChange();

                Console.WriteLine(
"Thank you for your test!Good bye!\n" +
                 
"You will learn more if you understand all above.\n" +
                 
"Do you want to try it again?\n" +
                 
"Choose \"Y\" means yes,or \"N\" means no!   (Y/N)?\n");   //写分行代码的时候你需要用到加号

                
do
                
{
                    
string switchKey = Console.ReadLine();
                    iIsReturn 
= ReturnToContinue(switchKey);
                }
 while (iIsReturn == 2);
            }
 while (iIsReturn == 1);
            Console.ReadKey();
        }

    }

}


有任何疑问请留言或发送邮件,我将及时为大家解答。因为只是DEMO,所以会有很多的BUG,请大家见谅!
原文地址:https://www.cnblogs.com/volnet/p/552441.html