Writing the first 3 letters of a list <> in C #

starting with C ++, now studying C # and just want to know why this code does not work, there are just a lot of errors in the output, I saw a few more examples from Microsoft documents and they use something called var(in particular, when using foreach) , so when I tried to use it, VS tells me that the data type vardoes not exist (maybe a missing library?) is because the reason it .Skip()doesn't work? so what should i use var? just for writing the first three letters of the stack? I really cannot say what is wrong. In fact, what I do for me is stupidity. Any help is appreciated.

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

namespace Calendar
{
    class days
    {
        private List <int>    numbers_of_days;
        private List <String> names_of_days;

        public days()
        {
            numbers_of_days = new List<int>();
            names_of_days = new List<string>
                            { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" };

            for (int i = 0; i < 31; i++)
            {
                numbers_of_days.Add(i);
            }

        }

        public void print_days()
        {
            foreach (string day in names_of_days)
            {
                Console.Write(" " + day.Skip(3));
            }
            Console.Writeline();
        }
    }
}
+4
2

print_days , :

    public void print_days()
    {
        foreach (string day in names_of_days)
        {
            foreach (char c in day.Skip(3))
            {
                Console.Write(c);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }

IEnumerable<char> , . 3 :

    public void print_days()
    {
        foreach (string day in names_of_days)
        {

            Console.WriteLine(day.Substring(0,3));
        }
        Console.WriteLine();
    }
+4

@S.Petrosov.Substring ( , 2 ... , answer , )

var, var .

        class days
        {
            // numbers_of_days removed from class scope to constructor scope (to demonstrate var)
            // you can't use var to declare a private member
//            private var not_valid = "compile error";
            private List<String> names_of_days;

            public days()
            {
                // when using var to declare a variable, the type is derived by the compiler from the right side of the declaration
                var numbers_of_days = new List<int>();
                names_of_days = new List<string>{ "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" };

                for (int i = 0; i < 31; i++)
                {
                    numbers_of_days.Add(i);
                }
            }

            public void print_days()
            {
                foreach (string day in names_of_days)
                {
                    Console.Write(" " + day.Substring(0, 3));
                }
                Console.WriteLine();
            }
        }
0

Source: https://habr.com/ru/post/1673259/


All Articles