1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Lesson 12: Structs pdf

3 140 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 3
Dung lượng 34,7 KB

Nội dung

Using Excellent Tools to Write Web Applications Targeting the .NET Common Language Runtime (CLR) [Home] [Up] [Lesson01] [Lesson02] [Lesson03] [Lesson04] [ Lesson05 [Lesson06] [Lesson07] [Lesson08] [Lesson09] [Lesson10] [ Lesson11 [Lesson12] [Lesson13] On sale Now! C# Unleashed is an in- depth guide for intermediate to advanced software developers to learn the C# programming language and serve as a desktop reference. . . . . . . . . . . The C# Station Tutorial by Joe Mayo, 01/19/02 Lesson 12: Structs This lesson teaches C# Structs. Our objectives are as follows: Understand the Purpose of structs. Implement a struct. Use a struct. A struct allows you to create new value-type objects that are similar to the built-in types (int, float, bool, etc.). When would you use a struct instead of a class? Think about how the built-in types are used. They have values and distinct operations to manipulate those values. If you have a need to create an object that behaves in this manner, consider implementing it as a struct . Later in this article, I'll explain a couple rules for using structs , which will give you a better idea of when to use them. In the meantime, here's an example. Listing 12-1. Example of a struct: StructExample.cs using System; struct Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } public Point Add(Point pt) { Page 1 of 3 Tutorial 6/24/2002 http://www.csharp - station.com/Tutorials/Lesson12.aspx Point newPt; newPt.x = x + pt.x; newPt.y = y + pt.y; return newPt; } } /// <summary> /// Example of declaring and using a struct /// </summary> class StructExample { static void Main(string[] args) { Point pt1 = new Point(1, 1); Point pt2 = new Point(2, 2); Point pt3; pt3 = pt1.Add(pt2); Console.WriteLine("pt3: {0}:{1}", pt3.x, pt3.y); } } Listing 12-1 shows how to declare and use a struct . It's easy to tell that a type is a struct because of the keyword "struct " used in it's definition. The basic layout of a struct is much like a class, but with differences, which will be explained in following paragraphs The Point struct has a constructor which initializes it's fields to the x and y values passed in. It also has a method named Add(), which will accept another Point struct, add it to this struct, and return a new struct. Notice that there is a Point struct declared in the Add() method. It does not need to be instantiated with a new operator, like a class. When this occurs, the struct is implicitly instantiated with it's default (or parameterless) constructor. The parameterless constructor initializes all struct fields to default values. i.e. integrals are 0, floating points are 0.0, and booleans are false. It's illegal to define a parameterless constructor for a struct. Although not required, a struct may be instantiated with a new operator. In Listing 12-1 the pt1 and pt2 Point stru cts are initialized with values by using the constructor defined in the Point struct. A third Point struct, pt3 is declared and defaults to using the parameterless (default) constructor, because it doesn't matter what it's value is at this point. The Add() method of the pt1 struct is then invoked, passing the pt2 struct as a parameter. The result is assigned to pt3, showing that a struct may be used just like any other value type. Here's the output from Listing 12-1: pt3: 3:3 Page 2 of 3 Tutorial 6/24/2002 http://www.csharp - station.com/Tutorials/Lesson12.aspx Another difference between structs and classes is that structs can not have destructors. Also, structs cannot inherit another class or struct or be inherited from. However, a struct may inherit multiple interfaces. An interface is a C# reference type with members that do not have implementations. Any class or struct inheriting an interface must implement every one of that interface's methods. Interfaces are a subject for a later lesson. In summary, you now know how to create a struct. You can also instantiate and use structs . When deciding whether to implement a type as a struct or class , you should consider how the type will be used. If you need to define a parameterless constructor, then a class is your only choice. Also, consider that a struct incurs less overhead than a class because, being a value type, it is stored on the stack rather than how a class is stored, on the heap. Your feedback is very important and I appreciate any constructive contributions you have. Please feel free to contact me for any questions or comments you may have about this lesson. Feedback C# Station Copyright (c) 2002 C# Station jmayo@csharp- station.com Page 3 of 3 Tutorial 6/24/2002 http://www.csharp - station.com/Tutorials/Lesson12.aspx . [Home] [Up] [Lesson0 1] [Lesson0 2] [Lesson0 3] [Lesson0 4] [ Lesson0 5 [Lesson0 6] [Lesson0 7] [Lesson0 8] [Lesson0 9] [Lesson1 0] [ Lesson1 1 [Lesson1 2] [Lesson1 3]. Joe Mayo, 01/19/02 Lesson 12: Structs This lesson teaches C# Structs. Our objectives are as follows: Understand the Purpose of structs. Implement

Ngày đăng: 17/01/2014, 09:20

w