User defined conversion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _07_06_2017_d
    class Person
        public string Name;
        public int Age;

        public Person(string n, int a)
            Name = n;
            Age = a;

        public static implicit operator int(Person p)
            return p.Age;

        public static implicit operator Person(int k)
            return new Person("Anonimus", k);
    class Program
        static void Main(string[] args)
            Person p1 = new Person("Ahmet", 2);
            int z = p1;     //int z=int(Person p1)

            int m = 35;

            Person p2 = m;   //Person p2= Person(35)

            Console.WriteLine(p2.Name + " " + p2.Age);

No comments yet»

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: