Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ref argument in C#

Tags:

c#

.net

First of all I am new to C# programming.

I read the parameter of

ImageObj.getBounds(ref GraphicsUnit unit);

When I tried this,

ImageObj.getBounds(ref GraphicsUnit.Pixel);

I still got the error. But this seemed to work perfectly fine.

GraphicsUnit u = GraphicsUnit.Pixel;
ImageObj.getBounds(ref u);

What is the difference between the two and how is the first wrong? Thank You.

like image 304
mihsathe Avatar asked Nov 30 '22 04:11

mihsathe


1 Answers

GraphicsUnit.Pixel is a property, you can not pass properties with ref/out parameters in C#. It's because ref/out is like pointer to pointer in other languages but property is not a variable - it's a 2 methods: getter and setter, so, you can't pass a pointer to pointer to value because you haven't value itself.

Added: Ok, GraphicsUnit.Pixel actually is an enum member - you also can't pass it with ref/out parameters because it's a constant.

like image 136
oxilumin Avatar answered Dec 16 '22 04:12

oxilumin