|
Just curiosity , Did anyone have idea of how many methods a single C# class can allow ?
I heard, it is compiler dependent If true, what is the maximum no allowed by the standard compiler ?
Thanks,
Vythees
|
|
|
|
|
vytheeswaran wrote: how many methods a single C# class can allow ?
We can have as we can.
For More Details:
MSDN[^]
Regards,
Satips.
|
|
|
|
|
Where does that article answer the OP's question? Or did you just chuck some keywords in to Google and hit "I'm feeling lucky"?
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
Thanks for Info,
But I read in one article that E-Bay once hits the compiler limit in max number of methods in single class.
Thanks,
Vythees
|
|
|
|
|
IIRC ebay is running its servers using c++ and ISAPI, not .net.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
I agree, I think then there should be the same constraint exist in .NET class also.
Thanks,
Vythees
|
|
|
|
|
Even assuming there is a constraint, you're talking about 2 totally different languages so why should they be the same?
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
vytheeswaran wrote: Did anyone have idea of how many methods a single C# class can allow ?
I think that if you ever reached that limit then you might want to seriously reconsider your design.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
Colin Angus Mackay wrote: I think that if you ever reached that limit then you might want to seriously reconsider your design.
or the switch/case limit or the if/elseif limit or the ....
or if none of that makes sense to you try this[^]
|
|
|
|
|
Oh God, thats classic link.
Thanks,
Vythees
|
|
|
|
|
|
Seeing Assembly metatokens takes the following form: 00 000000, where the former is the type, and the latter is the code, I would say the maximum number of methoddef's in an assembly would be limited to 24-bits, iow 16.7 million. Dunno if there is a limitation on a classes though.
|
|
|
|
|
A nice research.
Thanks,
Vythees
|
|
|
|
|
Not research, I know it from working with the spec And those values can be accessed from .NET 2 (nowadays).
|
|
|
|
|
There is a limit, I remember someone a while ago (I've got a feeling in this forum) hit a limit on the number of fields in anycase that they could include in a class. Buggered if I can remember where it was or what it was
|
|
|
|
|
Maybe you are thinking about the parameter limit, that is 16383/4.
|
|
|
|
|
No, I'm pretty sure that someone managed to hit a limit on the number of fields that could be contained in a class. I'll do some digging.
|
|
|
|
|
when in trouble, switch to Win64.
|
|
|
|
|
Fiction :
I may end up with Portablity issues with 32bit cousins.
Not an real issue, but just to know the number.
Thanks,
Vythees
|
|
|
|
|
 I let this run for an hour to get to 5000 before I gave up. Someone with more CPU and physical RAM than I have should run it and see where it ends...
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
namespace MethodCountLimitFinder {
class Program {
static void Main(string[] args) {
Int32 methodCount = 1;
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler icc = cscp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults cr = null;
string pre = "using System;" + Environment.NewLine +
Environment.NewLine +
"namespace Tester {" + Environment.NewLine +
" class Test {" + Environment.NewLine;
string post = " }" + Environment.NewLine +
"}";
string inner = string.Empty;
while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine;
cr = icc.CompileAssemblyFromSource(cp, pre + inner + post);
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
System.Console.WriteLine(methodCount);
}
}
}
Share and enjoy
Sean
|
|
|
|
|
Sean Michael Murphy wrote: while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine;
cr = icc.CompileAssemblyFromSource(cp, pre + inner + post);
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
Sean Michael Murphy wrote: Someone with more CPU and physical RAM than I have should run it and see where it ends...
No wonder, always use StringBuilder for string concatenation in a loop.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Cannot been said to often!
Good answere!
|
|
|
|
|
Well it can be said too often, but it's appropriate here.
|
|
|
|
|
dnh wrote: No wonder, always use StringBuilder for string concatenation in a loop.
Hmmm. Interesting. When I originally undertook to code this snippet to try to figure an answer to this guys question, optimization was pretty far from my mind. I mean, I cranked the original bit of code out in 15 minutes (or so) and had originally coded it so the methods would be recreated every time. I took another 5 minutes and optimized it so that only 1 method (the new one) would have to be concatenated to the "guts", which was then stuck in between the fixed "header" and "footer" of the class.
It ran slowly, but I assumed that most of the overhead was in the actual code compilation (compiling classes of 15000 lines), and not a little bit of string concatenation. So I've re-written it using StringBuilder and timed both versions for 500 iterations. The original code did 500 iterations on my PC in 161.5222 seconds. This version:
StringBuilder inner = new StringBuilder();
DateTime startTime = DateTime.Now;
for (Int32 i = 0; i < 500; i++) {
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine);
StringBuilder code = new StringBuilder(pre);
code.Append(inner);
code.Append(post);
cr = icc.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
TimeSpan ts = DateTime.Now - startTime;
System.Console.WriteLine(ts.TotalSeconds); did it in 160.111. Much less that 1% slower.
Not a string concatenation to be found, except for the line joins.
Anything to add?
Thanks.
Sean
|
|
|
|
|
Sean Michael Murphy wrote: Anything to add?
I'd agree that most time takes compilation, but the thing about string concatenation with + is that it's -unlike compilation - completely unnecessary. And I don't think that using StringBuilder for concatenating strings in big loops is optimalization - I think it's something you should do without thinking.
btw you're still allocating 7 or so strings in
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine); every cycle, that's 3500 unnecessary allocations
Anyway, cool way to check for number of methods limit indeed.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|