ความแตกต่างระหว่างConvert.ToString()และ.ToString()คืออะไร?
ฉันพบความแตกต่างมากมายทางออนไลน์ แต่ความแตกต่างที่สำคัญคืออะไร
ความแตกต่างระหว่างConvert.ToString()และ.ToString()คืออะไร?
ฉันพบความแตกต่างมากมายทางออนไลน์ แต่ความแตกต่างที่สำคัญคืออะไร
คำตอบ:
Convert.ToString()จัดการnullในขณะที่ToString()ไม่
nullส่งคืนสตริงว่างหรือโยนข้อยกเว้นหรือไม่ มันเหมือนกับความแตกต่างระหว่างการคัดเลือกนักแสดงและการใช้as: การแปลงแบบเงียบ
                    การเรียกใช้ToString()บนวัตถุสันนิษฐานว่าวัตถุนั้นไม่เป็นโมฆะ (เนื่องจากวัตถุจำเป็นต้องมีอยู่เพื่อเรียกเมธอดอินสแตนซ์กับมัน) Convert.ToString(obj)ไม่จำเป็นต้องสันนิษฐานว่าวัตถุนั้นไม่ใช่โมฆะ (เนื่องจากเป็นวิธีการคงที่ในคลาสแปลง) แต่จะกลับมาString.Emptyหากเป็นโมฆะ
Convert.ToString(string value)ส่งกลับถ้าอาร์กิวเมนต์เป็น  null ส่งกลับถ้าอาร์กิวเมนต์เป็น nullConvert.ToString(object value)String.Emptynull
                    นอกเหนือไปจากคำตอบอื่น ๆ เกี่ยวกับการจัดการnullค่าConvert.ToStringพยายามที่จะใช้IFormattableและการเชื่อมต่อก่อนที่จะเรียกฐานIConvertibleObject.ToString
ตัวอย่าง:
class FormattableType : IFormattable
{
    private double value = 0.42;
    public string ToString(string format, IFormatProvider formatProvider)
    {
        if (formatProvider == null)
        {
            // ... using some IOC-containers
            // ... or using CultureInfo.CurrentCulture / Thread.CurrentThread.CurrentCulture
            formatProvider = CultureInfo.InvariantCulture;
        }
        // ... doing things with format
        return value.ToString(formatProvider);
    }
    public override string ToString()
    {
        return value.ToString();
    }
}
ผลลัพธ์:
Convert.ToString(new FormattableType()); // 0.42
new FormattableType().ToString();        // 0,42
              IConvertibleจะมีความสำคัญมากกว่าIFormattableซึ่งจะมีความสำคัญเหนือกว่าObject.ToString()การใช้งาน
                    ให้เข้าใจความแตกต่างผ่านตัวอย่างนี้:
int i= 0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
เราสามารถแปลงจำนวนเต็มiใช้หรือi.ToString () Convert.ToStringดังนั้นความแตกต่างคืออะไร?
ความแตกต่างพื้นฐานระหว่างพวกเขาคือConvertฟังก์ชั่นจัดการ NULLS ในขณะที่i.ToString ()ไม่ได้; มันจะโยนข้อผิดพลาดข้อยกเว้นอ้างอิง NULL ดังนั้นการใช้โค้ดที่ดีconvertจึงปลอดภัยเสมอ
คุณสามารถสร้างคลาสและแทนที่toStringวิธีการทำสิ่งที่คุณต้องการ
ตัวอย่างเช่นคุณสามารถสร้างคลาส"MyMail"และแทนที่toStringวิธีการส่งอีเมลหรือดำเนินการอื่นแทนการเขียนวัตถุปัจจุบัน
Convert.toStringแปลงค่าที่ระบุเพื่อแสดงสตริงเทียบเท่า
object o=null;
string s;
s=o.toString();
//returns a null reference exception for string  s.
string str=convert.tostring(o);
//returns an empty string for string str and does not throw an exception.,it's 
//better to use convert.tostring() for good coding
              วิธีการที่จะ "พื้น" เดียวกันยกเว้นจัดการnull
Pen pen = null; 
Convert.ToString(pen); // No exception thrown
pen.ToString(); // Throws NullReferenceException
จาก MSDN: 
วิธีการ Convert.ToString
แปลงค่าที่ระบุเป็นการแสดงค่าสตริงที่เทียบเท่า
ส่งคืนสตริงที่แสดงถึงวัตถุปัจจุบัน
null, ""หรือ"null"?
                    ในConvert.ToString()การแปลงจัดการNULLค่าหรือไม่ แต่.ToString()มันไม่จัดการNULLค่าและNULLข้อผิดพลาดข้อยกเว้นการอ้างอิง ดังนั้นจึงเป็นการใช้งานที่Convert.ToString()ดี
สำหรับคนรักรหัสนี่เป็นคำตอบที่ดีที่สุด
    .............. Un Safe code ...................................
    Try
        ' In this code we will get  "Object reference not set to an instance of an object." exception
        Dim a As Object
        a = Nothing
        a.ToString()
    Catch ex As NullReferenceException
        Response.Write(ex.Message)
    End Try
    '............... it is a safe code..............................
    Dim b As Object
    b = Nothing
    Convert.ToString(b)
              ฉันเห็นด้วยกับคำตอบของ@ Ryan โดยวิธีการเริ่มต้นด้วย C # 6.0 เพื่อจุดประสงค์นี้คุณสามารถใช้:
