Differences between asp.net ToString format and Convert. ToString (1/2)

Source: Internet
Author: User
Tags datetime

Asp tutorial. net tostring format Daquan and convert. tostring difference

Generally, there are four methods for object to string: (assume object obj) obj. tostring, convert. tostring, (string) obj, obj as string.
They can all convert object objects to string objects. Let me talk about their similarities and differences and what they should be used in practice.

The first two methods are generally string objects obtained from other objects. The difference between them is only that when the object to be converted is null, if obj is null, obj is called. the tostring method causes nullreferenceexception and calls convert. tostring does not throw an exception and returns a null value.
Forced conversion (string) obj requires that the runtime type of obj must be string. If not, an exception is thrown. The as method is relatively stable. If the runtime type of obj is not string, null is returned without throwing an exception.

Therefore, when we need to get the string expression of an object, we should use tostring and convert. tostring. At this time, you have to select one based on the situation. If you can ensure that your object is not null, then the two are similar. If it is possible to be null, you should use convert. tostring. If you want it to be null and throw an exception, you can select. tostring.

Convert string

// C currency

2.5.tostring ("c"); // ¥2.50

// D 10-digit number

25. tostring ("d5"); // 25000

// E scientific type

25000. tostring ("e"); // 2.500000e + 005

// F fixed point

25. tostring ("f2"); // 25.00

// G general

2.5.tostring ("g"); // 2.5

// N number

2500000. tostring ("n"); // 2,500,000.00

// X hexadecimal

255. tostring ("x"); // ff


// C # Date format
Datetime dt = datetime. now;
 
Dt. tostring (); // 13:21:25

Dt. tofiletime (). tostring (); // 127756416859912816

Dt. tofiletimeutc (). tostring (); // 127756704859912816

Dt. tolocaltime (). tostring (); // 21:21:25

Dt. tolongdatestring (). tostring (); // November 5, 2005

Dt. tolongtimestring (). tostring (); // 13:21:25

Dt. tooadate (). tostring (); // 38661.5565508218

Dt. toshortdatestring (). tostring (); // 2005-11-5

Dt. toshorttimestring (). tostring (); // 13:21

Dt. touniversaltime (). tostring (); // 5:21:25

Dt. year. tostring (); // 2005

Dt. date. tostring (); // 0:00:00

Dt. dayofweek. tostring (); // Saturday

Dt. dayofyear. tostring (); // 309

Dt. hour. tostring (); // 13

Dt. millisecond. tostring (); // 441

Dt. minute. tostring (); // 30

Dt. month. tostring (); // 11

Dt. second. tostring (); // 28

Dt. ticks. tostring (); // 632667942284412864

Dt. timeofday. tostring (); // 13:30:28. 4412864

Dt. tostring (); // 13:47:04

Dt. addyears (1). tostring (); // 13:47:04

Dt. adddays (1.1). tostring (); // 16:11:04

Dt. addhours (1.1). tostring (); // 14:53:04

Dt. addmilliseconds (1.1). tostring (); // 13:47:04

Dt. addmonths (1). tostring (); // 13:47:04

Dt. addseconds (1.1). tostring (); // 13:47:05

Dt. addminutes (1.1). tostring (); // 13:48:10

Dt. addticks (1000). tostring (); // 13:47:04

Dt. compareto (dt). tostring (); // 0

Dt. add (?). Tostring (); // question mark is a time period

Dt. equals ("16:11:04"). tostring (); // false

Dt. equals (dt). tostring (); // true

Dt. gethashcode (). tostring (); // 1474088234

Dt. gettype (). tostring (); // system. datetime

Dt. gettypecode (). tostring (); // datetime

   

Homepage 1 2 Last page
Related Article

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.