Code Assessment

Hi everyone! I'm new to Programmers' Heaven and programming. I took the intiative recently to teach myself C# programming. Currently I'm reading the book "Microsoft C Sharp Programming for the Absolute Beginner" by Andy Harris. I've found the book easy to read and follow. But I started experimenting with the code presented in the first chapter and I've run into bugs already! Every time I compile I get the error message, "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." while using string interpolation. I've posted the code below (I'm using Visual C# 2008 express edition). The code is for a simple program I created for practice with variables, strings, and the console class. If someone could please explain to me what the problem is and how I can fix it, I'd be grateful. Also, if any of you have any tips or resources for a completely new programmer like me, that would be appreciated as well. Thank you! :D


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

namespace Console_Quote_Application
class Program
static void Main(string[] args)
string Quote;
string Author;

Console.Write("Please enter your favorite quote without quotes around it: ");
Quote = Console.ReadLine();
Console.Write("Now please enter the name of the person responsible for the quote: ");
Author = Console.ReadLine();
Console.Write("{1} once said "", Author);
Console.Write("{0}"", Quote);


  • in the statement
    Console.Write("{1} once said "", Author);

    you are doing something called a string format. Everywhere you have a number inside of {} one of the arguments after the initial string will be inserted.

    The number inside the brackets refers to the arguments that follow the string. The index referencing is what is called "Zero Based" which means that the first argument is actually at index 0, the second argument is at index 1, third is at index 2, and so on...

    In your statement you are asking to have the second argument inserted into your string "{1}", but you are only providing one argument. If you change this to {0} then it will use the first argument that you provide.
  • Thank you for your response DataDink.

    So just so I get this straight I have to number according to when I introduce the strings into the Console.WriteLine, and not according to in what order I wrote, "string Quote" and "string Author"?
  • Ok so I just experimented with the code a bit and now I completely understand what you said. The code now works! Thanks! :D
  • Just another question; Could you please explain to me what arguments and index referincing are from a technical standpoint? Thank you.
  • well the "Console.WriteLine" method you are using actually takes all of the arguments you pass it (other than the format string) and puts them all into an array. Arrays are just a collection of items. Each item in an array can be individually referenced using an index, but in .net world the indexes start with 0 instead of 1. This is common in most languages because 0 is actually the lowest non-negative number, not 1.

    So let's say you were writing the Console.WriteLine method it would look something resembling this...

    public static void WriteLine(string format, params object[] args)
    // the "params" keyword allows all parameters to be inserted
    // into an array. This allows a variable number of arguments
    // to be handled by a single method definition.

    // So here we have the args array that contains each of the items
    // you want inserted into your console output.

    // We are going to loop through each of the objects passed and replace
    // its respective index reference in the format string.

    for (int i = 0; i < args.Length; i++)
    // This is what we will be replacing in the string - e.g. {0}, {1}, etc...
    string indexRef = "{" + i.ToString() + "}";

    // This is what we will be replacing it with...
    string indexItem = args[i].ToString();

    format = format.Replace(indexRef, indexItem);

    // Great - now that all of the items in the "args" array have been
    // inserted, we can print the finished string to the console...
  • Thank you.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!