string.Join() in .net 3.5

2020-04-02 07:26发布

I have a .net 3.5 project in vs2008 and I'm trying to use this overload of string.Join() (the one that takes a string and IEnumerable<T>) and the compiler does not seem to know about this overload.

This is the code that I tried

    var result = string.Join(" ", Foo());

where Foo() is

    IEnumerable<string> Foo()
    {

        foreach(string s in new []{"1", "2", "3"} )
        {
            yield return s;
        }
    }

I get

> Error 2   Argument '2': cannot convert from
> 'System.Collections.Generic.IEnumerable<string>' to 'string[]'

Of course, if I use Foo().ToArray() it works but I'm wondering why the overload that takes IEnumerable<T> won't work.

MSDN in classic view says it's compatible with vs2008/.net 3.5

enter image description here

(I couldn't find the message "This page is specific to...." in non-classic views so I thought I'd put up a screen-cap.)

标签: c#
6条回答
爷的心禁止访问
2楼-- · 2020-04-02 07:58

The MDSN page you are looking at is only for .Net 4 and later. This is the page you should check:

http://msdn.microsoft.com/en-us/library/0h8wc12c(v=VS.90).aspx

Notice that it specifically lists .Net Framework 3.5 and that it does not include your overload.

查看更多
做自己的国王
3楼-- · 2020-04-02 07:59

If you take a look at the Supported Platforms section you can find out that:

NET Framework Supported in: 4

So as a case use ToArray() along with Foo() call:

Foo().ToArray()
查看更多
姐就是有狂的资本
4楼-- · 2020-04-02 08:07

For what it's worth, if you want to stick with .Net 2.0 or 3.5, and avoid having to write ".ToArray()" on the second argument of String.Join(), here's a simple-minded implementation that should mimic the String.Join() implemented in .Net 4.0.

   public static class MString
   {
      /// <summary>
      /// This method simply duplicates the String.Join() method that was introduced in .Net 4.0. So
      /// it can be used in C# projects targeting .Net 3.5 or earlier.
      /// </summary>
      /// <typeparam name="T">type of the object collection (typically string)</typeparam>
      /// <param name="separatorString">separator to be placed between the strings</param>
      /// <param name="valueObjects">collection of objects that can be converted to strings</param>
      /// <returns>string containing all of the objects converted to string with separator string in between</returns>
      public static string Join<T>(string separatorString, IEnumerable<T> valueObjects)
      {
         if (separatorString == null)
            separatorString = "";

         bool firstValue = true;
         StringBuilder stringBuilder = new StringBuilder();
         foreach (T oneObject in valueObjects)
         {
            if (!firstValue)
               stringBuilder.Append(separatorString);
            firstValue = false;

            stringBuilder.Append(oneObject == null ? "" : oneObject.ToString());
         }

         return stringBuilder.ToString();
      }
   }
查看更多
家丑人穷心不美
5楼-- · 2020-04-02 08:13

The version information at the bottom says something different:

Version information on MSDN

Note that you can find that version information at the bottom of the article, regardless of the selected view (it might just look a little different).

The note in the upper-right you have found is referring to the selected resource version you can find in the URL, such as in:

http://msdn.microsoft.com/en-us/library/dd783876(VS.90).aspx

(highlighted in bold). Usually this selects the framework version as well (since those are released in tandem with VS), but apparently there seems to be a mistake in the classification here.

I'd just report it as a mistake.

查看更多
唯我独甜
6楼-- · 2020-04-02 08:22

The string.Join overload accepting IEnumerable<T> was not added until .NET 4. It is not available in .Net 3.5. The classic view in MSDN documentation is simply incorrect.

In .NET 3.5, you will need to invoke ToArray() on the IEnumerable<string> in order to pass it into the Join method.

string.Join(" ", Foo().ToArray()); 

For reference, these are the overloads supported by .NET 3.5.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-04-02 08:22

The classic view does indeed provide a link to it, but it must be a wrong entry in their CMS or whatever. If you click the aforementioned link, you see that it's lost its context (the menu on the left does not know where you are, which should be in System.String.Join), and the version dropdown doesn't recognize it either.

The normal version works, and shows it's only for .NET 4 and .NET 4.5.

.NET Framework Supported in: 4

.NET Framework Client Profile Supported in: 4

查看更多
登录 后发表回答