please explain the use of "default()" in this code
source http://technet.microsoft.com/en-us/library/ms162234%28SQL.100%29.aspx
code
//Connect to the local, default instance of SQL Server.
{
Server srv = default(Server);
srv = new Server();
//Create a linked server.
LinkedServer lsrv = default(LinkedServer);
lsrv = new LinkedServer(srv, "OLEDBSRV");
//When the product name i开发者_Go百科s SQL Server the remaining properties are
//not required to be set.
lsrv.ProductName = "SQL Server";
lsrv.Create();
}
why to use default(Server),? -even if its like server asd = new asd(); it will still connect to the default instance!
why to use default(linkedserver) -whats the point? we still specify the srv and provider and product!
default(...)
is the default value operator. It evaluates to null for reference types, or the "zero" value for value types.
There's absolutely no point to it here... the variable is assigned a different value immediately. Here's the equivalent, tidier code:
Server srv = new Server();
//Create a linked server.
LinkedServer lsrv = new LinkedServer(srv, "OLEDBSRV");
//When the product name is SQL Server the remaining properties are
//not required to be set.
lsrv.ProductName = "SQL Server";
lsrv.Create();
The default
keyword was added in .NET 2.0 to satisfy generics. It just represents the default (meaning uninitialized) value of whatever type is passed to it. For reference types, this is null
; for value types, it's the "zero" value. There's really no reason to use this operator unless you're writing a generic function.
In your example it's not doing anything. It could be removed and the first two lines could be combined.
Server srv = new Server();
Same thing with the linked server.
LinkedServer lsrv = new LinkedServer(srv, "OLEDBSRV");
there doesnt appear to be a point, considering it is assigned immediately after. the statement in this situation is useless. You might as well put
Server srv = new Server();
The default
can be useful for assigning typed null references. I have a preference for using the var
keyword when declaring variables like this:
var value = default(Class);
The only way I can do this is by using default
because the compiler needs type information to infer value
. In your specific example it simply adds pointless verbosity.
The default(Server)
will return null. Syntactically it's the equivalent of
Server srv = null;
It's convenience code which allows for uniform handling of value types and reference types, it's especially convenient when you are dealing with generics.
Check out the following reference: http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx
default
provides the default value of the given type (used heavily with generics). For reference types this is null and value types is the zero value.
In your situation, there is no point - you could instantiate the instance inline or specify null
as you know the type...
default
always zeros out a field. For reference types that means the reference is null
(as null
is really pointing to address zero), and for value types it means all the fields in it are zero'd in memory, effectively making numeric types zero, DateTime
be DateTime.MinValue
, etc.
In this case, I am assuming both Server
and LinkedServer
are reference types, so using default is the same as setting them to null
. I assume whoever wrote this code doesn't really understand what default
does.
By using default
keyword you can assign a default value to a variable. It comes in handy when you're using generic types.
精彩评论