-3

\Edited the code getting exception at sb.Append(" Allow Prefixes: ") line \An exception of type 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code

public partial class IndexingPolicy : IEquatable

{
  public IndexingPolicy(List<string> allowPrefixes = default(List<string>), List<string> denyPrefixes = default(List<string>), bool? disableIndexing = default(bool?))

    {

        this.AllowPrefixes = allowPrefixes;

        this.DenyPrefixes = denyPrefixes;

        this.DisableIndexing = disableIndexing;
    }


    [DataMember(Name="allowPrefixes", EmitDefaultValue=false)]

    public List<string> AllowPrefixes { get; set; }


    [DataMember(Name="denyPrefixes", EmitDefaultValue=false)]

    public List<string> DenyPrefixes { get; set; }


    [DataMember(Name="disableIndexing", EmitDefaultValue=false)]

    public bool? DisableIndexing { get; set; }


   ").Append(DisableIndexing).Append("\n");

    public override string ToString()
    {
        var sb = new StringBuilder();

        sb.Append("class IndexingPolicy {\n");

        sb.Append(" AllowPrefixes: ").Append(
          string.Join(",", AllowPrefixes.ToList())
        ).Append("\n");


        sb.Append(" DenyPrefixes: ").Append(
          string.Join(",", DenyPrefixes.ToList())
        ).Append("\n");

        sb.Append("  DisableIndexing: ").Append(DisableIndexing).Append("\n");

        sb.Append("}\n");

        return sb.ToString();
    }
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • 1
    I think I can guess what `AllowPrefixes` is, but just in case, can you add it to the question? –  May 15 '18 at 18:25
  • 1
    Please post code that actually compiles; for example `AllowPrefixes`, `DenyPrefixes`, and `DisableIndexing` are all undefined in your code. Also, use `AppendLine()` instead of `Append("\n")`. – Dour High Arch May 15 '18 at 18:27
  • 1
    Possible duplicate of [Convert a list to a string in C#](https://stackoverflow.com/questions/4981390/convert-a-list-to-a-string-in-c-sharp) – Caramiriel May 15 '18 at 18:34
  • I'm guessing `AllowPrefixes` isn't actually a string. Could you tell us what `AllowPrefixes` is, i.e., the type and content of that variable? You probably just have to cast it to a string. – Max von Hippel May 15 '18 at 18:34
  • 1
    @MaxvonHippel it's a `List`, according to the output. –  May 15 '18 at 18:37
  • 2
    Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted. – Glorfindel Sep 23 '18 at 17:31

1 Answers1

1

The issue is that the default ToString() function just describes the class (in this case, System.Collections.Generic.List1[System.String]`). To get a string, you have override the string in some way, or convert the list contents to the string you're looking for.

I like to use string.Join() for this kind of situation.

public override string ToString()
{   
  var sb = new StringBuilder();    
  sb.Append("class IndexingPolicy {\n");    
  sb.Append(" AllowPrefixes: ").Append(
    string.Join(",", AllowPrefixes.ToList())
  ).Append("\n");

  sb.Append(" DenyPrefixes: ").Append(
    string.Join(",", DenyPrefixes.ToList())
  ).Append("\n");

  sb.Append(" DisableIndexing: ").Append(
    string.Join(","< DisableIndexing.ToList())
  ).Append("\n");

  sb.Append("}\n");

  return sb.ToString();
}

... and as a sidenote, I have found that using an array with string.Join is "cleaner" than using a stringbuilder, but just as performant.

public override string ToString()
{   
  var response = new List<string>() {
     "class IndexingPolicy {",
    $" AllowPrefixes: {string.Join(",", AllowPrefixes.ToList())}",   
    $" DenyPrefixes: {string.Join(",", DenyPrefixes.ToList())}",    
    $" DisableIndexing: {string.Join(",", DisableIndexing.ToList())}",
     "}",
     ""

  return string.Join(Environment.NewLine, response);
}

... and if use use an extension method for join:

public static string Join(this IEnumerable @this, string connector)
  => string.Join(connector, @this.ToList());

it gets even prettier:

public override string ToString()
{   
  var response = new List<string>() {
     "class IndexingPolicy {",
    $" AllowPrefixes: {AllowPrefixes.Join(",")}",   
    $" DenyPrefixes: {DenyPrefixes.Join(",")}",    
    $" DisableIndexing: {DisableIndexing.Join(",")}",
     "}",
     ""

  return string.Join(Environment.NewLine, response);
}
theGleep
  • 1,179
  • 8
  • 14
  • 1
    Fascinating how you could answer a question even tho it's not clear, you have a typo `<` should be a comma at the third `Join()` – Rainbow May 15 '18 at 18:38
  • 2
    @ZohirSalakCeNa the question is clear enough to be answered. –  May 15 '18 at 18:40
  • Yeah, I fixed the typo. It's not *obvious* - but I've seen the output the OP is suffering from often enough to know exactly what the issue is. (I added to the top of my answer to give an explanation of what the problem is - which also explains how I was able to understand the original question). – theGleep May 15 '18 at 18:40
  • 1
    Its a common issue. People expect a list's `ToString()` to output each item of the string, not realized it will use the default implementation from `object.ToString()` and simply spit out the class name. –  May 15 '18 at 18:43
  • getting an An exception of type 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code at the below line but in the output data is comming now sb.Append(" AllowPrefixes: ").Append( string.Join(",", AllowPrefixes.ToList()) ).Append("\n"); – santosh kumar May 16 '18 at 12:33
  • @santoshkumar: You shouldn't replace your question with the answer, it gets confusing for other users. You can post an answer to your own question instead. – Meta-Knight May 16 '18 at 12:43
  • The output data for the allowprefixes is null as there is no data for it. but getting an exception saying it cannot be null in the code – santosh kumar May 16 '18 at 12:46
  • @Meta-Knight yes, I agree. i just edited the question because there was not the complete code before , so added the complete code now – santosh kumar May 16 '18 at 12:50
  • @theGleep after adding the code which u mentioned above, I am getting the data, but at the same time getting the exception at line sb.Append(" AllowPrefixes: ").Append( string.Join(",", AllowPrefixes.ToList()) ).Append("\n"); the exception is as follows 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code, let me know if i need to add anything in the above code thanks – santosh kumar May 16 '18 at 17:29
  • @theGleep allow prefixes doesnt have any data so even in the output it will be empty, data is getting displayed in the output for Denyprefixes, so any idea how to get rid of this null exception for allow prefixes thanks – santosh kumar May 16 '18 at 17:46
  • @santoshkumar - "allow prefixes doesn't have any data" - is it empty, or is it null? Find the null value - maybe it's an element in AllowPrefixes? Without more detail, I can only guess. – theGleep May 16 '18 at 18:35
  • @theGleep detailed exception is as follows: Value cannot be null. the output is AllowPrefixes: , Denyprefixes: values, values.... here no isues with the output values but just trying to find out why the exception – santosh kumar May 16 '18 at 18:55
  • @santoshkumar - still not enough information. I'd suggest setting a debugger breakpoint and inspecting all of your values to find what is null. – theGleep May 17 '18 at 13:59