please explain the use of "default()" in this code
Asked Answered
D

9

6

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 is 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!

Dispose answered 28/7, 2010 at 13:58 Comment(1)
Note that this default is nothing to do with the default SQL Server instance - it's purely a C# language construct (which, as all the answers point out, adds nothing here except confusion). I'm tempted to put in a bit of feedback asking that C# language examples are reviewed by an actual human who speaks C#...Genesisgenet
L
11

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(); 
Lassie answered 28/7, 2010 at 14:0 Comment(0)
C
2

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.

Catechetical answered 28/7, 2010 at 14:1 Comment(1)
I like using it for code contracts, when specifying the contract for an interface. You have to implement the interface, so anything returning a value has to return something. By explicitly using default I'm hoping it acts as reinforcement that we're not returning a value we particularly expect to be useful.Lassie
W
2

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");
Wandering answered 28/7, 2010 at 14:1 Comment(0)
N
2

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();
Na answered 28/7, 2010 at 14:1 Comment(0)
A
2

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.

Anemology answered 28/7, 2010 at 14:3 Comment(9)
I would definitely find that less readable than Class value = null; - I'm not one of these people who pathologically dislikes var, but I see no benefit in using it here.Lassie
@Jon - It is about consistency. I also find it more readable so there is opinion involved as well.Anemology
@Chaos: Consistency of what? Do you use var to declare every local variable?Lassie
@Jon - Indeed. Can I assume the emphasis on every means you are surprised?Anemology
@Chaos: Absolutely. In many cases the return type of a method isn't blindingly obvious from the method name, and in such cases I would almost never use var. Do you use it for integer literals as well? What's the type of var x = 300000000;? If you have to count the digits in a literal to work out the type of a variable, there's something wrong IMO :)Lassie
@Jon - Point 1: It forces me to write detailed method names and I don't use notepad to write my code. :) Point 2: I use literal notation var x = 300000000L or explicit casting var x = (byte)1.Anemology
@Chaos: Detailed method names don't always mean including the return type though... and it doesn't help if you're implementing an interface, for example. Basically I would find it hard going reading a codebase which used var everywhere. In moderation, yes - but I wouldn't do it everywhere.Lassie
@All - I'm with skeet on that one. There's a good section on CLR Via C# (3rd edition) stating that vars should be kept to a minimum and I generally end up only using them based on some sort of linq expression being returned. If you need to use vars for ints / decimals / strings there's something wrongHeavyfooted
@Jon Skeet, @Heavyfooted - F# seems to pull it off quite well. It isn't for everyone though. It isn't like I am trying to convince you to use it.Anemology
O
1

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

Octahedron answered 28/7, 2010 at 14:1 Comment(0)
U
0

By using default keyword you can assign a default value to a variable. It comes in handy when you're using generic types.

Ukrainian answered 28/7, 2010 at 14:1 Comment(0)
K
0

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...

Kassi answered 28/7, 2010 at 14:2 Comment(0)
V
0

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.

Villainage answered 28/7, 2010 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.