Why does this compile?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Rizzo

    Why does this compile?

    Maybe I don't know all the c# quirks, but the code below should be compiling, but it does.  See the bolded code at the bottom.

    using System;

    namespace ConsoleApplicat ion19
    {
        class Class1
        {
         &nb sp;  [STAThread]
         &nb sp;  static void Main(string[] args)
         &nb sp;  {
         &nb sp;   &nb sp;  Child child = new Child();
         &nb sp;   &nb sp;  Console.W riteLine(child. SomeVariable.To String());
         &nb sp;  }
        }

        public class MyBase
        {
         &nb sp;  public int SomeVariable;
        }

        public class Child : MyBase
        {
         &nb sp;  public Child()
         &nb sp;  {
         &nb sp;   &nb sp;  base.Some Variable = 1;
         &nb sp;   &nb sp;  
         &nb sp;   &nb sp;  // why in the world does this compile???
         &nb sp;   &nb sp;  // note the space between the base and .SomeVariable
         &nb sp;   &nb sp;  base .SomeVariable = 2;    

         &nb sp;  }
        }
    }

  • Thomas T. Veldhouse

    #2
    Re: Why does this compile?

    Frank Rizzo <none@none.comw rote:
    [-- text/html, encoding 7bit, charset: ISO-8859-1, 44 lines --]
    >
    I don't know, as you posted HTML in a text newsgroup. Please repost you
    question in a format appropriate to the forum.

    Thank you.

    --
    Thomas T. Veldhouse
    Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Why does this compile?

      Frank,

      There is nothing in the specification that says that the period has to
      come as the character right after the expression and before the
      property/method. You can have as many spaces as you want.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Frank Rizzo" <none@none.comw rote in message
      news:OUNrsI%235 GHA.2104@TK2MSF TNGP06.phx.gbl. ..
      Maybe I don't know all the c# quirks, but the code below should be
      compiling, but it does. See the bolded code at the bottom.

      using System;

      namespace ConsoleApplicat ion19
      {
      class Class1
      {
      [STAThread]
      static void Main(string[] args)
      {
      Child child = new Child();
      Console.WriteLi ne(child.SomeVa riable.ToString ());
      }
      }

      public class MyBase
      {
      public int SomeVariable;
      }

      public class Child : MyBase
      {
      public Child()
      {
      base.SomeVariab le = 1;

      // why in the world does this compile???
      // note the space between the base and .SomeVariable
      base .SomeVariable = 2;
      }
      }
      }


      Comment

      • Frank Rizzo

        #4
        Re: Why does this compile?

        Thomas T. Veldhouse wrote:
        Frank Rizzo <none@none.comw rote:
        >[-- text/html, encoding 7bit, charset: ISO-8859-1, 44 lines --]
        >>
        >
        I don't know, as you posted HTML in a text newsgroup. Please repost you
        question in a format appropriate to the forum.
        Sorry. I was having line overruns, which is why I posted it in html.
        Here is the fixed up version.

        using System;

        namespace ConsoleApplicat ion19
        {
        class Class1
        {
        [STAThread]
        static void Main(string[] args)
        {
        Child child = new Child();
        Console.WriteLi ne(child.SomeVa riable.ToString ());
        }
        }

        public class MyBase
        {
        public int SomeVariable;
        }

        public class Child : MyBase
        {
        public Child()
        {
        base.SomeVariab le = 1;

        // why in the world does this compile???
        // note the space between the base and .SomeVariable
        base .SomeVariable = 2;
        }
        }
        }


        >
        Thank you.
        >

        Comment

        • Mark Rae

          #5
          Re: Why does this compile?

          "Frank Rizzo" <none@none.comw rote in message
          news:ecpmuk%235 GHA.4304@TK2MSF TNGP03.phx.gbl. ..
          base.SomeVariab le = 1;
          >
          // why in the world does this compile???
          // note the space between the base and .SomeVariable
          base .SomeVariable = 2;
          Why wouldn't it compile? Are you attributing some mystical significance to
          the white space...?

          Because C# certainly doesn't...


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Why does this compile?

            Frank Rizzo <none@none.comw rote:
            Thomas T. Veldhouse wrote:
            Frank Rizzo <none@none.comw rote:
            [-- text/html, encoding 7bit, charset: ISO-8859-1, 44 lines --]
            >
            I don't know, as you posted HTML in a text newsgroup. Please repost you
            question in a format appropriate to the forum.
            >
            Sorry. I was having line overruns, which is why I posted it in html.
            Here is the fixed up version.
            <snip>
            // why in the world does this compile???
            // note the space between the base and .SomeVariable
            base .SomeVariable = 2;
            Whitespace like that is fine. It's not uncommon to see:

            x.DoSomething()
            .DoSomethingEls e()
            .AndSomethingEl se()

            In particular, that's often seen with strings:

            x = x.Trim()
            .Replace ("\r", "\\r")
            .Replace ("\n", "\\n");

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Frank Rizzo

              #7
              Re: Why does this compile?

              Jon Skeet [C# MVP] wrote:
              Frank Rizzo <none@none.comw rote:
              >Thomas T. Veldhouse wrote:
              >>Frank Rizzo <none@none.comw rote:
              >>>[-- text/html, encoding 7bit, charset: ISO-8859-1, 44 lines --]
              >>>>
              >>I don't know, as you posted HTML in a text newsgroup. Please repost you
              >>question in a format appropriate to the forum.
              >Sorry. I was having line overruns, which is why I posted it in html.
              >Here is the fixed up version.
              >
              <snip>
              >
              > // why in the world does this compile???
              > // note the space between the base and .SomeVariable
              > base .SomeVariable = 2;
              >
              Whitespace like that is fine. It's not uncommon to see:
              >
              x.DoSomething()
              .DoSomethingEls e()
              .AndSomethingEl se()
              This doesn't compile on my box.
              >
              In particular, that's often seen with strings:
              >
              x = x.Trim()
              .Replace ("\r", "\\r")
              .Replace ("\n", "\\n");
              This does.
              >

              Comment

              • Frank Rizzo

                #8
                Re: Why does this compile?

                Frank Rizzo wrote:
                Jon Skeet [C# MVP] wrote:
                >Frank Rizzo <none@none.comw rote:
                >>Thomas T. Veldhouse wrote:
                >>>Frank Rizzo <none@none.comw rote:
                >Whitespace like that is fine. It's not uncommon to see:
                >>
                > x.DoSomething()
                > .DoSomethingEls e()
                > .AndSomethingEl se()
                >
                This doesn't compile on my box.
                Never mind, I misunderstood you. It, of course, compiles just fine.

                Comment

                • Daniel

                  #9
                  Re: Why does this compile?

                  Er.....who cares? The fact is it does, at a guess the compiler reads the
                  line and looks for the next character finds a '.' reads it as a combined
                  line and sees no issues.

                  i cant believe you are wasting your own time posting this and anybody elses
                  to ask such a question?



                  "Frank Rizzo" <none@none.comw rote in message
                  news:OUNrsI%235 GHA.2104@TK2MSF TNGP06.phx.gbl. ..
                  Maybe I don't know all the c# quirks, but the code below should be
                  compiling, but it does. See the bolded code at the bottom.

                  using System;

                  namespace ConsoleApplicat ion19
                  {
                  class Class1
                  {
                  [STAThread]
                  static void Main(string[] args)
                  {
                  Child child = new Child();
                  Console.WriteLi ne(child.SomeVa riable.ToString ());
                  }
                  }

                  public class MyBase
                  {
                  public int SomeVariable;
                  }

                  public class Child : MyBase
                  {
                  public Child()
                  {
                  base.SomeVariab le = 1;

                  // why in the world does this compile???
                  // note the space between the base and .SomeVariable
                  base .SomeVariable = 2;
                  }
                  }
                  }


                  Comment

                  • DeveloperX

                    #10
                    Re: Why does this compile?


                    Daniel wrote:
                    Er.....who cares? The fact is it does, at a guess the compiler reads the
                    line and looks for the next character finds a '.' reads it as a combined
                    line and sees no issues.
                    >
                    i cant believe you are wasting your own time posting this and anybody elses
                    to ask such a question?
                    >
                    >
                    >
                    "Frank Rizzo" <none@none.comw rote in message
                    news:OUNrsI%235 GHA.2104@TK2MSF TNGP06.phx.gbl. ..
                    Maybe I don't know all the c# quirks, but the code below should be
                    compiling, but it does. See the bolded code at the bottom.
                    >
                    using System;
                    >
                    namespace ConsoleApplicat ion19
                    {
                    class Class1
                    {
                    [STAThread]
                    static void Main(string[] args)
                    {
                    Child child = new Child();
                    Console.WriteLi ne(child.SomeVa riable.ToString ());
                    }
                    }
                    >
                    public class MyBase
                    {
                    public int SomeVariable;
                    }
                    >
                    public class Child : MyBase
                    {
                    public Child()
                    {
                    base.SomeVariab le = 1;
                    >
                    // why in the world does this compile???
                    // note the space between the base and .SomeVariable
                    base .SomeVariable = 2;
                    }
                    }
                    }
                    That's a bit snide. Any developer who didn't know that may be saved
                    time debugging someone elses code who did know it just by reading this
                    thread. Second any curiosity is good and should be encouraged. Third,
                    this works in C# but not in VB.net so for anyone converting from one to
                    the other, it would be confusing.
                    If I had to recruit either you or the OP I'd go with the OP simply
                    because he's curious and seeks a deeper understanding of his subject
                    which will serve him well in the future.

                    Comment

                    • Frank Rizzo

                      #11
                      Re: Why does this compile?

                      Daniel wrote:
                      i cant believe you are wasting your own time posting this and anybody elses
                      to ask such a question?
                      I can't believe you are wasting your own time and everybody elses
                      answering such a dumb question.

                      Comment

                      • jen

                        #12
                        Re: Why does this compile?


                        DeveloperX wrote:
                        If I had to recruit either you or the OP I'd go with the OP simply
                        because he's curious and seeks a deeper understanding of his subject
                        which will serve him well in the future.
                        i totally agree. i know that's what we look for.

                        cheers...

                        --
                        estroJen

                        Comment

                        Working...