Reputation: 37
So I am making a super basic (I'm in my second week of learning C# so please excuse my ignorance) program that takes a string input from a user and outputs the string backwards. I have copied the book to a T in regards to a majority of it but I have noticed spelling errors in some of their code so I don't have a lot of faith in what they are showing. My compiler is giving me an error with WriteLine and ReadLine and I don't understand why as the book says it works. This is the error I am getting;
"WriteLine does not exist in the current context" same with "ReadLine"
My code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class funcStrings
{
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
class runProgram
{
class Program
{
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
}
}
}
Thanks for any guidance here
Upvotes: 0
Views: 119
Reputation: 750
Currently it is:
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
You need to add a Console.WriteLine("Enter your name to be reversed"):
Upvotes: 0
Reputation: 299
You can add such namespace:
using static System.Console;
This brings all the static members from the System.Console
class into scope, so that you don't need to prefix them with Console.
It is a C# 6 feature, and useful when accessing many members in a static class. See relevant documentation.
Upvotes: 4
Reputation: 112
The problem is probably that it should be Console.WriteLine
and Console.ReadLine
.
Upvotes: 2
Reputation: 1808
You just need to add Console.
in front:
Console.WriteLine("Enter your name to be reversed ");
name = Console.ReadLine();
Upvotes: 4