TheSwamp

Code Red => .NET => Topic started by: Kerry on November 25, 2015, 12:15:41 PM

Title: Something new for me.
Post by: Kerry on November 25, 2015, 12:15:41 PM
I came across this code tonight and it has me stumped

Code - C#: [Select]
  1.  
  2.             Editor ed = default(Editor);
  3.             ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  4.  
  5.             PromptPointResult ppr1 = default(PromptPointResult);
  6.             ppr1 = ed.GetPoint(new PromptPointOptions("Select start point"));
  7.  
  8.             if (ppr1.Status != PromptStatus.OK) {return;}
  9.  
  10.             PromptPointResult ppr2 = default(PromptPointResult);
  11.             ppr2 = ed.GetPoint(new PromptPointOptions("Select end point"));
  12.             if (ppr2.Status != PromptStatus.OK) {
  13.                 return;
  14.             }
  15.  
The source is
http://adndevblog.typepad.com/autocad/2015/11/mleader-text-that-reflects-mleaderstyle-settings.html

The code compiles and executes after netloading.
My query is regarding the use of the default keyword.

My help file associates it's use with generics where the type is unknown.
https://msdn.microsoft.com/EN-US/library/xwth0h0d(v=VS.140,d=hv.2).aspx

It's very late for me and I'm a little brain dead but I don't understand why the keyword is used.

Can anyone venture an opinion ??

Regards,


added:
I probably should ask Balaji :)
Title: Re: Something new for me.
Post by: Keith Brown on November 25, 2015, 12:26:11 PM
My thought is that it initializes each variable to its default value.  Used in case you do not know its default value.  In the context that it is being used, I do not belive that it is required.  c# will automatically set the variable to its default value in its constructors.
Title: Re: Something new for me.
Post by: Kerry on November 25, 2015, 12:32:56 PM

Hi Keith,
I stepped through in the debugger and the values were null until initialised on the following line ; so it serves no 'obvious/demonstrable' purpose.

My initial thoughts were the same as yours ... but it's been nagging at me .

Thanks


Title: Re: Something new for me.
Post by: Kerry on November 25, 2015, 12:44:07 PM
I posted a comment for Balaji ... perhaps we'll know tomorrow.

for a giggle and with respect

Title: Re: Something new for me.
Post by: MickD on November 25, 2015, 03:43:42 PM

Given what was said in the help doc's it seems like a reasonable way to avoid using null as an initialiser which can only be a good thing in avoid null ref exceptions. Thanks for bringing it up :)

In the case of the Editor declaration above though, it is just wasted keystrokes and I'm surprised it compiles, perhaps it's optimised away by the compiler given the 'default' hint?
Title: Re: Something new for me.
Post by: Keith Brown on November 25, 2015, 04:07:44 PM

Given what was said in the help doc's it seems like a reasonable way to avoid using null as an initialiser which can only be a good thing in avoid null ref exceptions.

If the variable is not initialized in the constructor, and it is a reference type it will be set to null anyway.  At least that is my understanding of how it works.  A reference type has a default value of null.  Correct?
Title: Re: Something new for me.
Post by: MickD on November 25, 2015, 04:24:50 PM
...
A reference type has a default value of null.  Correct?

Right you are Keith, my bad, I didn't grok it on first read through.
I was hoping it was .net heading in a direction to eliminate null (like in F#) although they would most likely have to implement the 'Option' type as an alternative which is much safer to use and check.

The example in the doc's shows one of the very few places it would be useful (inside a generic list), elsewhere there is no real reason to use it given the context you are working with as far as I can see.
Title: Re: Something new for me.
Post by: Kerry on November 25, 2015, 09:09:23 PM

A web search found a few usages of  default(PromptPointResult)

Most were by Oleg ( fixo ) during the last 2 1/2 years.
Title: Re: Something new for me.
Post by: Kerry on November 26, 2015, 02:12:49 AM
From the Author :
Quote

Balaji has replied to your comment:

Hi Kerry,

I had implemented the code in this blog post initially in VB.Net and later used an online converter to do the C# conversion for the sake of creating this blog post. The converter had introduced this and i did not notice it. Sorry, I do not know what that means. But, the C# version did build and run ok in AutoCAD.

Please do let me know if you have any information on it.

This is the converter that i had used for my VB.Net to C# conversion :
converter.telerik.com

Thanks

Balaji

View the post and comment: http://adndevblog.typepad.com/autocad/2015/11/mleader-text-that-reflects-mleaderstyle-settings.html?cid=6a0167607c2431970b01b8d17b453d970c#comment-6a0167607c2431970b01b8d17b453d970c


added:
In light of this, it's possible that Oleg translated his usage from VB.net with the same translator.
Title: Re: Something new for me.
Post by: huiz on November 26, 2015, 03:17:01 AM
The most common usage is in a Generic class, not when you create a new object form a known type.

Code: [Select]
public T Foo<T>() {
 ...
 ...
 ...
 return default(T);
}
Title: Re: Something new for me.
Post by: huiz on November 26, 2015, 03:19:14 AM
Probably converted from VB.NET where you can use

Code: [Select]
  Dim variable As T = Nothing
Title: Re: Something new for me.
Post by: Kerry on November 26, 2015, 03:19:41 AM
The most common usage is in a Generic class, not when you create a new object form a known type.

Code: [Select]
public T Foo<T>() {
 ...
 ...
 ...
 return default(T);
}

Yes.
Title: Re: Something new for me.
Post by: Kerry on November 26, 2015, 03:23:57 AM
Probably converted from VB.NET where you can use

Code: [Select]
  Dim variable As T = Nothing

I can't imagine that being the case. I doubt the original author would use generics for something like this ... it is VERY type specific.

Perhaps Balaji will post the VB.net code here if he happens to visit.