Objective C # provides a meaningful tostring Function

Source: Internet
Author: User

When writing custom types, even if we do not writeTostringFunction, the system will also automatically provideTostringFunction, for example:

Public ClassClsuserinfo

{

Private StringStrusername;

......

}

 

However, the system providesTostringFunction, not doing too many things, and cannot truly reflect some attributes of the current object.Code, CallTostringReturned results andGetTypeConvertStringIs the same. The returned value does not have much meaning for the caller.

 

To provide a meaningfulTostringFunction, You need to reloadTostringFunction to implement an object-orientedTostringFunction. For exampleClsuserinfoThis type will makeTostringFunctions are more meaningful.

Public ClassClsuserinfo

{

Private StringStrusername;

......

Public Override StringTostring ()

{

Return String. Format ("User name: {0}", strusername );

}

}

 

Provide a meaningfulTostringFunctions are useful for debugging or publishing such a feature, because this method is the easiest way to combine strings with the member attributes of the feature class..However, when writing code, people, including me, tend to ignore providing a meaningfulTostringFunction, which seems to be improved in future encoding.

 

tostring C # iformattable interface, to provide more complex tostring functions, I don't want Because the interface is inherited, the tostring functions may change a lot. This can be realized only in real applications.

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.