Put logic to property setter : Properties : Class C# Examples


C# Examples » Class » Properties »

 

Put logic to property setter









    
using  System;
using  System.Collections.Generic;
using  System.Text;

class  MainClass
{
        static  void  Main(string[]  args)
        {
                Person  person  =  new  Person();

                person.SetName("A",  "B");

                Console.WriteLine("The  person's  full  name  is  {0}",person.FullName);
                person.FullName  =  "A  b  c";
                Console.WriteLine("The  person's  full  name  is  {0}",person.FullName);
        }
}
class  Person
{
        private  string  _lastname;
        private  string  _firstname;

        public  void  SetName(string  lastname,  string  firstname)
        {
                _lastname  =  lastname;
                _firstname  =  firstname;
        }
        public  string  FullName
        {
                get
                {
                        return  _firstname  +  "  "  +  _lastname;
                }
                set
                {
                        string[]  names  =  value.Split(new  string[]  {  "  "  },StringSplitOptions.RemoveEmptyEntries);
                        _firstname  =  names[0];
                        _lastname  =  names[names.Length  -  1];

                }
        }
}
    
   
  
   



Output

The person's full name is B A
The person's full name is A c


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Properties