none
Question on using .net object on powershell RRS feed

  • Question

  • Hi,

    Is there any difference between

    [datetime]::now

    and 

    $datetime=[Datetime]

    $datetime.now()

    Tuesday, November 24, 2015 6:14 PM

Answers

  • "dot" is instance member dereference operator.

    :: is the "type" static member dereference operator.


    \_(ツ)_/

    • Marked as answer by Moonshekar Thursday, November 26, 2015 5:20 PM
    Wednesday, November 25, 2015 7:06 PM
  • i am little confused with the use of "::" and "." with .net objects

    when should i use :: and when should i use "."

    if i do get-members of [datetime] , I don't see the property "now". How do i see that.

    See


    PS C:\> help about_Operators

    You will find the following description:


    :: Static member operator
        Calls the static properties operator and methods of a .NET
        Framework class. To find the static properties and methods of an
        object, use the Static parameter of the Get-Member cmdlet.
    

    The Now property is a static property of the [DateTime] class.


    PS C:\> [DateTime] | Get-Member -Static
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Moonshekar Thursday, November 26, 2015 1:22 PM
    Thursday, November 26, 2015 12:22 AM
    Moderator

All replies

  • Not really. They should compile down to the same CLR instructions as far as i'm aware.

    • Proposed as answer by Mike Laughlin Tuesday, November 24, 2015 6:25 PM
    Tuesday, November 24, 2015 6:17 PM
  • Hi,

    Is there any difference between

    [datetime]::now

    and 

    $datetime=[Datetime]

    $datetime.now()

    Yes.

    One works, the other doesn't.

    PS C:\> [datetime]::now
    
    Tuesday, November 24, 2015 1:22:36 PM
    
    
    
    PS C:\> $datetime = [datetime]
    
    PS C:\> $datetime.now()
    Method invocation failed because [System.RuntimeType] does not contain a method named 'now'.
    At line:1 char:1
    + $datetime.now()
    + ~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     

    If this is what you're really asking, then no.

    PS C:\> [datetime]::now.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                      
    -------- -------- ----                                     --------                                                                                                      
    True     True     DateTime                                 System.ValueType                                                                                              
    
    
    
    PS C:\> $datetime::Now.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                      
    -------- -------- ----                                     --------                                                                                                      
    True     True     DateTime                                 System.ValueType 


    Tuesday, November 24, 2015 6:24 PM
  • Hi,

    Is there any

    between

    [datetime]::now

    and 

    $datetime=[Datetime]

    $datetime.now()


    No other than a waste of typing.  There is not really any need to do that.

    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Tuesday, November 24, 2015 6:26 PM
    Tuesday, November 24, 2015 6:25 PM
  • What's provoking the question?

    -- Bill Stewart [Bill_Stewart]

    Tuesday, November 24, 2015 6:45 PM
    Moderator

  • i am little confused with the use of "::" and "." with .net objects

    when should i use :: and when should i use "."

    if i do get-members of [datetime] , I don't see the property "now". How do i see that.


    Wednesday, November 25, 2015 6:43 PM
  • "dot" is instance member dereference operator.

    :: is the "type" static member dereference operator.


    \_(ツ)_/

    • Marked as answer by Moonshekar Thursday, November 26, 2015 5:20 PM
    Wednesday, November 25, 2015 7:06 PM
  • i am little confused with the use of "::" and "." with .net objects

    when should i use :: and when should i use "."

    if i do get-members of [datetime] , I don't see the property "now". How do i see that.

    See


    PS C:\> help about_Operators

    You will find the following description:


    :: Static member operator
        Calls the static properties operator and methods of a .NET
        Framework class. To find the static properties and methods of an
        object, use the Static parameter of the Get-Member cmdlet.
    

    The Now property is a static property of the [DateTime] class.


    PS C:\> [DateTime] | Get-Member -Static
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Moonshekar Thursday, November 26, 2015 1:22 PM
    Thursday, November 26, 2015 12:22 AM
    Moderator
  • Thank you Bill that answered my question.
    Thursday, November 26, 2015 5:19 PM