[Mono-dev] Mono's handling of Structs

Bill Six billsix316 at yahoo.com
Tue Nov 22 23:40:48 EST 2005


Hi,

I'm new to C#, but it was my understanding that a
struct does not need a call to the constructor before
its use, in the same way that all other primitives
(ints, floats, etc) don't.

MCS and GMCS seem to require that a constructor is
called before the struct can be used.  For instance,
the code at the bottom of this email won't compile, as
the error says "Use of unassigned local variable p2"

If instead of
                Position p2;
you use 
                Position p2 = new Postion();
it will compile fine.


Is this a bug in the mono compiler?  Should it
automatically know that a declaration of a struct
implies calling the default constructor?  If so I will
spend time looking through the Mono compiler to fix
it, as I've wanted to look through the code anyways.


Test.cs..........................................



using System;
 
public struct Position
{
        public Position(int x, int y)
        {
                this.myx = x;
                this.myy = y;
        } 

        public override string ToString()
        {
                return String.Format("{0} {1}", myx,
myy);
        }
 

        public int x {
                get{
                         return myx;
                }
                set{
                        this.myx = value;
                }
        }

        public int y {
                get{
                         return myy;
                }
                set{
                        this.myy = value;
                }
        }

        private int myx ;
        private int myy;
}

public class Test
{
        public static void Main(string[] args)
        {
                Position p = new Position(1,2);
                Console.WriteLine(p);


                // Mono compiler gives an error here
                Position p2;
                p2.x = 5;
                p2.y = 7;
                Console.WriteLine(p2);

        }

}

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Mono-devel-list mailing list