Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to declare and use conversions in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    struct conversion_struct
    {
        private int pn;

        public conversion_struct(int n)
        {
            this.pn = n;
        }
        static public implicit operator conversion_struct(int n)
        {
            return new conversion_struct(n);
        }
        static public explicit operator int(conversion_struct cs)
        {
            return cs.pn;
        }
        static public implicit operator string(conversion_struct cs)
        {
            return "value is: " + cs.pn;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                conversion_struct c_struct;

                c_struct = 30;

                Console.WriteLine((int)c_struct);

                Console.WriteLine(c_struct);

                short s = (short)c_struct;

                Console.WriteLine(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
    
30
value is: 30
30
  
*/

 



answered Sep 5, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    public class conversion_class
    {
        private int pn;

        public conversion_class(int n)
        {
            this.pn = n;
        }
        static public implicit operator conversion_class(int n)
        {
            return new conversion_class(n);
        }
        static public explicit operator int(conversion_class cs)
        {
            return cs.pn;
        }
        static public implicit operator string(conversion_class cs)
        {
            return "value is: " + cs.pn;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                conversion_class c_struct;

                c_struct = 30;

                Console.WriteLine((int)c_struct);

                Console.WriteLine(c_struct);

                short s = (short)c_struct;

                Console.WriteLine(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
     
30
value is: 30
30
   
*/

 



answered Sep 7, 2015 by avibootz

Related questions

1 answer 69 views
69 views asked Oct 31, 2024 by avibootz
1 answer 167 views
167 views asked Jan 19, 2017 by avibootz
1 answer 149 views
1 answer 135 views
1 answer 127 views
127 views asked Jan 9, 2017 by avibootz
1 answer 167 views
1 answer 126 views
...