Hello,

I am trying to write a simple program which should read the first line from the file s1,
and display it. When I run it , I get the message

cannot implicitly convert 'System.IO.Streamreader 'to 's1.Program'
which happens for the line Program re = new StreamReader("s1.txt");

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

namespace s1
{
    class Program
    {
        static void Main(string[] args)

        {
            Program re = new StreamReader("s1.txt");

            s = re.Readline();

            System.Console.WriteLine(s);
            System.Console.WriteLine("Hello, World!");


        }
    }
}

Dani AI

Generated

Brief summary and fixes (for ): the compile error came from assigning a StreamReader to a variable declared as your Program class — they are different types. already pointed that out. The runtime output of "System.String[]" happened because you passed a string[] to Console.WriteLine, and that prints the array’s type name, not its contents.

A few practical points you can apply right away:

  • To print the whole file, join the lines: use string.Join(Environment.NewLine, ...) or enumerate the array.
  • Indexing a string like someLine[0] returns a char (the first character). To get the first word, split the line; to get the last word, take the last element of the split result.
  • Use a split that ignores extra spaces/tabs to avoid empty entries: supply a char[] and StringSplitOptions.RemoveEmptyEntries.

A minimal, robust pattern (avoids loading the entire file and handles blank lines):

using System;
using System.IO;
using System.Linq;

class Example
{
    static void Main()
    {
        var path = "s1.txt";
        var firstLine = File.ReadLines(path).FirstOrDefault();
        if (firstLine == null) return;
        var words = firstLine.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        var firstWord = words.Length > 0 ? words[0] : "(none)";
        var lastWord  = words.Length > 0 ? words[words.Length - 1] : "(none)";
        Console.WriteLine(firstWord);
        Console.WriteLine(lastWord);
    }
}

Troubleshooting tips: check the file path and file existence, Trim() words to remove punctuation if needed, and guard against empty files or lines to avoid IndexOutOfRange. ’s StreamReader approach is fine for small samples; for large files prefer File.ReadLines or a using StreamReader to avoid leaking resources.

Recommended Answers

All 20 Replies

Program is a class, StreamReader is a constructor that constructs a class of type StreamReader. The Program class is not a StreamReader class.
After you figure that out, you'll get an error in line 4 :)

One great benefit of this site is the great number of questions that have already been answered that you can search through.
If you were to use the word StreamReader in the search window, you would probably get a multitude of examples.

Hmm, here is much work to be done.
You are really messing things.
This code does not even complie.

To do your simple program, you can do:

class Program
{
    static void Main(string[] args)
    {
        string filePath = @"C:\YourFolder\testFile.txt";
        StreamReader sr = new Streamreader(filePath);
        string line = "";
        for (int i = 0; i < 1; i++)
            line = sr.ReadLine();
        sr.Dispose();
        Console.WriteLine(line);
        Console.ReadLine();            
    }
}

thank you for all the suggesions.

I made the change but, C# still got the error.
to go around it I have changed the code abit.
Now I am getting System.String[] when i run it.

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

namespace s1
{
    class Program
    {
        static void Main(string[] args)

        {

            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Documents\Visual Studio 2010\Projects\store1\st1\input_1.txt");
            System.Console.WriteLine(lines);



        }
    }
}

ok, it seems that C# can only read some lines from the file.

Now I am trying to figure out how to get the first word, from the first line.
for example if my line is rock your body 1233

how can I get the first word rock or the last word 1233 ?

I tried

System.Console.WriteLine(lines[0][0]);

but that will give the first letter from the first word

I also tried this way. but

for some reason the line s = line.Split(" "); seems not to work

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

namespace s1
{
    class Program
    {
        static void Main(string[] args)

        {

            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Documents\Visual Studio 2010\Projects\store1\s1\s1.txt");
            System.Console.WriteLine(lines);
            System.Console.WriteLine(lines[0]);
            System.Console.WriteLine(lines[0][0]);

            foreach (string line in lines)
            {

               string s  = " ";
               s = line.Split(" ");
               System.Console.WriteLine(s);
            }





        }
    }
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.