C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures

things about

1. Declare an enumeration type.

2. Create and use an enumeration type.

3. Declare a structure type.

4. Create and use a structure type.

5. Explain the differences in behavior between a structure and a class.

Declaring an enumeration

enum Season { Spring, Summer, Fall, Winter }

Using an enumeration

You can assign a value that is defined by the enumeration only to an enumeration variable.

Note:As you can with all value types, you can create a nullable version of an enumeration variable by using the ? modifier. You can then assign the null value, as well the values defined by the enumeration, to
the variable: Season? colorful = null;

All enumeration literal names are scoped by their enumeration type. This is useful because it allows different enumerations to coincidentally contain literals with the same name.

Many of the standard operators you can use on integer variables can also be used on enumeration variables (except the bitwise and shift operators, which are covered in Chapter 16, “Using Indexers”). For example, you can compare two enumeration
variables of the same type for equality by using the equality operator (==), and you can even perform arithmetic on an enumeration variable (although the result might not always be meaningful!).

Choosing enumeration Literal Values

Internally, an enumeration type associates an integer value with each element of the enumeration. By default, the numbering starts at 0 for the first element and goes up in steps of 1.

If you prefer, you can associate a specific integer constant (such as 1) with an enumeration literal (such as Spring), as in the following example:

enum Season { Spring = 1, Summer, Fall, Winter }

Important:The integer value with which you initialize an enumeration literal must be a compile-time constant value (such as 1).

the underlying values of Spring, Summer, Fall, and Winter are now 1, 2, 3, and 4.

You are allowed to give more than one enumeration literal the same underlying value.

enum Season { Spring, Summer, Fall, Autumn = Fall, Winter }

Choosing an enumeration’s Underlying type

When you declare an enumeration, the enumeration literals are given values of type int. You can also choose to base your enumeration on a different underlying integer type. For example, to declare that Season’s underlying type is a short rather
than an int, you can write this:

enum Season : short { Spring, Summer, Fall, Winter }

The main reason for doing this is to save memory; an int occupies more memory than a short, and if you do not need the entire range of values available to an int, using a smaller data type can make sense.

You can base an enumeration on any of the eight integer types: byte, sbyte, short, ushort, int, uint,long, or ulong. The values of all the enumeration literals must fit inside the range of the chosen base type. For example, if you base an enumeration
on the byte data type, you can have a maximum of 256 literals (starting at 0).

Working with Structures

In some cases, the class can contain so little data that the overhead of managing the heap becomes disproportionate. In these cases, it is better to define the type as a structure. A structure is a value type. Because structures are stored on
the stack, as long as the structure is reasonably small, the memory management overhead is often reduced.

Like a class, a structure can have its own fields, methods, and (with one important exception discussed later in this chapter) constructors.

Common Structure types

In C#, the primitive numeric types int, long, and float are aliases for the structures System.Int32, System.Int64, and System.Single, respectively. These structures have fields and methods, and you can actually call methods on variables and literals
of these types.

Declaring a Structure

To declare your own structure type, you use the struct keyword followed by the name of the type, followed by the body of the structure between opening and closing braces. Syntactically, the process is similar to declaring a class.

As with classes, making the fields of a structure public is not advisable in most cases; there is no way to control the values held in public fields.A better idea is to make the fields private and provide your structure with constructors and methods
to initialize and manipulate these fields。

Note By default, you cannot use many of the common operators on your own structure types. For example, you cannot use operators such as the equality operator (==) and the inequality operator (!=) on your own structure type variables. However, you
can use the builtin Equals() method exposed by all structures to compare them, and you can also explicitly declare and implement operators for your own structure types. The syntax for doing this is covered in Chapter 22, “Operator Overloading.”

tip Use structures to implement simple concepts whose main feature is their value rather than the functionality that they provide.

Understanding Structure and Class Differences

You can’t declare a default constructor (a constructor with no parameters) for a structure.

The reason you can’t declare your own default constructor for a structure is that the compiler always generates one.

You can initialize fields to different values by providing a nondefault constructor. However,when you do this, your nondefault constructor must explicitly initialize all fields in your structure; the default initialization no longer occurs. If
you fail to do this, you’ll get a compile-time error.

