locked
Hybrid code RRS feed

  • Question

  • Can we write hybrid code in C#? I mean can we use both unsafe and managed code in the same C# source so they can interact with each other?

    If we can, is there any advantage of hybrid codes?

    And also, if we can, can someone please give some example of hybrid code or some reference?

    Monday, November 12, 2012 9:21 PM

Answers

  • You can mix safe and unsafe code in C#.  The unsafe keyword (http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx) allows you to setup an unsafe context, and use pointers, etc.

    This allows you to use unsafe code, but only where you explicitly say "I know I'm doing something that's dangerous".

    As for reference material, see "Unsafe code and pointers": http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Steve Hémond Monday, November 12, 2012 9:32 PM
    • Marked as answer by Tonchi91 Tuesday, November 13, 2012 1:13 AM
    Monday, November 12, 2012 9:25 PM
  • You would need two implementations to compare the performance difference.  But if the code cannot be written in managed code alone then you cannot have two versions to compare. 


    Paul Linton

    • Marked as answer by Tonchi91 Tuesday, November 13, 2012 1:13 AM
    Tuesday, November 13, 2012 1:10 AM
  • That is obvious. I want to know is there any difference in performance?

    There can be, but there isn't necessarily any difference.

    For example, see this question and my answer - in that case, by using unsafe pointer math, the poster was able to get managed code faster than pure native C++, where before the native was 20% faster.

    That being said, this is one of those cases where you'd probably only want to drop into unsafe code if you have a good, measured (ie: via a profiler) reason to do so.  I've done this many times - my company's main product has quite a few small unsafe blocks scattered throughout the code, but only in places where we've had measured bottlenecks and I could get a significant improvement via using pointers, avoiding bounds checks, etc.

    That being said, it's also just as possible to reduce your performance if you're not careful.  The JIT tends to not do as many optimizations in unsafe code, so you can easily work against yourself if you optimize prematurely via unsafe blocks.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Reed Copsey, JrMVP Tuesday, November 13, 2012 1:24 AM
    • Marked as answer by Tonchi91 Thursday, November 15, 2012 7:13 AM
    Tuesday, November 13, 2012 1:23 AM

All replies

  • You can mix safe and unsafe code in C#.  The unsafe keyword (http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx) allows you to setup an unsafe context, and use pointers, etc.

    This allows you to use unsafe code, but only where you explicitly say "I know I'm doing something that's dangerous".

    As for reference material, see "Unsafe code and pointers": http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Steve Hémond Monday, November 12, 2012 9:32 PM
    • Marked as answer by Tonchi91 Tuesday, November 13, 2012 1:13 AM
    Monday, November 12, 2012 9:25 PM
  • Ok thank you. But this is only a one part of answer. I also want to know what is the advantage of the hybrid code?

    Monday, November 12, 2012 11:15 PM
  • The advantage would be doing things that cannot be achieved in managed code alone.

    Paul Linton

    Tuesday, November 13, 2012 12:44 AM
  • That is obvious. I want to know is there any difference in performance?
    Tuesday, November 13, 2012 1:01 AM
  • You would need two implementations to compare the performance difference.  But if the code cannot be written in managed code alone then you cannot have two versions to compare. 


    Paul Linton

    • Marked as answer by Tonchi91 Tuesday, November 13, 2012 1:13 AM
    Tuesday, November 13, 2012 1:10 AM
  • That is obvious. I want to know is there any difference in performance?

    There can be, but there isn't necessarily any difference.

    For example, see this question and my answer - in that case, by using unsafe pointer math, the poster was able to get managed code faster than pure native C++, where before the native was 20% faster.

    That being said, this is one of those cases where you'd probably only want to drop into unsafe code if you have a good, measured (ie: via a profiler) reason to do so.  I've done this many times - my company's main product has quite a few small unsafe blocks scattered throughout the code, but only in places where we've had measured bottlenecks and I could get a significant improvement via using pointers, avoiding bounds checks, etc.

    That being said, it's also just as possible to reduce your performance if you're not careful.  The JIT tends to not do as many optimizations in unsafe code, so you can easily work against yourself if you optimize prematurely via unsafe blocks.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Reed Copsey, JrMVP Tuesday, November 13, 2012 1:24 AM
    • Marked as answer by Tonchi91 Thursday, November 15, 2012 7:13 AM
    Tuesday, November 13, 2012 1:23 AM
  • I know all of that. Don't worry, this is just a matter of learning.
    Tuesday, November 13, 2012 8:46 AM