BUG: You cannot create delegates on non-virtual methods of value classes (327421)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition

This article was previously published under Q327421

SYMPTOMS

When you try to create a delegate on a non-virtual method of a value class, you receive the following System.NotSupportedException error message in the common language runtime:
Unhandled Exception: System.NotSupportedException: Delegates on value classes can only be formed on virtual methods
at MyDel..ctor(Object object, IntPtr method)
at M.Main() in c:\test\class1.cs:line 27

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

Compile and run the following code to reproduce the problem:
using System;

public struct Pair
{
	string a; 
	int b;
	
	public Pair(string av, int bv) 
	{
		a = av; 
		b = bv; 
	}
	
	public string Fst() 
	{
		return a;
	}
	
	public int Snd() 
	{
		return b;
	}
}   

public delegate string MyDel();

class M
{
	public static void Main()
	{
		Pair p = new Pair("hello", 59);
		Console.WriteLine(p.Fst());
		MyDel d = new MyDel(p.Fst);
		Console.WriteLine(d());
	}
}
				

Modification Type:MajorLast Reviewed:8/31/2005
Keywords:kbvs2002sp1sweep kbbug KB327421