Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It's need me count amount of C# programmers through function.For example i have Developer arrays`

Developer[] d = new Developer[5]
{
new FrontEndDeveloper("Hakob", "Hakobyan", 23, 2.5, "C#"),
new BackEndDeveloper("Petros", "Petrosyan", 25, 5, "Python"),
new FullStackDeveloper("Poghos", "Poghosyan", 30, 8, "C#"),
new BackEndDeveloper("Valod", "Aramyan", 24, 3.5, "JavaScript"),
new FrontEndDeveloper("Sanasar", "Araqelyan", 26, 4, "C#"),
};

I must have function in Developer Class, which name is AmountofC#Programmers();
When i will call this function in Main Method, it will print "There are 3 C# Programmers"

What I have tried:

I tried like this`

Class Developer

public void AmountofC#Programmers()
{
int amount = 0;
if(languages == "C#")
{
amount++;
Console.WriteLine("There are {0} C# Programmers",amount);
}

}

static void Main()

Developer[] d = new Developer[5]
{
new FrontEndDeveloper("Hakob", "Hakobyan", 23, 2.5, "C#"),
new BackEndDeveloper("Petros", "Petrosyan", 25, 5, "Python"),
new FullStackDeveloper("Poghos", "Poghosyan", 30, 8, "C#"),
new BackEndDeveloper("Valod", "Aramyan", 24, 3.5, "JavaScript"),
new FrontEndDeveloper("Sanasar", "Araqelyan", 26, 4, "C#"),
};

foreach (Developer item in d)
{
item.AmountofC#Programmers();
}

But it's not working.
Posted
Updated 19-Jan-18 22:18pm

You can keep track of a counter variable, which you increase when you come across a C# programmer while iterating over all items in the array. This would be your loop:
C#
int counter = 0;

foreach (Developer item in d)
{
    if (item.Language == "C#") // change 'Language' into the correct identifier
    {
        counter++;
    }
}

Console.WriteLine("There are {0} C# programmers", counter);

The way you try to do it wouldn't work: aside from the fact that you cannot put '#' in method names, the Developer class itself isn't the right place to put an AmountOfCSharpProgrammers method, because the Developer class only has information about one developer, whereas for this counter functionality, you need the information of the full array of developers.
 
Share this answer
 
Comments
Suren97 16-Jan-18 15:38pm    
Thank you very much!
Thomas Daniels 16-Jan-18 15:50pm    
You're welcome!
Ahem...


Console.WriteLine($"There are {d.Count(x=>x.Language=="C#")} C# programmers");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900