Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Structure (Struct)

C# Structure (Struct)

In C#, a struct (structure) is a value type used to organize and store related data.

In C#, a structure is a value-type data structure that allows a single variable to store related data of various types.

The struct keyword is used to create structures.

A structure is used to represent a record. Suppose you want to track the movement of books in a library; you might want to track the following properties of each book:

Defining Structs

To define a structure, you must use the struct statement.

The struct statement defines a new data type with multiple members for the program.

For example, you can declare the Book structure as follows:


struct Books
{
   public string title;
   public string author;
   public string subject;
   public int book_id;
};  

The following program demonstrates the use of structures:

Example

using

System

;

using

System.Text

;

     

struct

Books

{

   

public

string

title

;

   

public

string

author

;

   

public

string

subject

;

   

public

int

book_id

;

}

;

 

public

class

testStructure

{

   

public

static

void

Main

(

string

[

]

args

)

   

{

      Books Book1

;

       

/* Declare Book1 of type Books */

      Books Book2

;

       

/* Declare Book2 of type Books */

     

/* book 1 detail */

      Book1

.

title

=

"C Programming"

;

      Book1

.

author

=

"Nuha Ali"

;

      Book1

.

subject

=

"C Programming Tutorial"

;

      Book1

.

book_id

=

6495407

;

     

/* book 2 detail */

      Book2

.

title

=

"Telecom Billing"

;

      Book2

.

author

=

"Zara Ali"

;

      Book2

.

subject

=

 

"Telecom Billing Tutorial"

;

      Book2

.

book_id

=

6495700

;

     

/* print Book1 information */

      Console

.

WriteLine

(

"Book 1 title : {0}"

, Book1

.

title

)

;

      Console

.

WriteLine

(

"Book 1 author : {0}"

, Book1

.

author

)

;

      Console

.

WriteLine

(

"Book 1 subject : {0}"

, Book1

.

subject

)

;

      Console

.

WriteLine

(

"Book 1 book_id :{0}"

, Book1

.

book_id

)

;

     

/* print Book2 information */

      Console

.

WriteLine

(

"Book 2 title : {0}"

, Book2

.

title

)

;

      Console

.

WriteLine

(

"Book 2 author : {0}"

, Book2

.

author

)

;

      Console

.

WriteLine

(

"Book 2 subject : {0}"

, Book2

.

subject

)

;

      Console

.

WriteLine

(

"Book 2 book_id : {0}"

, Book2

.

book_id

)

;

     

      Console

.

ReadKey

(

)

;

   

}

}

When the code above is compiled and executed, it produces the following result:


Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Features of C# structures

Structures provide a lightweight data type suitable for representing simple data structures, with good performance characteristics and value semantics:

Class vs. structure

Classes and structures have different considerations in design and use. Classes are suitable for representing complex objects and behaviors, supporting inheritance and polymorphism, while structures are more suitable for representing lightweight data and value types, to improve performance and avoid the overhead of reference management.

Classes and structures have the following fundamental differences:

Value type vs. reference type:

Inheritance and polymorphism:

Default constructor:

Assignment behavior:

** Passing method:**

Nullability:

Performance and memory allocation:

In the following example, MyStruct is a structure and MyClass is a class.

The comments demonstrate that a structure cannot contain a parameterless constructor, cannot inherit, and that copying a structure instance copies the entire contents of the structure. In contrast, a class can contain a parameterless constructor, can inherit, and copying an instance copies the reference.

Example

using

System

;

// structure declaration

struct

MyStruct

{

   

public

int

X

;

   

public

int

Y

;

   

// a structure cannot have a parameterless constructor

   

// public MyStruct()

   

// {

   

// }

   

// a parameterized constructor

   

public

MyStruct

(

int

x,

int

y

)

   

{

        X

=

x

;

        Y

=

y

;

   

}

   

// a structure cannot inherit

   

// struct MyDerivedStruct : MyBaseStruct

   

// {

   

// }

}

// class declaration

class

MyClass

{

   

public

int

X

;

   

public

int

Y

;

   

// a class can have a parameterless constructor

   

public

MyClass

(

)

   

{

   

}

   

// a parameterized constructor

   

public

MyClass

(

int

x,

int

y

)

   

{

        X

=

x

;

        Y

=

y

;

   

}

   

// a class supports inheritance

   

// class MyDerivedClass : MyBaseClass

   

// {

   

// }

}

class

Program

{

   

static

void

Main

(

)

   

{

       

// a structure is a value type, allocated on the stack

        MyStruct structInstance1

=

new

MyStruct

(

1

,

2

)

;

        MyStruct structInstance2

=

structInstance1

;

// copy the entire structure

       

// a class is a reference type, allocated on the heap

        MyClass classInstance1

=

new

MyClass

(

3

,

4

)

;

        MyClass classInstance2

=

classInstance1

;

// copy the reference, pointing to the same object

       

// modifying a structure instance does not affect other instances

        structInstance1

.

X

=

5

;

        Console

.

WriteLine

(

$

"Struct: {structInstance1.X}, {structInstance2.X}"

)

;

       

// modifying a class instance affects other instances

        classInstance1

.

X

=

6

;

        Console

.

WriteLine

(

$

"Class: {classInstance1.X}, {classInstance2.X}"

)

;

   

}

}

Let's rewrite the previous example based on the above discussion:

Example

using

System

;

using

System.Text

;

     

struct

Books

{

   

private

string

title

;

   

private

string

author

;

   

private

string

subject

;

   

private

int

book_id

;

   

public

void

setValues

(

string

t,

string

a,

string

s,

int

id

)

   

{

      title

=

t

;

      author

=

a

;

      subject

=

s

;

      book_id

=

id

;

   

}

   

public

void

display

(

)

   

{

      Console

.

WriteLine

(

"Title : {0}"

, title

)

;

      Console

.

WriteLine

(

"Author : {0}"

, author

)

;

      Console

.

WriteLine

(

"Subject : {0}"

, subject

)

;

      Console

.

WriteLine

(

"Book_id :{0}"

, book_id

)

;

   

}

}

;

 

public

class

testStructure

{

   

public

static

void

Main

(

string

[

]

args

)

   

{

      Books Book1

=

new

Books

(

)

;

/* Declare Book1 of type Books */

      Books Book2

=

new

Books

(

)

;

/* Declare Book2 of type Books */

     

/* book 1 detail */

      Book1

.

setValues

(

"C Programming"

,

     

"Nuha Ali"

,

"C Programming Tutorial"

,

6495407

)

;

     

/* book 2 detail */

      Book2

.

setValues

(

"Telecom Billing"

,

     

"Zara Ali"

,

"Telecom Billing Tutorial"

,

6495700

)

;

     

/* print Book1 information */

      Book1

.

display

(

)

;

     

/* print Book2 information */

      Book2

.

display

(

)

;

      Console

.

ReadKey

(

)

;

   

}

}

When the code above is compiled and executed, it produces the following result:


Title : C Programming
Author : Nuha Ali
Subject : C Programming Tutorial
Book_id : 6495407
Title : Telecom Billing
Author : Zara Ali
Subject : Telecom Billing Tutorial
Book_id : 6495700