In a class, you can initialize instance fields at their point of declaration. In a structure, you cannot.

There are other differences between classes and structures concerning inheritance. These differences are covered in Chapter 12, “Working with Inheritance.”

Declaring Structure Variables

Note As with enumerations, you can create a nullable version of a structure variable by using the ? modifier. You can then assign the null value to the variable:

Time? currentTime = null;

Understanding Structure Initialization

Time now = new Time();

However, because structures are value types, you can also create structure variables without calling a constructor

Note that in both cases, the Time variable is created on the stack.

If you’ve written your own structure constructor, you can also use that to initialize a structure variable.

Time now = new Time(12, 30);

Copying Structure Variables

You’re allowed to initialize or assign one structure variable to another structure variable, but only if the structure variable on the right side is completely initialized (that is, if all its fields are populated with valid data rather than undefined
values).

Date now = new Date();

Date copy = now;

When you copy a structure variable, each field on the left side is set directly from the corresponding field on the right side. This copying is done as a fast, single operation that copies the contents of the entire structure and that never throws
an exception.

Note C++ programmers should note that this copy behavior cannot be customized.

Structs and Compatibility with the Windows runtime on Windows 8

All C# applications execute by using the common language runtime (CLR) of the .NET Framework.  The CLR is responsible for providing a safe and secure environment for your application code in the form of a virtual machine。When you compile a C# application,
the compiler converts your C# code into a set of instructions using a pseudo-machine code called the Common Intermediate Language (CIL).These are the instructions that are stored in an assembly. When you run a C#

application, the CLR takes responsibility for converting the CIL instructions into real machine instructions that the processor on your computer can understand and execute. This whole environment is known as the managed execution environment, and C# programs
are frequentlyreferred to as managed code.

On Windows 7 and earlier, you can additionally write unmanaged applications, also known as native code, based on the Win32 APIs, which are the APIs that interface directly with the Windows operating system (the CLR also converts many of the functions
in the .NET Framework into Win32 API calls if you are running a managed application, although this process is totally transparent to your code). To do this, you can use a language such as C++. The .NET Framework enables you to integrate managed code into unmanaged
applications and vice versa through a set of interoperability technologies.

Windows 8 implements an alternative strategy, in the form of the Windows Runtime, or WinRT. WinRT provides a layer on top of the Win32 API (and other selected native Windows APIs) that is optimized for touch-based devices and user interfaces, such
as those found on Windows 8 tablets. When you build a native application on Windows 8, you use the APIs exposed by WinRT rather than Win32. Similarly, the CLR on Windows 8 also uses WinRT; all managed code written by using C# or any other managed language
is still executed by the CLR, but at run time the CLR converts your code into WinRT API calls rather than Win32. Between them, the CLR and WinRT are responsible for managing and running your code safely.

A primary purpose of WinRT is to simplify the interoperability between languages, enabling you to more easily integrate components developed by using different programming languages into a single seamless application. However, this simplicity comes
at a cost, and you have to be prepared to make a few compromises based on the different feature sets of the various languages available. In particular, for historical reasons, although C++ supports structures, it does not recognize member functions. In C#
terms, a member function is an instance method. So, if you are building C# structs that you want to package up in a library to make available to developers programming in C++ (or any other unmanaged language), these structs should not contain any instance
methods. The same restriction applies to static methods in

structs. If you want to include instance or static methods, you should convert your struct into a class. Additionally, structs cannot contain private fields, and all public fields must be C# primitive types, conforming value types, or strings.

WinRT also imposes some other restrictions on C# classes and structs if you want to make them available to native applications. Chapter 13, “Creating Interfaces and Defining Abstract Classes” provides more information.

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures,布布扣,bubuko.com

时间: 2024-10-04 04:52:58

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures的相关文章

《solidity学习笔记》chapter 2-solidity基础知识

映射 映射本质上是存储和查找数据所用的键-值对. mapping (uint => string) intToStr; 地址 以太坊区块链由 account (账户)组成,你可以把它想象成银行账户.一个帐户的余额是 以太 (在以太坊区块链上使用的币种),你可以和其他帐户之间支付和接受以太币,就像你的银行帐户可以电汇资金到其他银行帐户一样.每个帐户都有一个"地址",你可以把它想象成银行账号.这是账户唯一的标识符. mapping (address => uint )publi

