473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C#: method with ref-to-object argument declared

Guys, I 've met some strange behaviour of the C# compiler. To reproduce it
please try to compile the code below and you 'll be really amazed. The
problem is that you can not use an interface pointer variable as a
ref-to-object parameter.

interface IMy
{
void f();
}

public class My : IMy
{
public void f()
{
}

public static void Main(string[] args)
{
IMy val = null;
System.Threading.Interlocked.CompareExchange(ref val, new My(), null);
}
}
Nov 16 '05 #1
2 1725
Alexander,

It's not surprizing at all. ref arguments must match exactly,
otherwise, you could do this:

void DoSomething(ref object o)
{
o = 1;
}

// Call do something with a string.
string s = "Hello";

// Make the call.
DoSomething(ref s);

Allowing this kind of behavior would be very, very bad.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexander Fedin" <Alexander Fe***@discussions.microsoft.com> wrote in
message news:BF**********************************@microsof t.com...
Guys, I 've met some strange behaviour of the C# compiler. To reproduce it
please try to compile the code below and you 'll be really amazed. The
problem is that you can not use an interface pointer variable as a
ref-to-object parameter.

interface IMy
{
void f();
}

public class My : IMy
{
public void f()
{
}

public static void Main(string[] args)
{
IMy val = null;
System.Threading.Interlocked.CompareExchange(ref val, new My(), null);
}
}

Nov 16 '05 #2
<=?Utf-8?B?QWxleGFuZGVyIEZlZGlu?= <Alexander
Fe***@discussions.microsoft.com>> wrote:
Guys, I 've met some strange behaviour of the C# compiler. To reproduce it
please try to compile the code below and you 'll be really amazed. The
problem is that you can not use an interface pointer variable as a
ref-to-object parameter.


I won't be amazed at all, having read the spec, which states:

(Section 14.4.2.1, ECMA spec)

<quote>
A function member is said to be an applicable function member with
respect to an argument list A when all of the following are true:

* The number of arguments in A is identical to the number of
parameters in the function member declaration.
* For each argument in A, the parameter passing mode of the
argument (i.e., value, ref, or out) is identical to the parameter
passing mode of the corresponding parameter, and
o for a value parameter or a parameter array, an implicit
conversion (§13.1) exists from the type of the argument to the type of
the corresponding parameter, or
o for a ref or out parameter, the type of the argument is
identical to the type of the corresponding parameter.
</quote>

Note the last part.

This shouldn't be surprising at all though. Consider the following
code, based on yours:

interface IMy
{
void f();
}

public class My : IMy
{
public void f()
{
}

static void DoSomething (ref object o)
{
o = "hello";
}

public static void Main(string[] args)
{
IMy val = null;
DoSomething(ref val);
}
}

If this were to compile, it would be horrible - either a runtime
exception would have to occur, or you'd end up with val being a
reference to something which *isn't* an IMy.

Note that this has nothing to do with interfaces per se - the same
would have been true if you'd not had an IMy interface at all, and just
used My val = null; instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: VM | last post by:
In my Win app, I have an event that calls method myMethod.Process() which returns a struct. This method Process() is composed of several other methods: openDB(), checkDB, expireDB(), etc... and...
8
by: news.chi.sbcglobal.net | last post by:
Hi. I am a little confused about the difference between a "value class" and a "ref class". I have the following code sample that shows the definition of a value class and of a ref class and...
3
by: mortb | last post by:
Why won't the third messagebox show in the second example, but not the first? cheers, mortb /////////////////////////////////////////////////////// /// /// Example 1 ///
12
by: rsdev | last post by:
Hi, I am using FindControl to access dynamically loaded user controls. I'm sure I've got it all wrong as I'm new to .NET. Currently if I want to access a button or view mode in a user control...
14
by: Mathijs | last post by:
Hi group, I have a class (TheClass) with static methods from an external DDL. private void Test(ref byte param) { byte a = 0, b = 0; param = 1; TheClass.CallToDLLFunction(ref a, ref b);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.