c# Parse()方法

Parse()方法用于将任意字符串转换成任意类型,实际上是一种对字符串的解析。
要求等式左右两边的类型要匹配
例一:
将字符串类型转换为布尔类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApp4
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            string isTrue = "true";  
            bool a = bool.Parse(isTrue);
            Console.WriteLine(isTrue);  
            Console.ReadKey();
        }
    }
}

输出:
true
例二:
将字符串类型转换为int类型

string a = "12345";
int b = int.Parse(b);

输出:
12345

おすすめ

転載: blog.csdn.net/qq_43511299/article/details/120482590
おすすめ