简体   繁体   中英

Error in select record from list in c#

Here is my code:

namespace sealedclass
{

    public class Contact
    {
        private string _firstName;
        private string _lastName;
        private int _age;

        public Contact(string fname, string lname, int age)
        {
            _firstName = fname;
            _lastName = lname;
            _age = age;
        }
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                _firstName = value;
            }
        }
        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                _lastName = value;
            }
        }
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }

    }

     private List<Contact> _contactList = new List<Contact>();
     _contactList.Add(new Contact("selva", "rani", 45));
    _contactList.Add(new Contact("sandhu", "dhya", 20));
    _contactList.Add(new Contact("sasi", "kala", 19));
    _contactList.Add(new Contact("s2", "s3", 44));

    public List<string> FirstNames
    {
        get
        {
           return _contactList.Select(C => C.FirstName.ToList());
        }
    }
}

I am new to c#, I found the above code in google to filter a particular record from a list.

Now it shows the error "Expected class,enum,delegate,interface or struct"

And I don't know where I can apply console.writeLine(); and how to fix these errors.

Can anyone help me? Thanks,

Please try with below code snippet.

public List<string> FirstNames
{
    get
    {
        return _contactList.Select(C => C.FirstName).ToList<string>();
    }
} 
//Below line will help you to write all first name on console
FirstNames.ForEach(Console.WriteLine);

Edit 1: (I have created new console application and written below code snippet and it works as expected). Let me know if any concern.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Contact c1 = new Contact();
            c1.FirstNames.ForEach(Console.WriteLine);
            Console.ReadLine();
        }
    }

    public class Contact
    {
        private string _firstName;
        private string _lastName;
        private int _age;

        public Contact(string fname, string lname, int age)
        {
            _firstName = fname;
            _lastName = lname;
            _age = age;
        }
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                _firstName = value;
            }
        }
        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                _lastName = value;
            }
        }
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }

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

        public Contact()
        {
            List<Contact> _contactList = new List<Contact>();
            _contactList.Add(new Contact("selva", "rani", 45));
            _contactList.Add(new Contact("sandhu", "dhya", 20));
            _contactList.Add(new Contact("sasi", "kala", 19));
            _contactList.Add(new Contact("s2", "s3", 44));
            FirstNames = _contactList.Select(C => C.FirstName).ToList<string>();
        }
    }
}

You can't have such code outside any method

_contactList.Add(new Contact("selva", "rani", 45));
_contactList.Add(new Contact("sandhu", "dhya", 20));
_contactList.Add(new Contact("sasi", "kala", 19));
_contactList.Add(new Contact("s2", "s3", 44));

It may be a good idea to have those lines in a method like:

public void Initialize()
{
    _contactList.Add(new Contact("selva", "rani", 45));
    _contactList.Add(new Contact("sandhu", "dhya", 20));
    _contactList.Add(new Contact("sasi", "kala", 19));
    _contactList.Add(new Contact("s2", "s3", 44));
}

Everything in C# has to exist in a class. You need to do something like this:

namespace sealedclass
{
    public class Contact
    {
        private string _firstName;
        private string _lastName;
        private int _age;

        public Contact(string fname, string lname, int age)
        {
            _firstName = fname;
            _lastName = lname;
            _age = age;
        }
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                _firstName = value;
            }
        }
        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                _lastName = value;
            }
        }
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }

    }
    class Program
    {

        public static List<string> FirstNames
        {
            get
            {
                return _contactList.Select(C => C.FirstName).ToList(); // Note that this isn't the best use of properties. Because it is creating a new object, this should really be a method.
            }
        }
        private static List<Contact> _contactList = new List<Contact>();

        static void Main(string[] args)
        {
            _contactList.Add(new Contact("selva", "rani", 45));
            _contactList.Add(new Contact("sandhu", "dhya", 20));
            _contactList.Add(new Contact("sasi", "kala", 19));
            _contactList.Add(new Contact("s2", "s3", 44));

            //You can do whatever you want with Console.WriteLine() here

        }
    }
}

this will work,

Add .ToArray() at the end of the return statement.

public List<string> FirstNames
    {
        get
        {
           return _contactList.Select(C => C.FirstName).ToArray();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM