3

Ive always chosen VB/VB.NET as my language. I know that performance wise the difference between Vb.NET & C# are minimal if none. But there are some things that C# can do that VB.NET cant do.

I was reading about differences between the 2 languages and came across unsafe code. As i understand unsafe code is code that could cause a error at runtime rather than during compile is that correct? Either way, can someone maybe lay it out for me what it is?

Also, most importantly, why you would use it?

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55
  • 3
    Though I don't speak with authority, `unsafe` code is code that can leverage lower level functions like pointers and such. Thus it is *not managed*. This is a concern specifically with memory. – Mike Perrenoud Nov 12 '13 at 14:27
  • 2
    The notion that only unsafe code can cause an error at runtime is incorrect. I've written plenty of C# code that is safer than the Queen of England - and it's died at runtime :) – Moo-Juice Nov 12 '13 at 14:29
  • @Moo-Juice yes i just saw from msdn that '•In order for C# to compile unsafe code, the application must be compiled with /unsafe.'. – Mad Dog Tannen Nov 12 '13 at 14:30
  • @MichaelPerrenoud VB can work with unmanaged code. So whats the difference? – Mad Dog Tannen Nov 12 '13 at 14:31
  • @KayNelson, I didn't say *work with* unmanaged code; I said *write* unmanaged code. – Mike Perrenoud Nov 12 '13 at 14:32
  • @MichaelPerrenoud sorry my fault – Mad Dog Tannen Nov 12 '13 at 14:33
  • 1
    @KayNelson, no worries at all. I wanted to make it very clear. I apologize if it came off as rash. – Mike Perrenoud Nov 12 '13 at 14:33
  • That above statement is correct, just wanted to add that having to work with COM objects (and sometimes you have to do that, whether you like it or not) is probably the most common reason to use unsafe code. – MolestedRabbit Nov 12 '13 at 14:31
  • In a unsafe context you can use pointers. There is a method to convert an image to grayscale using pointers. There is a significant performance impact. – BrunoLM Dec 20 '13 at 03:02

0 Answers0