C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: Code Assessment Posted by DataDink on 17 Aug 2009 at 1:28 PM
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...
    WriteLine(format);
}

Thread Tree
Legend457 Code Assessment on 17 Aug 2009 at 10:56 AM
DataDink Re: Code Assessment on 17 Aug 2009 at 11:06 AM
Legend457 Re: Code Assessment on 17 Aug 2009 at 11:14 AM
Legend457 Re: Code Assessment on 17 Aug 2009 at 11:16 AM
Legend457 Re: Code Assessment on 17 Aug 2009 at 11:20 AM
DataDink Re: Code Assessment on 17 Aug 2009 at 1:28 PM
Legend457 Re: Code Assessment on 17 Aug 2009 at 7:34 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.