0

I know how to use MailAddress like this: new MailAddress("someone@foo.com")

But what is this I'm seeing: new MailAddress[0] - how is this used and when is it ok to use? I don't see anything about this in the docs on MSDN.

JacobIRR
  • 8,545
  • 8
  • 39
  • 68
  • 3
    `new MailAddress[0]` creates an array of `MailAddress`es of 0 size (i.e. no elements). You can use it whenever you want. It's not specific to the `MailAddress` type. – itsme86 Nov 09 '17 at 19:09
  • Ah ok. And is this just used to satisfy type requirements when calling a given method? – JacobIRR Nov 09 '17 at 19:10
  • Hard to say without seeing its use in context, but more than likely, yes. – itsme86 Nov 09 '17 at 19:11
  • It's usually used when you need to pass in an empty collection, though `Enumerable.Empty()` would be the preferred way to do it, assuming the method needs a sequence rather than an array. – Eric Lippert Nov 09 '17 at 19:11
  • its a way to pass in an empty array to a function that requires an array – pm100 Nov 09 '17 at 19:11
  • 2
    Note that it is considered a very poor programming practice to pass `null` in when what you mean is "a collection that happens to be empty". Whoever wrote this code should be given points for not passing null, but could also have clarified the code by saying why it is that they're passing in an empty collection. – Eric Lippert Nov 09 '17 at 19:13
  • awesome thanks, makes sense now – JacobIRR Nov 09 '17 at 19:13
  • @EricLippert Just wondering, why something like [`EmptyArray.Value`](http://referencesource.microsoft.com/mscorlib/R/8a9692baba002fe6.html) was not made public? It replicated many time in .NET Framework itself: [1](http://referencesource.microsoft.com/System.Core/R/09bbfaa1f07d0c51.html), [2](http://referencesource.microsoft.com/mscorlib/R/61f6a8d9f0c40f6e.html), [3](http://referencesource.microsoft.com/System/R/84d671b41038f83d.html), [4](http://referencesource.microsoft.com/System/R/106534f51ac82633.html). – user4003407 Nov 09 '17 at 19:42
  • @PetSerAl: There's no need to supply a reason for *not* doing a trivial feature that any developer can do themselves. Such a feature would add almost no value, but add significant costs: design, testing, documentation, and so on. – Eric Lippert Nov 09 '17 at 19:51
  • 1
    @EricLippert - I would remove the "testing" part or are you saying that the .NET devs only test stuff thats public? ;) – Rand Random Nov 09 '17 at 19:53
  • @EricLippert Point is to reduce amount of unique empty array instances, that is why them cached in the first place instead of using `new T[0]` everywhere. Sure it is trivial to make such cache per assembly. But I do not see any trivial way to make it per `AppDomain`. Yes, anyone can create assembly with public empty array cache, but no one will use it unless it is part of core library. – user4003407 Nov 09 '17 at 20:19

1 Answers1

0

After all the comments above.... the main difference is

mailAddress mA = new mailAddress();

mA object is initialized by calling default constructor.

mailAddress[] mA1 = new mailAddress[0];

In this case, no object is initialized, it is just declaration of array of objects. No constructor calls.

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • @Naidu That explains why it's valid to construct an array of size zero. Not why it's valid to access the first element of an array with zero items in it. Feel free to run the code yourself if you want to see it for yourself. – Servy Nov 09 '17 at 19:29
  • I was totally blinded with construction.....and thought some kind of optimization is working..... Thanks you @Sefe – Pavan Chandaka Nov 09 '17 at 19:48