Why does this compile?

F

Frank Rizzo

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 ConsoleApplication19
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            Child child = new Child();
            Console.WriteLine(child.SomeVariable.ToString());
        }
    }

    public class MyBase
    {
        public int SomeVariable;
    }

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

        }
    }
}
 
T

Thomas T. Veldhouse

Frank Rizzo said:
[-- 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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

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 ConsoleApplication19
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Child child = new Child();
Console.WriteLine(child.SomeVariable.ToString());
}
}

public class MyBase
{
public int SomeVariable;
}

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

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

Daniel

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?



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 ConsoleApplication19
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Child child = new Child();
Console.WriteLine(child.SomeVariable.ToString());
}
}

public class MyBase
{
public int SomeVariable;
}

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

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

Frank Rizzo

Thomas said:
Frank Rizzo said:
[-- 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 ConsoleApplication19
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Child child = new Child();
Console.WriteLine(child.SomeVariable.ToString());
}
}

public class MyBase
{
public int SomeVariable;
}

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

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

Mark Rae

base.SomeVariable = 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...
 
J

Jon Skeet [C# MVP]

Frank Rizzo said:
Thomas said:
Frank Rizzo said:
[-- 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.

// 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()
.DoSomethingElse()
.AndSomethingElse()

In particular, that's often seen with strings:

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

Frank Rizzo

Jon said:
Frank Rizzo said:
Thomas said:
[-- 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.

// 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()
.DoSomethingElse()
.AndSomethingElse()

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.
 
D

DeveloperX

Daniel said:
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?



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 ConsoleApplication19
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Child child = new Child();
Console.WriteLine(child.SomeVariable.ToString());
}
}

public class MyBase
{
public int SomeVariable;
}

public class Child : MyBase
{
public Child()
{
base.SomeVariable = 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.
 
F

Frank Rizzo

Daniel said:
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.
 
J

jen

DeveloperX said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top