Home c# System.Collections.Genic.list`1

System.Collections.Genic.list`1

Author

Date

Category

Comrades of experts! Thank you in advance for the answer.

The program must create a method for filtering the list by specified parameters. As parameters, the list itself must be set, a filtering property (Name or Surname) and a text value.

Questions:

  1. was originally an error CS0103 (on an object in the call Filtr ()), it turned out to get rid of it, now the project is started, but in the console it gives

System.Collections.Genic.list`1 [ArrayFiltr.Object]

saw an article with the same question in the English stack, but did not understand how to apply it in his code, and I did not find anything that helped b.

  1. Does Name or Surname be set as a parameter, you can not specify a text value as written in my code?
using system;
Using System.Collections.Genic;
Using System.Linq;
Namespace ArrayFiltr.
{
  Class Object.
  {
    Public String Name {Get; SET; }
    Public String Surname {Get; SET; }
    Public Static List & LT; Object & GT; Objects = New List & LT; Object & GT;
    {
      New Object {name = "Ivan", Surname = "Ivanov"},
      New Object {name = "Petr", Surname = "Petrov"},
      New Object {name = "Vasiliy", Surname = "Vasiliev"}
     };
   }
  Class Program: Object
  {
    Public Static Void Main (String [] Args)
    {
      Filtr (Objects, "Ivan");
    }
    Static Void Filtr (List & LT; Object & GT; Objects, String Name)
    {
      List & lt; Object & GT; NewObjects = New List & LT; Object & GT; {};
      var selectedObject = from Object in Objects
                 Where object.name == NAME
                 Select Object;
      Foreach (Object Object in SelectedObject)
        newObjects.add (Object);
      Console.WriteLine (NewObjects);
    }
  }
}

Answer 1, Authority 100%

newObjects.Foreach (OBJ = & GT; Console.WriteLine ("{0} {1}", obj.name , obj.surname))

or overload toostring () :

public override string toostring ()
{
  Return String.Format ("{0} {1}", name, Surname);
}
...
newObjects.Foreach (Console.Writeline);

And even better like this:

public static void foreach & lt; t & gt; (this ienumerable & lt; t & gt; enumeration, action & lt; t & gt; action)
{
  Foreach (T Item in Enumeration)
  {
    Action (Item);
  }
}
...
var selectedObjects = from Object in Objects
           Where object.name == NAME
           Select Object;
selectedObject.Foreach (Console.WriteLine);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions