C # Constants

Source: Internet
Author: User

Constants are fixed values that do not change during program execution. Constants can be any basic data type, such as Integer constants, floating-point constants, character constants, or string constants, as well as enumeration constants.

Constants can be used as regular variables, except that their values cannot be modified after they are defined.

Integer constants

An integer constant can be a decimal, octal, or hexadecimal constant. The prefix specifies the cardinality: 0x or 0X for hexadecimal, 0 for octal, and no prefix for decimal.

Integer constants can also have suffixes, which can be a combination of U and L, where u and L represent unsigned and long respectively. Suffixes can be uppercase or lowercase, and multiple suffixes are combined in any order.

Here are some examples of integer constants:

212         /* Legal*/215u */legal */0xFeeL//Legal */078/* Illegal: 8 not an octal number */032UU/* Illegal: cannot repeat suffix */        

The following are examples of the various types of integer constants:

*/         * decimal */0213*/octal */0X4B */hex *//INT */30u/* unsigned int */3 0l/* Long/30ul/* unsigned long *            / 
Floating-point constants

A floating-point constant consists of an integral part, a decimal point, a fractional part, and an exponential part. You can use decimal or exponential form to represent floating-point constants.

Here are some examples of floating-point constants:

3.14159       /* Legal */314159e-5l*/legal */510E* Illegal: Incomplete index */210f/* Illegal: No decimal or Index */. c18>/* illegal: missing integers or decimals */        

When you use decimal notation, you must include a decimal point, an exponent, or both. When used as an exponential representation, you must include an integer part, a fractional part, or both. The signed exponent is denoted by E or E.

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   
String constants

Character constants are enclosed in double quotes "", or are enclosed in @ "". String constants contain characters similar to character constants, which can be: ordinary characters, escape sequences, and universal characters

When you use string constants, you can split a very long line into multiple rows, and you can separate sections with spaces.

Here are some examples of string constants. The various forms listed below represent the same string.

String a = "Hello, world";                  Hello, worldstring b = @ "Hello, world";               Hello, worldstring c = "Hello \ t World";               Hello     worldstring d = @ "Hello \ t World";               Hello \ t worldstring e = "Joe said \" Hello\ "to Me";      Joe said "Hello" to mestring f = @ "Joe said" "Hello" "To Me";   Joe said "Hello" to mestring g = "\\\\server\\share\\file.txt";   \\server\share\file.txtstring h = @ "\\server\share\file.txt";      \\server\share\file.txtstring i = "One\r\ntwo\r\nthree"; string j = @ "Onetwothree";
Defining constants

Constants are defined using the const keyword. The syntax for defining a constant is as follows:

Const<data_type><constant_name>= value;    

The following code demonstrates how to define and use constants in a program:

Using System;Namespace Declaringconstants{ Class Program { Static void Main(String[]Args) { Const DoublePi= 3.14159; Constant declaration DoubleR; Console.WriteLine("Enter Radius:");R= Convert.ToDouble(Console.readline ());  double areacircle = pi Span class= "pun" >* R * R; console. Writeline ( "Radius: {0}, area: {1}" , R, Areacircleconsole. Readline} }}    /span>                

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

Enter Radius:3radius:3, area:28.27431

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.