Summarize some questions about structure in C #

Record a few abnormal questions that others have interviewed.

1. What should I pay attention to when defining a structure?

First look at the two test results:

Summary: The definition constructor that is not allowed to be displayed in the structure can be defined with parameters, but the fields defined in the structure need to be assigned in the function. In addition, please note that it is not allowed to directly assign values ​​to fields in the structure.

3. What type is the class in the structure, value type or reference type? The structure inherits the interface, is it a value type or a reference type?

 1 using System;
 2 
 3 namespace ConsoleApp1
 4 {
 5     interface IPeople { }
 6 
 7     class Job { }
 8 
 9     struct People : IPeople
10     {
11         public Job job;
12         public string name;
13         public int age;
14 
15         public People(int age)
16         {
17             this.job = new Job();
18             this .name = " Jim " ;
 19              this .age = age;
 20          }
 21      }
 22  
23      public  class Program
 24      {
 25          static  void Main ( string [] args)
 26          {
 27              People p = new People ( 12 );
 28              p .job = new Job ();
 29              Console.WriteLine ( " \ nThe structure inherits whether the interface value type: " +p.GetType (). IsValueType); 30              Console.WriteLine ( " \ nWhether
 the class in the structure is a value type: " + p.job.GetType (). IsValueType); 31              Console.WriteLine ( " \ nin the
 structure Whether the string value type: " + p.name.GetType (). IsValueType);
 32              Console.ReadLine ();
 33          }
 34      }
 35 }

 

Guess you like

Origin www.cnblogs.com/luguoshuai/p/12719534.html