[ACCEPTED]-is it possible to do inline function in vb.net?-vb.net

Accepted answer
Score: 14

The answers I've seen assume you're talking 22 about compile or JIT-time inlining - and 21 they're entirely correct. However, the other 20 use of the word "inline" that I've heard 19 is for things like lambda expressions - in 18 C#, things like:

public IEnumerable<int> Foo()
    int[] numbers = new[] { 1, 5, 2, 5, 6 };

    return numbers.Select(x => x+10);

The x => x+10 (a lambda expression) can 17 be regarded as an "inline function" in source 16 code terms, in that it's an extra function 15 declared "inline" in another method.

And 14 yes, VB9 has this:

Dim len As Func(Of String, Integer) = Function(x) x.Length       

I believe there are more 13 restrictions in VB9 compared with C# though 12 - for instance, I don't think you can create 11 an Action<T> (or any other delegate type with a void 10 return type) using a lambda expression in 9 VB9, whereas it's easy in C#. Likewise I 8 believe the lambda has to be just a single 7 expression in VB9, whereas in C# it can 6 be a whole block in braces.

As of VB10, you 5 can create multi-line lambda expressions 4 in VB too.

Of course, if the question was 3 really referring to the execution rather 2 than the source code, all of this is irrelevant 1 :)

Score: 8

Inlining is the responsibility of the CLR 7 (JIT) and there are some conditions as to 6 when a function is inlined:

  1. The code size is smaller than 32 bytes of IL.
  2. The function does not contain "complex" constructs, e.g. loops, switch etc.
  3. The function does not contain try/catch/finally.
  4. The function is not declared as virtual.

As you will probably 5 find out, 32 bytes is not very much in terms of code, it 4 is suitable for quick and small if-else 3 condition testing, property getters/setters. I 2 don't think you can gain any speed from 1 inlining a bigger amount of code.

Score: 6

this question still have some activities 2 5 years later (already???)

with .net 4.5 it is now possible to specify inline method

simply use this 1 attribut

Score: 2

The .NET team has wisely decided that such 7 features have no place in the programming 6 language itself since the compiler is better 5 in deciding what to inline anyway. That's 4 putting it rather aggressively but the fact 3 is that modern C++ actually often disregard 2 the inline instruction for their decision-making 1 process about which functions to inline.

Score: 2

Here's one scenario when it would be a nice 22 facility:

We have a standard logging function 21 to which it is often useful to pass the 20 caller's name. However, if logging is set 19 to a low verbosity, the logging function 18 simply returns to aid performance. To avoid 17 the caller making CPU-expensive calls to 16 System.Reflection.MethodBase.GetCurrentMethod.Name, only 15 for the logging function to not use them, it 14 would be nice if we could create an inline 13 function which decided in advance whether 12 to bother finding out the method's name:

Public Inline Function GetMyname(iLogLevel) as string
  if iLogLevel < 3 then return ""
  return System.Reflection.MethodBase.GetCurrentMethod.Name()
End Function 

Then 11 in the caller, we could have put a neat 10 line like:

Log(2,GetMyname(2) & ": Something 9 happened")

But if the compiler decides not 8 to inline GetMyname() (as it surely would) then 7 the methodname that would appear in the 6 log would always be "GetMyName" - no use 5 at all.

Call me old fashioned, but if we 4 start leaving all decisions to the compiler 3 (or the CLR!) then we may as well look for 2 a new career and leave it to "the .Net team". Sometimes, software 1 designers DO actually know best?

Score: 1

You cannot declare a function as inline 2 explicitly, but the JIT optimizer is free 1 to inline functions if it sees the benefit.

Score: 0

Noting Tom's issue of May `11, I'd have 7 to say that if you have an application that 6 is truly performance critical, then using a language 5 that depends on the CLR is probably not 4 your best option.

In fact, an object-oriented 3 design probably isn't your best choice either. You 2 might want to seriously look at C as an 1 alternative.

More Related questions