- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
3: C# Language Fundamentals in C#
| 3: C# Language Fundamentals Paint Code-128 In C#.NET Using Barcode creation for .NET Control to generate, create Code 128A image in Visual Studio .NET applications. www.OnBarcode.comScanning USS Code 128 In C# Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comThe entries in the enumeration are separated by commas. Many programmers like to leave a comma after the last entry in an enumeration as a convenience for adding more values later. Other programmers find this, at best, sloppy. The code will compile either way. PDF-417 2d Barcode Creator In C# Using Barcode printer for .NET framework Control to generate, create PDF 417 image in VS .NET applications. www.OnBarcode.comGTIN - 128 Generator In C#.NET Using Barcode encoder for .NET Control to generate, create UCC.EAN - 128 image in Visual Studio .NET applications. www.OnBarcode.comThe syntax for specifying an enumeration uses the enum keyword, as follows: Create GTIN - 13 In C#.NET Using Barcode creator for .NET framework Control to generate, create EAN-13 image in Visual Studio .NET applications. www.OnBarcode.comBarcode Generator In C#.NET Using Barcode generator for Visual Studio .NET Control to generate, create Barcode image in .NET framework applications. www.OnBarcode.comenum identifier [:base-type] {enumerator-list}; Generate 2D In Visual C# Using Barcode printer for .NET framework Control to generate, create 2D Barcode image in .NET applications. www.OnBarcode.comPrint Identcode In C#.NET Using Barcode encoder for Visual Studio .NET Control to generate, create Identcode image in .NET framework applications. www.OnBarcode.comIn a specification statement such as the preceding example, the square brackets indicate an optional element. Thus, you can declare an enum with no base type, simply by leaving it out (leave out the square brackets as well). Print Code128 In Visual Studio .NET Using Barcode encoder for ASP.NET Control to generate, create USS Code 128 image in ASP.NET applications. www.OnBarcode.comCode 128 Code Set B Encoder In .NET Framework Using Barcode creation for Reporting Service Control to generate, create Code 128 Code Set C image in Reporting Service applications. www.OnBarcode.comThere are also optional attributes and modifiers you can use, but you don t need them right now. An enumeration begins with the keyword enum, which is generally followed by an identifier; in this case, Temperatures: Reading Code 128B In VB.NET Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comANSI/AIM Code 39 Generation In Java Using Barcode drawer for Eclipse BIRT Control to generate, create Code-39 image in Eclipse BIRT applications. www.OnBarcode.comenum Temperatures
EAN-13 Recognizer In VB.NET Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comDrawing USS Code 128 In Visual Basic .NET Using Barcode creation for .NET Control to generate, create USS Code 128 image in .NET framework applications. www.OnBarcode.comThe base type is the underlying type for the enumeration. You might specify that you are declaring constant ints, constant longs, or something else. If you leave out this optional value (and often you will), it defaults to int, but you are free to use any of the integral types (ushort, long) except for char. For example, the following fragment declares an enumeration with unsigned integers (uint) as the base type: Generate EAN128 In .NET Framework Using Barcode drawer for Visual Studio .NET Control to generate, create UCC.EAN - 128 image in Visual Studio .NET applications. www.OnBarcode.comUPC Code Generation In None Using Barcode generation for Software Control to generate, create UCC - 12 image in Software applications. www.OnBarcode.comenum ServingSizes : uint { Small = 1, Regular = 2, Large = 3 } ECC200 Generator In Visual Studio .NET Using Barcode drawer for Reporting Service Control to generate, create ECC200 image in Reporting Service applications. www.OnBarcode.comDrawing Code 128 Code Set C In Java Using Barcode generation for BIRT Control to generate, create Code 128 Code Set C image in BIRT applications. www.OnBarcode.comNotice that an enum declaration ends with the enumerator list, which contains the constant assignments for the enumeration, each separated by a comma. Example 3-5 rewrites Example 3-4 to use an enumeration. Barcode Creator In .NET Using Barcode creator for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comPrint EAN128 In .NET Framework Using Barcode encoder for Reporting Service Control to generate, create EAN / UCC - 14 image in Reporting Service applications. www.OnBarcode.comusing using using using System; System.Collections.Generic; System.Linq; System.Text; namespace Example_3_5_ _ _ _Enumerations { class Values { // declare the enumeration
Constants
| enum Temperatures { WickedCold = 0, FreezingPoint = 32, LightJacketWeather = 60, SwimmingWeather = 72, BoilingPoint = 212, } static void Main( ) { System.Console.WriteLine("Freezing point of water: {0}", (int)Temperatures.FreezingPoint); System.Console.WriteLine("Boiling point of water: {0}", (int)Temperatures.BoilingPoint); } } } In Example 3-5, you declare an enumerated constant called Temperatures. When you want to use any of the values in an enumeration in a program, the values of the enumeration must be qualified by the enumeration name. That is, you can t just refer to FreezingPoint; instead, you use the enumeration identifier (Temperature) followed by the dot operator and then the enumerated constant (FreezingPoint). This is called qualifying the identifier FreezingPoint. Thus, to refer to the FreezingPoint, you use the full identifier Temperature.FreezingPoint. You might want to display the value of an enumerated constant to the console, as in the following: Console.WriteLine("The freezing point of water is {0}", (int) Temperature.FreezingPoint); To make this work properly, you must cast the constant to its underlying type (int). In this case, you are saying, Treat this enumerated constant as an int. Because you know the underlying type is int, this is safe to do. (See Casting earlier in this chapter.) In Example 3-5, the values in the two enumerated constants FreezingPoint and BoilingPoint are both cast to type int; then that integer value is passed to WriteLine( ) and displayed. Each constant in an enumeration corresponds to a numerical value. In Example 3-5, each enumerated value is an integer. If you don t specifically set it otherwise, the enumeration begins at 0 and each subsequent value counts up from the previous. Thus, if you create the following enumeration: enum SomeValues { | 3: C# Language Fundamentals
First, Second, Third = 20, Fourth } the value of First will be 0, Second will be 1, Third will be 20, and Fourth will be 21.
Strings
It is nearly impossible to write a C# program without creating strings, and we wouldn t want to deprive you of them here. Strings are actually complex classes that we ll cover much more thoroughly in 15. For now, though, all you need to know is that a string object holds a series of characters. You declare a string variable using the string keyword much as you would create an instance of any type: string myString; You specify a string literal by enclosing it in double quotes: "Hello World" You already used a string literal back in 1, in the Hello World example. You ll frequently initialize a string variable by assigning it a string literal: string myString = "Hello World";
|
|