Get 20M+ Full-Text Papers For Less Than $1.50/day. Start a 14-Day Trial for You or Your Team.

Learn More →

A Programmer’s Introduction to C# 2.0Structs (Value Types)

A Programmer’s Introduction to C# 2.0: Structs (Value Types) 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. http://www.deepdyve.com/assets/images/DeepDyve-Logo-lg.png

A Programmer’s Introduction to C# 2.0Structs (Value Types)

Editors: Gunnerson, Eric; Wienholt, Nick

Loading next page...
 
/lp/springer-journals/a-programmer-s-introduction-to-c-2-0-structs-value-types-wk5JZFrgIq

References (0)

References for this paper are not available at this time. We will be adding them shortly, thank you for your patience.

Publisher
Apress
Copyright
© Apress 2005
ISBN
978-1-59059-501-5
Pages
79 –83
DOI
10.1007/978-1-4302-0035-2_9
Publisher site
See Chapter on Publisher Site

Abstract

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

There are no references for this article.