Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2016
    Posts
    14

    C# basic question

    Grettings,

    Could someone please help me understand this very basic syntax issue regarding C# please?

    Here is some ridiculously basic C# code:

    Code:
    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            int myIndex = 0;
    
            while (myIndex < 10)
            {
                Console.WriteLine("myIndex value is: " + myIndex);
                myIndex++;
            }
    
            Console.ReadKey();
        }
    }
    In one of the C# tutorial videos I noticed that the main line whithin the while loop was put in the following way:

    Code:
    Console.WriteLine("myIndex value is: {0}", myIndex);
    and the code worked.

    How does this work? What is this {0} ?? Please help in understanding.
    Last edited by goldeen; 09-24-2016 at 03:17 AM.

  2. #2
    Join Date
    Oct 2011
    Posts
    547
    it is a nice way to format strings. instead of writing
    ("My ice cream costs: " + costVariable + "dollars and my cone is: ” + coneType). you would write "My ice cream costs: {0} dollars and my cone is: {1}", costVariable, coneType). no plus signs needed. the variables are easier to spot in the wall of text. Each number declared is expected. if you gave {0},{1},...,{n} in the string then the coresponding variables for those numbers are expected. in the example above 2 are declared so {0},{1} corresponds to costVariable and coneType.

  3. #3
    Join Date
    Aug 2010
    Posts
    158
    While florem is correct that it is a nice way to write strings, it does much more then that. It uses an internal string builder to save time and memory. Can be used to aid in local formatting of text based on language and region and such. Can be used to make easily templatized strings.

    The confusing this to realize is that the 2nd variable in this case is actually a literal array of strings called an arg[]. Even though it might look like many arguments the compiler creates an array out of the last arguments. string.Format("a string" , an, array, of, items, gets, put, into, an, arg[])

    It is a expensive task to concatenate strings. Remember that string are immutable (they can't be changed). This means that when you are "adding" two strings together you are taking the length of the array of chars of string 1 plus the length of the array of chars from string 2, creating a new char array with that length and inserting each character into the appropriate place. This means multiple memory allocations for larger strings.

    If you take a long string ("a" + " " + "b" + " " + "c") it means that you do this operation multiple times creating a new array.
    "a" + " " + "b" + " " + "c" // 5 blocks of memory to start with a new allocation for each concatenation.
    "a " + "b" + " " + "c"
    "a b" + " " + "c"
    "a b " + "c"
    "a b c" // this took 9 allocations of memory to get here. The initial 5 plus an additional allocation for each concatenation.

    string format replaces the markers with the 0 based index of the args[] and calls the ToString() method on it. This means you could put other types in there and it would automatically call the toString() for you.

    string myString = "world";

    Console.WriteLine("Hello " + myString); // String concatenation

    StringBuilder sb = new StringBuilder();
    sb.Append("Hello");
    sb.Append(myString);
    Console.WriteLine(sb.ToString()); // String builder

    Console.WriteLine(string.Format("Hello {0}", myString); // string format
    Console.WriteLine($"Hello {myString}"); // C#6 string interpolation replaces the need for string.Format()

    All of those would print "Hello world". Use C#6 version as it is clean and the easiest to understand. Just make sure to put a $ before the string.

  4. #4
    Join Date
    Oct 2011
    Posts
    547
    Thank you for your more in-depth look into the matter. Cheers.

  5. #5
    Join Date
    Sep 2016
    Posts
    0
    nice one have fun buddy

  6. #6
    Join Date
    Sep 2016
    Posts
    14
    I'd like to thank you both for the clarification.

    By the way, I noticed that only one of the methods accepts string[], while both accept string.

    P.S. A note to the admin: I've only just noticed that I posted this under "MMO Programming" rather than under plain Programming. If this thread needs to be moved, please do so.

  7. #7
    Join Date
    Oct 2016
    Location
    United States
    Posts
    0
    place holder
    My signature is assignment service and this job is my passion.

  8. #8
    Join Date
    Nov 2016
    Posts
    0
    am385, Thank U for useful information

  9. #9
    Join Date
    Dec 2016
    Location
    london
    Posts
    0
    Great information you have share this help many people. Thank you for this.
    Help for Dissertation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •