Explain the use of "default ()" in this code

Source http://technet.microsoft.com/en-us/library/ms162234%28SQL.100%29.aspx

the 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 is SQL Server the remaining properties are //not required to be set. lsrv.ProductName = "SQL Server"; lsrv.Create(); } 

why use default (Server)? - even if its similar server asd = new asd (); it will still connect to the default instance!

why use default (linkedserver) - what's the point? we still indicate srv and the supplier and product!

+6
c # smo
source share
9 answers

default(...) is the default operator. It evaluates to null for reference types or null for value types.

There is no point ... a variable is immediately assigned a different value. 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(); 
+11
source share

The default keyword was added in .NET 2.0 to satisfy generics. It simply represents the default value (uninitialized value) of any type passed to it. For reference types, this is null ; for value types, this value is zero. There really is no reason to use this operator unless you write a generic function.

+2
source share

In your example, this does nothing. It can be deleted, and the first two lines can be combined.

 Server srv = new Server(); 

Same thing with a linked server.

 LinkedServer lsrv = new LinkedServer(srv, "OLEDBSRV"); 
+2
source share

the point is not displayed there, assuming that it is assigned immediately after. statement in this situation is useless. You could also put

 Server srv = new Server(); 
+2
source share

default can be useful for assigning typed null references. I prefer to use the var keyword when declaring such variables:

 var value = default(Class); 

The only way to do this is to use default , because the compiler needs type information in order to draw value . In your specific example, this simply adds meaningless verbosity.

+2
source share

default(Server) will return null. Syntactically, this is equivalent

 Server srv = null; 

This is a convenience code that allows you to uniformly process value types and reference types, especially when dealing with generics.

Check out the following link: http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx

+1
source share

Using the default keyword, you can assign a default value to a variable. This comes in handy when you use generic types.

0
source share

default provides the default value for this type (heavily used with generics). For reference types, this is null, and values ​​are null.

There is no point in your situation - you can create an instance of the inline instance or specify null , as you know the type ...

0
source share

default always a null field. For reference types that mean a null reference (since null really points to a null address), and for value types, this means that all fields in it are zero in memory, effectively making the numeric types zero, DateTime be DateTime.MinValue , etc. .

In this case, I assume that both Server and LinkedServer are reference types, so using the default value is the same as setting them to null . I assume that the one who wrote this code really does not understand what default does.

0
source share

All Articles