O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

26.1. First look at structs

You will be surprised how much structs appear to be like classes at first sight.

A struct is declared in the code below to represent a person's name. This is done using the struct keyword.

 1: using System;
 2:
 3: struct Name {
 4:   private string FirstName;
 5:   private string LastName;
 6:
 7:   public string GetName(){
 8:     return FirstName + " " + LastName;
 9:   }
10:
11:   // accessors and mutators
12:   public string GetFirstName(){
13:     return FirstName;
14:   }
15:   public void SetFirstName(string newFirstName){
16:     FirstName = newFirstName;
17:   }
18:   public string GetLastName(){
19:     return LastName;
20:   }
21:   public void SetLastName(string newLastName){
22:     LastName = newLastName;
23:   }
24: }

That's the Name struct. Now you can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required