Skip to content

Class : Methods and properties can't have the same name #8235

Closed

Description

@AlexBillon07

Steps to reproduce

class TestClass {
    [string] hidden $prop

    [string]Prop() {
        return $this.prop
    }
}

$obj = [TestClass]::new()

Expected behavior

Get an error saying you can't have a property and a method with the same name

Actual behavior

PS C:\EDICore> $obj | Get-Member


   TypeName: TestClass

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
Prop        Method     string Prop()
ToString    Method     string ToString()


PS C:\EDICore> $obj.Prop()
Method invocation failed because [TestClass] does not contain a method named 'Prop'.
At line:1 char:1
+ $obj.Prop()
+ ~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 6.1.7601 S
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. iSazonov commented on Nov 13, 2018

    @iSazonov
    Collaborator

    /cc Rob Holt (@rjmholt) For your collection.

  2. added
    Issue-Bug
    WG-Engine
    on Nov 13, 2018
  3. rjmholt commented on Nov 13, 2018

    @rjmholt
    Collaborator

    C# disallows this; try compiling the following:

        class Program
        {
            static string Main { get; } = "Hi";
            
            static void Main(string[] args)
            {
                Console.WriteLine($"{Main} World!");
            }
        }

    From The C# Language Specification - ECMA 334:

    17.2 Class members

    The name of a constant, field, property, event, or type must differ from the names of all other members declared in the same class.

    That doesn't necessarily mean PowerShell must also, although I think it's indicative.

    The Common Language Infrastructure Specification - ECMA 335 has the following to say:

    I.10.2 Overloading

    ...
    Following the rules of the CTS, it is possible for duplicate names to be defined in the same scope
    as long as they differ in either kind (field, method, etc.) or signature.
    ...

    CLS Rule 5

    All names introduced in a CLS-compliant scope shall be distinct independent of
    kind, except where the names are identical and resolved via overloading. That is, while the CTS
    allows a single type to use the same name for a method and a field, the CLS does not.

    I think it's fair to say that CLS compliance is no longer a consideration. But, given that C# doesn't allow this, I strongly suspect that the current behaviour is by design. Maybe Bruce Payette (@BrucePay) or Jason Shirk (@lzybkr) can shed some light there?

  4. rjmholt commented on Nov 13, 2018

    @rjmholt
    Collaborator

    The fact that the method Prop() is actually registered looks like a bug though -- we should probably fix that and turn this into a specific parse-time error (e.g. Multiple class members with the same name are not allowed ☝️).

  5. rjmholt commented on Nov 13, 2018

    @rjmholt
    Collaborator

    /cc @PowerShell/powershell-committee for discussion

  6. mklement0 commented on Nov 13, 2018

    @mklement0
    Contributor

    Rob Holt (@rjmholt): +1 on the parse-time error.

    Allowing members of different types to have the same name is asking for trouble with no obvious benefit (and wer're not breaking anything, given that the method cannot be called).

    Note that C# does allow names that differ in casing, which we should disallow as well, given PowerShell's case-insensitive nature.

  7. added
    Review - Committee
    on Nov 14, 2018
  8. added a commit that references this issue on Nov 24, 2018
    bfd4be7
  9. IISResetMe commented on Nov 24, 2018

    @IISResetMe
    Collaborator

    Michael Klement (@mklement0) Rob Holt (@rjmholt) looks like this is fairly straightforward to catch at parse-time: IISResetMe@bfd4be7

    image

  10. mklement0 commented on Nov 25, 2018

    @mklement0
    Contributor

    Cool, Mathias R. Jessen (@IISResetMe), thanks for taking this on.

    Sounds like Jason Shirk (@lzybkr) is already advising you on the implementation (which I'd be ill-equipped to do anyway).

    One suggestion re the user-facing part: How about mimicking the wording of the C# error message, so as to include the type name too?

    The type 'myclass' already contains a definition for 'A'
    

    Though spelling out method or property can't hurt:

    The type 'myclass' already contains a definition for method or property 'A'
    
  11. IISResetMe commented on Nov 27, 2018

    @IISResetMe
    Collaborator

    Michael Klement (@mklement0) moving the mutual check back to TypeDefiner (DefineProperty() and DefineMethod() respectively) makes it way faster and avoids attempts at generating invalid/unusable types should someone pass in an AST generated by a different parser (or by hand)

    IISResetMe@5404f94#diff-640a7359770582f83b945ef2a276d2de

  12. SteveL-MSFT commented on Jan 23, 2019

    @SteveL-MSFT
    Member

    The PR was already reviewed by @PowerShell/powershell-committee so updating label #8372 (comment)

  13. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Jan 23, 2019
  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  16. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  17. added
    Resolution-No Activity
    on Nov 16, 2023
  18. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  19. added
    In-PR
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedIn-PRIssue-BugResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions