Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,
I'm learning c# but i have a problem now. As you see, i have a list name which is "notlar". When someone press "2" i want average of list items. How can I do " +listitem1+listitem2". After that i want to write it to int sonuc

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrtalamaHesabı
{
    class Program
    {
        static void Main(string[] args)
        {
           int  secenek, devam = 1;
           List<int> notlar = new List<int>();
           int sonuc = 0;

            while (devam == 1)
            {
                Console.WriteLine();

                Console.WriteLine("Add number ==>");
                notlar.Add(int.Parse(Console.ReadLine()));

                Console.WriteLine("Add number->1 ,Take arithmatic average ->2, Exit ->3");

                secenek = int.Parse(Console.ReadLine());

                if (secenek == 3)
                    devam = 0;
                if (secenek == 2)
                {

                }
            }
        }
    }
}
Posted
Updated 16-Mar-11 13:38pm
v4

To add all list items:
C#
using System.Linq;

//...

sonuc = notlar.Sum();
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 20:07pm    
I voted 5 but took a liberty to add the first line in your sample to avoid possible misunderstanding or down-votes. In this form, it makes the best answer. Would you mind?
--SA
JOAT-MON 16-Mar-11 20:11pm    
Thank you, SA, I think it makes the answer more complete. I hadn't even thought about specifying the 'using' statement since the OP had tagged with C#4.0, which automatically adds it in VS. Good revision.
Sergey Alexandrovich Kryukov 16-Mar-11 20:41pm    
You see, for a user of C# before Linq, lambda and extension methods (and most users are still there) this Sum call looks completely mysterious. Four "using" lines in Question would not help: which is relevant. If it was more trivial, I would not bother.
--SA
If you had used an internet search engine with a suitable phrase, you would not have needed to ask this question.

Do a Search on c# list<> average. You will get lots of useful possibilities.
 
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