Alex
Alex

Reputation: 14513

What is this syntax, initializing an interface as it would be an array of classes? (new IItemTransform[0])

new IItemTransform[0] is found in System.Web.Optimization.Bundle. I've tried to google for anything regarding this syntax, but with no success due to search queries of too generic words.

public virtual Bundle IncludeDirectory(string directoryVirtualPath, string searchPattern, bool searchSubdirectories)
    {
      if (ExceptionUtil.IsPureWildcardSearchPattern(searchPattern))
        throw new ArgumentException(OptimizationResources.InvalidWildcardSearchPattern, "searchPattern");
      PatternType patternType = PatternHelper.GetPatternType(searchPattern);
      Exception exception1 = PatternHelper.ValidatePattern(patternType, searchPattern, "virtualPaths");
      if (exception1 != null)
        throw exception1;
      Exception exception2 = this.Items.IncludeDirectory(directoryVirtualPath, searchPattern, patternType, searchSubdirectories, new IItemTransform[0]);
      if (exception2 != null)
        throw exception2;
      else
        return this;
    }

IItemTransform:

namespace System.Web.Optimization
{
  public interface IItemTransform
  {
    string Process(string includedVirtualPath, string input);
  }
}

This feel really unintuitive, does anyone recognize the syntax?

Upvotes: 3

Views: 201

Answers (1)

TypeIA
TypeIA

Reputation: 17258

new IItemTransform[0] is simply creating an array (of size zero) of references to type IItemTransform. It's not creating any objects that implement IItemTransform.

It's syntactically the same as creating an empty array of strings:

string[] foo;
foo = new string[0];

It's just an array of IItemTransform, not string.

Upvotes: 5

Related Questions