0

Hi I'm trying to add some data to my list including string and integer. I want to store student's name , last name and phone number in the same list so I'm using class. here is my code

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


 namespace educationsystem
{
   public class student 
        {
            public string name { get;set;}
            public string lastname {get;set;}
            public long phone {get;set;}
        }
           List<student> Students = new List<student>();
           Students.Add(new student {name= "mahta",lastname= "sahabi",phone= "3244"});
class Program
{

    static void Main(string[] args)
    {


        Console.WriteLine("please choose one of the numbers below : "+"\n");
        Console.WriteLine("1.adding new student"+"\n");
        Console.WriteLine("2.adding new course"+"\n");
        Console.WriteLine("3.adding new grade"+"\n");
        Console.WriteLine("4.showing the best student"+"\n");
        Console.WriteLine("5.getting students average"+"\n");
        Console.WriteLine("6.exit"+"\n");
         string input = Console.ReadLine();


             if (input == "1")
            {
                Console.Clear();
                List<int> grades = new List<int>();

                    Console.WriteLine("please enter the students name");
                    string name = Console.ReadLine();
                    Console.WriteLine("please enter the students last name");
                    string lastname = Console.ReadLine();
                    Console.WriteLine("please enter the students phone number");
                    long phone = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine(name +" "+ lastname +" " + phone);





            }
            else if (input == "2")
            {

            }
            else if (input == "3")
            {

            }
            else if (input == "4")
            {

            }
            else if (input == "5")
            {

            }


        Console.ReadKey();
}

 }
       }

but it is not working. My list name is students but the system wouldn't recognize it. would you help me please?

1
  • which list Students or grades? Commented Oct 19, 2016 at 9:21

1 Answer 1

2

You should place your list inside your class Making variables global is not possible in c#. global variables can be done in languages like PHP and c++

class Program
{
    public List<student> Students = new List<student>();
}

If you want to make students global accesible from your class you could make it static

public static List<student> Students = new List<student>();
Sign up to request clarification or add additional context in comments.

4 Comments

If my answer solved your problem, click the big checkbox to accept it as the answer.
how should I print my list in output?
Students.ForEach((student) => { Console.WriteLine($"name = {student.name}\nlastName {student.lastname}\nphone = {student.phone}"); });
you're the best

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.