Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Implicit and Explicit Operators in C#

For a side project I'm working on, explicit operators have come in handy in the data layer. With explicit operators, you can convert from one class to another using a user-defined conversion operator. For example:

    public class MyClass

    {

        private int _id;

        private string _description;

 

        public int ID

        {

            get { return _id; }

            set { _id = value ; }

        }

 

        public string Description

        {

            get { return _description; }

            set { _description = value ; }

        }

 

        public static MyClass GetMyClass( int id)

        {

            return ( MyClass ) AnotherClass .GetRows(id);

        }

 

        public static explicit operator MyClass ( DataTable dataTable)

        {

            MyClass myClass = new MyClass ();

            myClass.ID = Convert .ToInt32(dataTable.Rows[0][ "ID" ]);

            myClass.Description = Convert .ToString(dataTable.Rows[0][ "Description" ]);

            return myClass;

        }

    }

 

    public class AnotherClass

    {

        public static DataTable GetRows( int id)

        {

            DataTable dataTable = new DataTable ();

            //...

            return dataTable;

        }

    }

With implicit operators, you can implicitly convert from one class to another using a user-defined operator without casting. For example:

    public class MyClass

    {

        private int _value;

 

        public int Value

        {

            get { return _value; }

            set { _value = value ; }

        }

 

        public MyClass()

        {

        }

 

        public MyClass( int value)

        {

            _value = value;

        }

 

        public static void SomeMethod()

        {

            MyClass myClass = new MyClass (2);

            int id = myClass;

        }

 

        public static implicit operator int ( MyClass myClass)

        {

            return myClass.Value;

        }

    }

Pretty cool, huh?

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: May 24 2007, 03:37 PM by Ryan Olshan | with no comments
Filed under:

Comments

No Comments