0

Does the {0} represent the first argument "numerator" and {1} represent the second argument "denominator" passed to parameters of the writeLine method?

Another question is why does the method say argument #1 and #2 are objects? Integers are primitive data types.

I come from a Java background so the method description seems confusing.

using System;

    struct Fraction
    {
        public int denominator;
        public int numerator;

        public void Print()
        {
            Console.WriteLine("{0}/{1}", numerator, denominator);
            Console.ReadKey();
        }

    }

    public class structTest
    {
        public static void Main()
        {
            Fraction f; 
            f.numerator = 8;
            f.denominator = 6;

            f.Print();
        }
    }
Nicholas
  • 679
  • 2
  • 11
  • 29
  • 1
    All value types, including enums and nullable types, are derived from `System.Object`. http://stackoverflow.com/questions/436211/is-everything-in-net-an-object – Tim Schmelter Feb 12 '13 at 08:23
  • 1
    Comparing with Java, this works analogous to String.format() method in which u pass values for its placeholders – mihirj Feb 12 '13 at 08:24
  • Nice explanation, I read all your feedbacks! =] – Nicholas Feb 12 '13 at 08:30

3 Answers3

3

First question: yes.

Second question:

In C# primitives (int, double etc), they are simply aliases to .NET defined types. That's why they're not properly "primitives," since they're actually hiding standard classes and structs.

So int is alias for Int32 and so on. More here

gzaxx
  • 17,312
  • 2
  • 36
  • 54
2

Yes, the order of your arguments are according to your thoughts, because that overload of WriteLine accepts params object[], which means an array of object instances with the special case that the items can be passed as the last collection of arguments.

WriteLine accepts object with certain overloads, and in this instance, and that's because A) the types could well, and most often do, differ and aren't known a priori, and there are no generic overloads (which again, could just complicate things with multiple types), and B) it calls ToString on objects anyway and all things are objects(that matter) with an assumed reasonable implementation of that method.

There are overloads of the WriteLine method that explicitly accept int, bool, decimal, double etc., however.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1
  1. Yes.

    {0} is a reserve place to be filled with any value from the first argument, {1} for the second argument, and so on. List item

  2. In C# everything is object. Some are created from structure (such as int, double, etc) and many others are created from class. So, if you want, you can actually do something like this:

     int a = new int();
    

    and you can do something like this:

     string aString = a.ToString();
    
Budi Hartanto
  • 337
  • 3
  • 14
  • That's pretty neat. That is something I can do in C# but not in Java. – Nicholas Feb 12 '13 at 08:38
  • Nearly but not _everything_. Interface types are not derived from `System.Object`. Pointer types don't derive from `Object`. ["Open" type](http://stackoverflow.com/questions/2173107/what-exactly-is-an-open-generic-type-in-net) parameter types are also not Objects. – Tim Schmelter Feb 12 '13 at 08:41