Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace accessors Vs. "Using" accessors

Tags:

c#

.net

Theoretical question: is it any difference between doing this:

using System;
...
var foo = new String("foo");

and this:

var foo = new System.String("foo");

DLL loading? A performance difference?

Mainly, my doubt is what's the best code practice in this situation?

like image 502
Javier Capello Avatar asked Nov 30 '22 20:11

Javier Capello


2 Answers

Same thing. Use any one you like more. I use using, and fallback to full names when name collision occurs.

like image 38
Dialecticus Avatar answered Dec 03 '22 08:12

Dialecticus


No, they'll be compiled to absolutely identical IL.

The using directive (there's no such term as "namespace accessor") is just a way of telling the C# compiler that it should look in that namespace when trying to resolve simple names to fully-qualified ones.

(Of course both will actually fail to compile as there's no String(String) constructor in .NET, but that's a different matter.)

Note that using the built-in alias string is identical to using the System.String type too - it really is just an alias. For example:

// Just one type!
string x = new String(new char[10]);
like image 110
Jon Skeet Avatar answered Dec 03 '22 08:12

Jon Skeet