What situations are appropriate for C# to C interop?

Given that C# has the unsafe keyword, are there situations in which it would still be beneficial to interoperate with C libraries?

I could see the need for very fast number crunching, graphical manipulation or matrix operations, using pre-existing C libraries. But is there a use case for writing greenfield DLL’s in, say, C99, and interoperating with them from C#?

I imagine that there might be edge cases where dropping down to C (in the sense that it is closer to the metal) would be the only way to get the needed performance characteristics.

2

There are a couple of things C has over C#.

Can do real time code with C. Not that windows is the best platform for it. But I have done realtime control systems with it. It would be nice to do it in C#, and most of the time it is predictable, but not always, and that’s killer for RT systems.

Portability of C code over a number of platforms ( not just linux / windows, but any number of embedded systems ). I use things like encryption and compression from windows to embedded devices, rather than having a C and C# version of the code, easier to have one library with C# interop.

2

First, unsafe is useful for a lot more than C interop. For example, doing image analysis with actual pixel data can be horribly slow while in “safe” mode. Flip over to unsafe mode, and doing the same work with direct access to the image buffer, is an order of magnitude faster.

Doing wholesale development in C/C++ for inclusion in a .Net project can certainly make sense, but it all comes down to performance. The classic advice would be to write the entire application in your language of choice, then profile. When you find a critical section of code that takes 50%+ of processing time and cannot be realistically tuned anymore while using a managed language, then re-implement that one routine in C and integrate.

A real life example: I was prototyping a path finding algorithm. After showing it worked with C#, I needed to make it fast enough to run many times a second. I had already built up some nice support UI in C# that showed all of the factors used for routing decisions, the results, etc. So I reimplemented the core part of the routing algorithm in C++. If I had started in C++, I doubt I would have gotten as far.

But is there a use case for writing greenfield DLL’s in, say, C99, and interoperating with them from C#?

The main use case I see is when you are going to write a special lib which is targeted for many platforms where C or C99 is available, but C# is not or only for a few of them (“C#” may be not available for organizational reasons, not just pure technical ones).

Another use case is when you have an expert in some field (for example, an expert about a specific hardware), and he has the task to code some of his expert knowledge into a library, but that expert has 30 years of experience in C but none in C#.

One thing to consider: “100% greenfield” is a very rare situation today. For most kind of problems, at least partial solutions exist, or similar solutions which can be re-used when modified. So using C makes a whole lot of sense when you have to build something new on an existing C code base.

2

Yes, for interfacing with legacy software. I had to create an interop DLL that could be consumed by my legacy C application to take advantage of technology that was not available when my toolkit was invented.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật