C # Constants

Source: Internet
Author: User

Character constants

Character constants are enclosed in single quotes, for example, ' X ', and can be stored in a simple character type variable. A character constant can be a normal character (for example, ' X '), an escape sequence (such as ' \ t '), or a universal character (such as ' \u02c0 ').

There are certain characters in C # that have a special meaning when preceded by a backslash, which can be used to represent a newline character (\ n) or Tab tab (\ t). Here, some escape sequence codes are listed:

Escape Sequences meaning
\\ \ character
\‘ ' Character
\" "Character
\? ? Character
\a Alert or Bell
\b BACKSPACE (Backspace)
\f Page break (Form feed)
\ n Line Break (Newline)
\ r Enter
\ t Horizontal Tab tab
\v Vertical Tab tab
\ooo Octal number of one to three bits
\xhh ... Hexadecimal number of one or more digits

Here are some examples of escape sequence characters:

Namespace Escapechar{ Class program {  void main (string[] Args) { console. Writelineconsole. Readline} }}    /span>                

When the above code is compiled and executed, it produces the following results:

Hello World   


1, the difference between convert.todouble and Double.Parse. In fact, convert.todouble and Double.Parse are more similar, in fact convert.todouble internally called Double.Parse:

(1) When the parameter is null:

    • When the convert.todouble parameter is NULL, 0.0 is returned;
    • Throws an exception when the Double.Parse parameter is null.

(2) when the parameter is "":

    • An exception is thrown when the convert.todouble parameter is "";
    • Throws an exception when the Double.Parse parameter is "".

(3) Other differences:

    • Convert.todouble can be converted to more types;
    • Double.Parse can only convert strings of numeric types.
    • Double.tryparse and Double.Parse are more similar, but it does not produce an exception, the conversion succeeds returns true, and the conversion fails to return false. The last parameter is the output value, and if the conversion fails, the output value is 0.0.

Attached test code:

Using System;Using System.Collections.Generic;Using System.Linq;Using System.Text;Namespace ConsoleApplication1{ Class Program { Static void Main(String[]Args) { Try { String a = "0.2"; string a = null; StringA= ""; Try { DoubleD1= Double.Parse(A); } Catch (ExceptionErr) { Console.WriteLine("D1 Conversion Error:" +Err.Message); } Try { DoubleD2= Convert.ToDouble(A); } Catch (ExceptionErr) { Console.WriteLine("D2 Conversion Error:" +Err.Message); } Try { DoubleD3; Double.TryParse(A,OutD3); } Catch (ExceptionErr  { console . writeline ( "D3 conversion error:"  +  Err. Message } } finally  { console. Readkey} } }< Span class= "pun" >               

2.convert.toint32 () and Int. The difference between the Parse ()

Do not understand Chu Convert.ToInt32 and int.parse () fine nuances, do not mess with, otherwise it may produce unpredictable results, for example: If you take a parameter page from the URL value, we know that this value is an int, So that you can use Convert.ToInt32 (request.querystring["page"]), you can also use, int. Parse (request.querystring["page"]), but if the page parameter does not exist in the URL, then the former will return 0,0 may be a valid value, so you do not know that the URL is not at all in this parameter and continue the next processing, This can have unintended effects, and in the latter case there is no page this argument throws an exception, we can catch the exception and then do the corresponding processing, such as prompting the user to lack of parameters, rather than the parameter value as a limit processing.

(1) The biggest difference between the two methods is their handling of the null value:

Convert.ToInt32 (NULL) returns 0 without generating any exceptions, but int. Parse (NULL) produces an exception.

(2) The difference between rounding the data

A. Convert.ToInt32 (double value)

If value is a number in the middle of two integers, the even numbers are returned, that is, 3.5 is converted to 4, 4.5 is converted to 4, and 5.5 is converted to 6. But 4.6 can be converted to 5, 4.4 to 4

B. Int. Parse ("4.5")

Direct error: "The input string is not properly formatted."

C. int (4.6) = 4

int conversions Other numeric types are int when not rounded, cast

(3) The difference between the type of being converted

Int. Parse is converting string to int

Convert.ToInt32 is the conversion of objects that inherit from object to int (there can be many other types of data).

You get an object, you want to convert it to int, with Int. It is not possible to parse, but to use Convert.ToInt32.

C # Constants

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.