0

I need to call throw ArgumentException on the list which stores name of company (name can exist once in current city) inside of a class City. How can I create a list of names and throw the exception if I have a list of names?

class City : ICity
{
    private List<string> _companyNames;
    internal City(string name)
    {
        this.Name = name;
        _companyNames = new List<string>();
    }
    public string Name
    {
         get;  
    }

    public ICompany AddCompany(string name)
    {

        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException("invalid name");
        }

        //create a list and check if exist
        List<string> _companyNames = new List<string>() {name, name, name};
        //public bool Exists(Predicate<T> match);
        //Equals(name) or sequennceEqual
        if (!_companyNames.Equals(obj: name))
        {
            throw new ArgumentException("name already used");
        }


        return new Company(name, this);
    }
}
dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • On what condition you want to throw exception ? What is the reason of that ? Explain bit more please – Ali Beyit Dec 16 '19 at 13:25
  • Use Linq `if (_companyNames.Any(s => s == name)) throw new Exception()` – panoskarajohn Dec 16 '19 at 13:27
  • change if statement as -> if(_companyNames.Contains(name)){...} – ycansener Dec 16 '19 at 13:27
  • Not sure I understand your question. Are you saying you want to throw an exception if the list of names already exists i.e. is identical or the name already exists in the list you have? – sr28 Dec 16 '19 at 13:29

2 Answers2

3

Don't use a List<string> for uniqueness-checking. It will become less efficient as the list will grow. Consider using a HashSet<string> for that.

class City
{
    private readonly HashSet<string> _companyNames = new HashSet<string>();

    public ICompany AddCompany(string name)
    {
        // check 'name' for null here ...
        // ...

        // 'Add' will return 'false' if the hashset already holds such a string
        if (!_companyNames.Add(name))
        {
            throw new ArgumentException("Such a company already exists in this city");
        }

        // ... your code
    }
}
dymanoid
  • 14,771
  • 4
  • 36
  • 64
0

If you want the Add itself to throw the exception, one thing you could do is to create your own implementation. Something like the following:

public class MyList<T> : List<T>
{
    public new void Add(T item)
    {
        if (Contains(item))
        {
            throw new ArgumentException("Item already exists");
        }
        base.Add(item);
    }
}
Shahzad
  • 2,033
  • 1
  • 16
  • 23
  • Hiding the public `Add` method is a very unreliable solution here. `List myList = new MyList(); myList.Add("foo");` won't throw. – dymanoid Dec 16 '19 at 13:43
  • The use of generic is not OK here you are not sure what T is. You assume T is a string. Although this answers the question is not a good suggestion, without elaborating. – panoskarajohn Dec 16 '19 at 13:46
  • @dymanoid Yes, I agree it wouldn't work if the reference type is not `MyList`. Your solution is a more complete one. – Shahzad Dec 16 '19 at 13:52
  • @panoskarajohn I'm not sure what you mean. I don't think my code is making an assumption of T being a string. Can you please give a code example – Shahzad Dec 16 '19 at 13:55
  • You should mention that in your answer. The readers might not know this and might choose your answer and thus get into trouble. – dymanoid Dec 16 '19 at 13:55
  • @ShahzadB -> https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.contains?view=netframework-4.8 . Please note on my previous comment `without elaborating`. As you can see in the microsoft docs, For the class that showcases the Example the `Equals method is overriden`. This is what i mean. Your generic is not fully implemented. What if i wanted to use it with a more complex object? – panoskarajohn Dec 16 '19 at 13:58
  • 2
    [Why not inherit from List?](https://stackoverflow.com/q/21692193/982149) – Fildor Dec 16 '19 at 14:38