《solidity学习笔记》chapter 3-solidity其他知识

Ownable contracts OpenZeppelin Solidity库里的合约之一,可以通过继承使用. /**  * @title Ownable  * @dev The Ownable contract has an owner address, and provides basic authorization control  * functions, this simplifies the implementation of "user permissions".  *

java系列:《java核心技术 卷1》学习笔记,chapter 11 调试技巧

11. 6 调试技巧 1)一个不太为人所知却非常有效的技巧是在每个类中放一个main方法,这样就可以对每个类进行单元测试.这个方法可以保留,因为在java虚拟机只调用启动类的main方法. 2)   日志代理,( logging proxy) ,通过一个子类对象,窃取父类的方法调用,并在其中进行日志记录,示例代码如下: Random random = new Random{ public double nextDouble(){ double result = nextDouble(); Log

机器学习学习笔记 PRML Chapter 2.0 : Prerequisite 2 -Singular Value Decomposition (SVD)

Chapter 2.0 : Prerequisite 2 -Singular Value Decomposition (SVD) Chapter 2.0 : Prerequisite 2 -Singular Value Decomposition (SVD) Christopher M. Bishop, PRML, Chapter 2 Probability Distributions 1. Vector Terminology Orthogonality Two vectors and are

JAVA学习笔记(五十六)- 泛型 Generic Types

泛型 Generic Types import java.util.ArrayList; import java.util.List; /* * 泛型 Generic Types * 集合泛型 * 类泛型 * 方法泛型 */ public class Test01 { public static void main(String[] args) { // 1.集合泛型,保证集合中元素的类型安全 List<Integer> nums = new ArrayList<Integer>(

RxJava 学习笔记(五) --- Creating 创建操作符

Create 使用一个函数从头创建一个Observable Just 将一个或多个对象转换成发射这个或这些对象的一个Observable From 将一个Iterable 一个Future 或者一个数组转换成一个Observable Defer 只有当订阅者订阅才创建Observable为每个订阅创建一个新的Observable Timer 创建一个在给定的延时之后发射单个数据的Observable Interval 创建一个按照给定的时间间隔发射整数序列的Observable Range 创建

《Spring3.X企业应用开发实战》学习笔记--SpringMVC

本篇是<Spring3.X企业应用开发实战>,陈雄华 林开雄著,电子工业出版社,2012.2出版"的学习笔记的第三篇,关于SpringMVC. Spring MVC 3.0和早期版本相比拥有了一个质的飞跃,全面支持REST风格的WEB编程.完全注解驱动.处理方法签名非常灵活.处理方法不依赖于Servlet API等. 由于Spring MVC框架在后头做了非常多的隐性工作,所以想深入掌握Spring MVC 3.0并非易事,本章我们在学习Spring MVC的各项功能时,还深入其内部

《Spring3.X企业应用开发实战》学习笔记--DAO和事务

本篇是"<Spring3.X企业应用开发实战>,陈雄华 林开雄著,电子工业出版社,2012.2出版"的学习笔记的第二篇,关于DAO和事务. 本篇从DAO操作,以及事务处理的基本知识谈起,介绍事务本身,以及Spring如何通过注解实现事务. DAO 近几年持久化技术领域异常喧嚣,各种框架如雨后春笋般地冒出,Sun也连接不断的颁布了几个持久化规范. Spring对多个持久化技术提供了持久化支持,包括Hibernate,iBatis,JDO,JPA,TopLink,另外,还通过S

【转】 Pro Android学习笔记(六七):HTTP服务(1):HTTP GET

目录(?)[-] HTTP GET小例子 简单小例子 出现异常NetworkOnMainThreadException 通过StrictMode进行处理 URL带键值对 Andriod应用可利用service提供很多功能,例如利用Google Maps API,现在我们将聚焦在HTTP serice中. Android SDK提供HttpClient,和J2EE中的接口非常相似.最常用的就是HTTP GET和HTTP POST.相关内容也可以阅读Android学习笔记(四五):互联网通信-Htt