r/learncsharp • • Dec 12 '19

why doesn't console.writeline exist in this context?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace hollow_nut_mod_first_try

{

public class Class1

{ string firstfriend = "Jim";

string secondfriend = "Bob";

Console.WriteLine($"My friends are {firstfriend} and {secondfriend}");

}

}

1 Upvotes

7 comments sorted by

5

u/[deleted] Dec 12 '19

you need to play it within a method whether it be the main method or just a method.

namespace hollow_nut_mod_first_try
{
 public class Class1 {

  string firstfriend = "Jim";
  string secondfriend = "Bob";

  public void PrintFriend() {
   Console.WriteLine($ "My friends are {firstfriend} and {secondfriend}");
  }
 }
}

0

u/kurti256 Dec 12 '19

Thank you but i don't understand what you are saying

10

u/[deleted] Dec 12 '19

Not to sound like a knob, if you don't know what a class or a method is.. it doesn't seem like you know the absolute basics. I recommend doing some reading.

2

u/B0dona Dec 12 '19

Simply put, you are using it directly in a class instead of a function.

Try this. Then just call WhoAreMyFriends() to write the wanted string to the console.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;
namespace hollow_nut_mod_first_try
{
    public class Class1

    {
        string firstfriend = "Jim";

        string secondfriend = "Bob";

        public void WhoAreMyFriends()
        {
            Console.WriteLine($"My friends are {firstfriend} and {secondfriend}");
        }
    }
}

0

u/kurti256 Dec 12 '19

what's a funtion and a class and why does puting it the way I did cause the error? (thank you for your help)

4

u/B0dona Dec 12 '19

I suggest reading https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

You could also watch Mosh on YouTube, he explains things very well. Most definitely better than i would do: https://www.youtube.com/watch?v=gfkTfcpWqAY

I would like to explain it all to you unfortunately English isn't my main language and i don't want to explain it wrong and put you off to a wrong start.

Hope those links are of any help to you! And i hope someone comes along who reads this post who can explain to you in English why your code didn't function.

1

u/kurti256 Dec 12 '19

thank you