struct

I l @ ve RuBoard

struct

Early on in this section, there was a discussion of value types and reference types. In C#, a struct is considered a value type and, as such, is managed on the stack rather than the heap. C# implements primitive types such as int and char as struct s.

struct s are best used for simple data types or mementos for object serialization and object state. struct s can contain data members , methods , properties, and constructors. In most ways, a struct is similar to a class. However, a struct cannot inherit from a class or another struct , but can implement one or more interfaces. struct s cannot contain constructors without parameters, and the compiler will issue an error if you attempt to define one.

The default protection level for struct members is private. In C++, the only real difference between a struct and a class was the default protection level. Not so in C#. It is important to know that in C#, struct s and classes are not interchangeable. Remember that a struct is considered a value type and a class is considered a reference type. Listing 2.1.20 implements a simple struct to represent a Point .

Listing 2.1.20 Declaring and Using a struct
 1: using System;  2:  3: public struct Point {  4:     public int x;  5:     public int y;  6: }  7:  8: public class StructTest {  9: 10:     public static void Main( ) { 11:         Point p; 12:         p.x = 5; 13:         p.y = 10; 14:         Point p2 = p; 15:         PrintPoint( p ); 16:         PrintPoint( p2 ); 17:     } 18: 19:     public static void PrintPoint( Point p ) { 20:         Console.WriteLine( "x = {0} , y = {1} ", p.x, p.y ); 21:     } 22: } 

Listing 2.1.20 demonstrates how to define and use a C# struct . The struct Point contains two public data members ” x and y . Notice that on line 14 the variable p2 is assigned the value of variable p . When dealing with value types, the default assignment operator will copy the contents of the right side to the variable on the left side. This is in sharp contrast to the way in which reference types work. Remember that when dealing with reference types, the assignment operator will act as a reference and not a copy, as with value types.

There are some interesting points to note when dealing with struct s. A struct cannot be used until all the values within the struct have been initialized . Every struct has a synthesized default constructor that initializes the data members to their respective default values. However, when a struct contains a private data member, the default constructor will not initialize it without an explicit invocation of the default constructor. To accomplish this, the struct must be created using the new operator. This is a bit confusing because the struct is still created on the stack and not on the heap, as would be expected by the use of the new operator. Listing 2.1.21 demonstrates the warning issued by the compiler when attempting to use a struct that contains un-initialized members.

Listing 2.1.21 struct Member Initialization
 1: using System;  2:  3: public struct Simple {  4:  5:     public  int i;  6:     private string s;  7:  8:     public void init( ) {  9:         i = 10; 10:         s = "Hello"; 11:     } 12: 13: } 14: 15: 16: 17: public class T { 18: 19:     public static void Main( ) { 20: 21:         Simple simple; 22: 23:         simple.init( ); 24: 25:     } 26: } 

When Listing 2.1.21 is compiled, the C# compiler will issue the following error.

 Microsoft (R) Visual C# Compiler Version 7.00.9030 [CLR version 1.00.2204.21] Copyright (C) Microsoft Corp 2000. All rights reserved. struct_02.cs(25,3): error CS0165: Use of unassigned local variable 'simple' 

The error issued by the C# compiler is somewhat misleading at first because it complains about the use of an unassigned local variable simple. This error is due to the fact that the private data member s is un-initialized. To have private data members initialized, an instance of the struct must be declared using the new operator (see Listing 2.1.22).

Listing 2.1.22 Revision of Listing 2.1.21
 1: using System;  2:  3: public struct Simple {  4:  5:     public  int i;  6:     private string s;  7:  8:     public void init( ) {  9:         i = 10; 10:         s = "Hello"; 11:     } 12: 13:     public void show( ) { 14: 15:         Console.WriteLine("i = {0} ", i); 16:         Console.WriteLine("s = {0} ", s); 17:     } 18: 19: } 20: 21: public class T { 22: 23:     public static void Main( ) { 24: 25:         Simple simple = new Simple( ); 26: 27:         simple.init( ); 28: 29:     } 30: } 

Revising the previous listing and making two changes now allows for a clean compile and the expected behavior. The first change involves the addition of the show method, which makes use of the data member s . This change satisfies the compilers complaint about the unused private data member.

Notice that the declaration on line 25 now makes use of the new operator. Again, it is important to understand that a struct is a value type and, as such, will still exist on the stack. The usage of the new operator forces the invocation of the default constructor and initializes the private data members.

Because C# provides a synthesized parameter less constructor, any attempt to implement such a construct for a value type will cause the compiler to issue an error.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net