Is it possible to embed C code in a C# project?

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2010-05-20T14:24:05Z Indexed on 2010/05/20 14:30 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

I know that it's possible to compile my C code into a dll, and then use P/Invoke to call that code.

What I wondered if it was possible to have a chunk of C code embedded directly in my code, perhaps only available to one class...

Something like this (non-working) example:

public class MyClass {
    extern "C" {
        int do_something_in_c(int i) {
            return i*2;
        }
    }

    public int DoSomething(int value) {
        return do_something_in_c(value);
    }
}

I've been trying for a few hours using Visual Studio 2008, but I'm not getting anywhere, and I suspect that it isn't actually possible. Can anyone confirm or deny this?

Thanks.

© Stack Overflow or respective owner

Related posts about c

    Related posts about c#