Access the full text.
Sign up today, get DeepDyve free for 14 days.
CHAPTER 9 ■ ■ ■ You’ll use classes to implement most objects. Sometimes, however, it may be desirable to create an object that behaves like one of the built-in types—one that’s cheap and fast to allocate and doesn’t have the overhead of references. In that case, you can use a value type by declaring a struct in C#. Structs act similarly to classes but with a few added restrictions. They can’t inherit from any other type (though they implicitly inherit from object), and other classes can’t inherit from them. A Point Struct In a graphics system, a value class could encapsulate a point. Here’s how you’d declare it: using System; struct Point public Point(int x, int y) this.x = x; this.y = y; public override string ToString() return(String.Format("({0}, {1})", x, y)); public int x; public int y; 1. Technically, structs are derived from System.ValueType, but that’s only an implementation detail. From a language perspective, they act as if they’re derived from System.Object. 79 80 CH APTER 9 ■ STRUCTS (VALUE TYP E S) class Test public static void Main() Point start = new Point(5, 5); Console.WriteLine("Start: {0}", start); The x and y components of the Point can be accessed.
Published: Jan 1, 2005
Keywords: Class Instance; Reference Type; Member Function; Static Void; Reference Semantic
Read and print from thousands of top scholarly journals.
Already have an account? Log in
Bookmark this article. You can see your Bookmarks on your DeepDyve Library.
To save an article, log in first, or sign up for a DeepDyve account if you don’t already have one.
Copy and paste the desired citation format or use the link below to download a file formatted for EndNote
Access the full text.
Sign up today, get DeepDyve free for 14 days.
All DeepDyve websites use cookies to improve your online experience. They were placed on your computer when you launched this website. You can change your cookie settings through your browser.