4

comming from C++ now learning C# and just want to know why this code doesn't work, the output is just a lot of errors, I saw another examples from the Microsoft docs and they use something called var (specificly when using foreach) so when I tried to use it, VS tells me the var datatype doesn't exists(maybe a missing library?) is that the reason why .Skip() does not work? so Should I use var? just for writing the first 3 letters of the stack? I really can't tell what is wrong. Actually that suposition I'm making for me is nonsense.. 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();
        }
    }
}
Egon Stetmann.
  • 461
  • 6
  • 14

2 Answers2

4

You need to change you print_days function to the following to get the result you expect:

    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();
    }

Skip function returns IEnumerable<char> in your case and you need to iterate one more time on it. For getting first 3 letters only you need to do the following:

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

            Console.WriteLine(day.Substring(0,3));
        }
        Console.WriteLine();
    }
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • 3
    It might also be worth noting that `Skip` does not get the first three characters, it literally *skips* them and returns the rest of the string. To get the first 3 characters, you would use `Take` instead of `Skip`. – Rufus L Mar 27 '17 at 03:37
  • the second code worked like a charm, thanks, I saw so many C# examples that I left the guard and though the programming language will handle a string of strings. – Egon Stetmann. Mar 27 '17 at 03:39
0

As per @S.Petrosov, .Substring will get you the first three characters (use with caution as a 2 character string will throw an exception...no issue with the given list, but this answer has a solution when the lengths are uncertain)

This answer gives the best descriptions on var, below gives some extra tips on var relative to your sample.

        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();
            }
        }
Community
  • 1
  • 1
wallismark
  • 1,766
  • 2
  • 21
  • 35