someString?.ToString() ?? string.Empty;
หรือ
$"{someString}"; // I do not recommend this approach, although this is the most concise option.
แทน
Convert.ToString(someString);
              ToString()ไม่สามารถจัดการกับค่า null และสามารถจัดการกับค่าที่เป็นโมฆะดังนั้นเมื่อคุณต้องการระบบการจัดการการใช้งานคุ้มค่าconvert.ToString() nullconvert.ToString()
Convert.ToString(strName)จะจัดการกับค่าที่สามารถเป็นโมฆะและstrName.Tostring()จะไม่จัดการกับค่าที่เป็นโมฆะและโยนข้อยกเว้น
ดังนั้นมันจะดีกว่าที่จะใช้Convert.ToString()แล้ว.ToString();
ToString() Vs Convert.ToString()
ความคล้ายคลึงกัน: -
ทั้งสองจะใช้ในการแปลงประเภทที่เฉพาะเจาะจงเพื่อสตริงเช่น int เพื่อสตริงลอยเป็นสตริงหรือวัตถุที่สตริง
ความแตกต่าง: -
ToString()ไม่สามารถจัดการค่าว่างในขณะที่ในกรณีที่Convert.ToString()จะจัดการกับค่า null
ตัวอย่าง:
namespace Marcus
{
    class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    class Startup
    {
        public static void Main()
        {
            Employee e = new Employee();
            e = null;
            string s = e.ToString(); // This will throw an null exception
            s = Convert.ToString(e); // This will throw null exception but it will be automatically handled by Convert.ToString() and exception will not be shown on command window.
        }
    }
}
              Convert.ToStringNull Exceptionไม่ได้จัดการ ทำได้ง่ายๆ:return value == null ? string.Empty : value.ToString()
                    เพื่อให้เข้าใจทั้งสองวิธีลองดูตัวอย่าง:
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i)); 
ที่นี่ทั้งสองวิธีจะใช้ในการแปลงสตริง แต่ความแตกต่างพื้นฐานระหว่างพวกเขาคือ: Convertจัดการฟังก์ชั่นNULLในขณะที่i.ToString()มันจะไม่โยนNULL reference exception error.ดังนั้นการฝึกการเขียนโปรแกรมที่ดีใช้convertปลอดภัยเสมอ
ลองดูตัวอย่างอื่น:
string s;
object o = null;
s = o.ToString(); 
//returns a null reference exception for s. 
string s;
object o = null;
s = Convert.ToString(o); 
//returns an empty string for s and does not throw an exception.
              Convert.ToString(value)ก่อนอื่นให้ลองทำการ obj กับIConvertibleจากนั้นIFormattableเพื่อเรียกToString(...)วิธีการที่เกี่ยวข้อง ถ้าแทนค่าพารามิเตอร์ที่ถูกแล้วกลับnull string.Emptyเป็นทางเลือกสุดท้ายให้กลับมาobj.ToString()ถ้าไม่มีอะไรทำงาน
เป็นที่น่าสังเกตว่าConvert.ToString(value) สามารถส่งคืนได้nullหากตัวอย่างvalue.ToString()คืนค่าเป็นศูนย์
ฉันเขียนโค้ดนี้และรวบรวมมัน
class Program
{
    static void Main(string[] args)
    {
        int a = 1;
        Console.WriteLine(a.ToString());
        Console.WriteLine(Convert.ToString(a));
    }
}
โดยใช้ 'วิศวกรรมย้อนกลับ' ( ilspy ) ฉันหา 'object.ToString ()' และ 'Convert.ToString (obj)' ทำสิ่งเดียว infact 'Convert.ToString (obj)' call 'object.ToString ()' ดังนั้น'object.ToString ()' จึงเร็วขึ้น
class System.Object
{
    public string ToString(IFormatProvider provider)
    {
        return Number.FormatInt32(this, null, NumberFormatInfo.GetInstance(provider));
    }
}
class System.Convert
{
    public static string ToString(object value)
    {
        return value.ToString(CultureInfo.CurrentCulture);
    }
}
              ใน C # หากคุณประกาศตัวแปรสตริงและหากคุณไม่ได้กำหนดค่าใด ๆ ให้กับตัวแปรนั้นโดยค่าเริ่มต้นแล้วตัวแปรจะรับค่า Null ในกรณีเช่นนี้หากคุณใช้เมธอด ToString () โปรแกรมของคุณจะแสดงข้อยกเว้นการอ้างอิงเป็นโมฆะ ในทางกลับกันถ้าคุณใช้เมธอด Convert.ToString () โปรแกรมของคุณจะไม่ส่งข้อยกเว้น
Convert.Tostring() โดยทั่วไปเพียงแค่เรียกสิ่งต่อไปนี้ value == null ? String.Empty: value.ToString()
(string)variableจะส่งเฉพาะเมื่อมีโอเปอเรเตอร์โดยนัยหรือชัดเจนในสิ่งที่คุณแคสต์
ToString()สามารถoverridenตามประเภท (มันมีการควบคุมสิ่งที่มันทำ) ถ้าไม่ส่งผลในชื่อของประเภท
เห็นได้ชัดว่าถ้าวัตถุนั้นเป็นโมฆะคุณไม่สามารถเข้าถึงสมาชิกอินสแตนซ์ToString()มันจะทำให้เกิดข้อยกเว้น