结构函数

结构类型不但可以用来存储数据元素,还可以用来包含函数。

举一个例子,

struct CustomerName
{
        public string firstName,lastName;
}

static void Main(string [] args)
{
        CustomerName myCustomer;
    
        myCustomer.firstName = "John";
        myCustomer.lastName = "Franklin"
        WriteLine($"{myCustomer.firstName} {myCustomer.lastName}");

}

这里显然有些繁琐。如果我们使用结构包含函数,语法就会简单很多。

struct CustomerName
{
        public string firstName,lastName;
        public string Name() =>firstName + " "+lastName;
}

static void Main(string [] args)
{
        CustomerName myCustomer;
    
        myCustomer.firstName = "John";
        myCustomer.lastName = "Franklin"
        WriteLine(myCustomer.Name());

}    
posted @ 2020-01-01 21:58  Attractor  阅读(311)  评论(0编辑  收藏  举报
                                                                                                                                                                                                        
                                                                                                                    :.Bs .Bs                                                                            
                                                                                                               :   BQgEbLE7                                                                             
                                                                                                           i .qQIEBQv.J2M2r K:                                                                          
                                                                                                         .L  r5ZgMPEjSY2UqPBQ1                                                                          
                                                                                                         PgZbuP2XPMIq51vSKZ2JrB:                                                                        
                                                                                                         BJBDRqgKPIbUqX1L5vr:DP                                                                         
                                                                                                         IsvEKQPddDgqvXU5ir7u7.                                                                         
                                                                                                       BPS7qZZPXLPEBRKYX5KYju                                                                           
                                                                                                       iQsL7DMqYX2gQbjJLQRUquY                                                                          
                                                                                                        PgSPPBRZ5ggdsS7UQJPU.Q                                                                          
                                                                                                        2qRPbXMXXKgIZYIDZL7.D                                                                           
                                                                                                       7.PXZKPSEIq2gsUPPrirPQ                                                                           
                                                                               QB        RB            PBvBQMIZbPuP5q5srrYb57                                                                           
                                                                             LQDZM       BPBi             XB5ZMMSPIEZqrXqE2                                                                             
                                                                            7Q55KQ      BbIvB7              jJP5PSDKXr5MP.                                                                              
                                                                            Qg5I2gr     RdIuSB               7DQEQdgPdI                                                                                 
                                                                            BPKsPYJ     BRQ1UMB             uMBEgPgPMZgv2Y5i:.                                                                          
                                                                           :RP1BP1XDgBQBDMPPUBi             :BZQbQdgPdPQMBQBQBQB5.                   .rj77iirYii...i:7Y5::                              
                                                                            ERBQDPBQBQgIDZQPB               DQBEMPgdgSZSPUPKMgBQBdg             iLqqPr7.   :svr:::uL7v5IPEBQP                           
                                                                             1MBgSqMZQPgPRP5U            vPQgBMBgBZQZMbgZRPdKPKqKDPBMRPEPdqb2XISLLii..   .  r1SQS7::         sr                         
                                                                              rSUZBPDPBXbKB:.vr         .QBRBDQgQMQRBDRZQdgdgdqQBJ2KgqqYuvJrvrrir.. .     .     ..i7LrL7i                               
                                                                                 vRBRBRBddQ.            QBRgDQZQZBZQgRDMdMEgPP   QQgsJsq2S7uvr.:..            ..       .iJ21.                           
                                                                                v5DMQDQQBMQ            KBDBEQgQDRDBDMDQZRPM5bM     :RgPvuj1JX7.              .ir:.7.:iYKQPBRBQi                         
                                                                                .BQQEgbRQBv            BRBMQgQZQDQdBZQZgPgKIEB        :dZBZbSbuL..     .ivrIUu::::.            K                        
                                                                                .QgSq5b5gQB            QBRBPQDQZBDMgQZRZgbPuBR            :ii:7rSUXvvir::                                               
                                                                               ..2EQdDIdqBR           QBDBQgDBDBgBZQgBdZbgI5Qi                                                                          
                                                                               PBQBERRBDBDb           BQBQBdQMQMBMQZBggqgPKuB                                                                           
                                                                               vQRgMSDDQRB2         .J5BDBQDMBMBgRDQgQDQEdJK5ri                                                                         
                                                                                .PgJQPPSQQR       BP:7PXQQBKggBRQEBZREBgE2uXdbBQ:     .             .           .:: : ....7   ..      . . :..           
   :      ..                                                         .           DQKqdXQMBL      iQQQXXMMBQg2QgQEBEBPZZZqI5d7qMEMi     ..:                   i.        ....:  .r   .:r   77Yi17. ur.rJrr
              :.      .               .       .     .           .:  ..i.r7:      XB5KvLJI1. :     ZdBIvZBQMJQZQgBDQZB2Q5XIBjsgB5Qq                .      ir : 7i7. ..   ..i :.r.    .:i:.   . ii. :i7r1r
                                                                 :              RBDQZgPE2SqPRBK    B5j:B57jSRBgBgQbMggbg2dqQ2BZPQi                        .    . : ..  ..rr  .:. : :.7i::r.:.: i7. :.:: 
                                                                              .EQqg5PKU2qJXdQqRgB7 .B:1Q2KBQQEREgPdXEZgPdbXIQgPP.                   :         .   .   .  .i .  .  ::rri.:..   ..ii.   : 
              .     ..           .                                          r:EBdEKSrUvUvJJPXP5PdBD75U.MQBbEgBRBgQgBZBgRqgUqJdqM       .   .    .7..   ...    i: ... . r   .::     ..:.i  ..   :..   .. 
  .         . ri7r:.                                    ..      .          UKQBgMPZPS:iirrEKdSPSbSgDgKSjSUEYr1gEBDgEQgBZgbPPgr.Ri       7QKI: ..  ..    i.:.  :i  qi.::.J.    :     :   :.ii: .:7.rr..v 
:  .         :   ...   r.                           .     .               :QRgPZPEKggZr::XSdqP5PSdSEKZPPSS7KuqQBEdjZdRPQEgPdQ  MKr..     UQqJUrJ.i.    .  .v.. :. .i .   Yr. . i ... :     :ir.  ..v.r. 
:   .:       :i.        :   .     :..   . :..:i... .r7:   7:             7BQQZgbXKEdQRMYPPP2PUP2qUbXPKgKE5PJg5EQBQBMQdgPdPQUsEddP1BZMPbv  qvEdPL:   .  .ri .Pb: rr. .ii  .: .   : .::  :1. .Yuj ..7i. r 
    :i..     . .        .i .:.     25ZPJ:Ur5iLYL iJ7:7ivj: 7 X..        rMJ2KQZRPPIgdDbDqgXEqd2P2KJq2P5P2dIUIg1SIQQBQBgQqdbQbBQBKIUE2BQBQguIK2IBYi sv:   .  :s     2L27  .   ::i::  iv:irv:i  :: ::::  .
r:r.::PPK7XYsvu7Jii:r:.   :rL77ii. .RQEU    7vYX.i7rI: .iJRbiiI1:      QBUvIDqQEZMQDgXP2dKdPRKEKZ5XuX2P5SUI7I2XsX1PPMDBRBQBdEPZUX2Ss2RBgQggPb5IPPu22PE27  .     Xi .EvJviJ7 .:v:..7L..i   :r.   :   :   
QK7D:  : :7bEgEUvX:. r.     r7:ri    L  : rr     ::.:RI  LrJ.:rv.UXPi: .rBQDbi75ubqEdQqPIPXZEBDRPESb2P5P5UL1jP2SjPKPIPKDKgdPSPXduPSgggQBQBdb5QEZYdqdvZ1BQYiD.  i:U irI.ir:SX:Ir: ..1r7 u. :i.r::.irYs:  
Dr.vqUPYr7YiUiX:  . .   r.  .L..i :7... Y..i  .7X.  ::Dgu     Y  ..      gB5irvUqSdSMQBgDqE5gIKPZPXddKZXDu1r25EI5jXIdIXUPUq5ILXUdZBPQPQRBQBgQgBQdrKSEKIUZMQPB. j7527SUq.iXQPYPZsY  rPsIrPrr...rL57i:Liir
  u5.ii.   2 ....  12r.Yrir: L   .   ir  q 7uL  :J .:  UR: :7rD7    i:.j  qK MQBgZIZ12qBQBgDKu.gZPrsPEXb5dUU75IP1Xu1v2sXuqIdqK7qdZSEKDdBDQgBRBRBM57EXZbZvXSQQM222XQBKPSPsdgBbPXQQg:vrs7u7EP5.7:7vK7::i:i
   7Y:i.j: iLr.:7i.i1b5Lq.    d.      :  JPriiLi:  J5:    ir2sURM.:2BKZ5i .i7 QQBQBQQY7:YUgMBP2:EQSiIIqSK2Kj5LUs2YPUq5bUP55IBQZPgKq2DDMSDZQgRgBdQULLKuEPP12IBQMXLUQQMPg2bUS1BDK1PQBs5XIs27SQPr7:irS2q1L.
.   J5EL:vrvii . : .  .vSQ: .  q      r: rP.  ...i..:isMr    ..UY7.  :.rir.::  .rsKBPrLdL:.vsEPP.jUJ7JrjYX51PbYIuPIPSK1u5g7:rJuI5DPdSXvKJu72UPPQQBZJJqs12PUJ7KsPq7vUubPqjK1IvdXP2v1BXq5XXXuPDRu1Lv:r:vJj