• Home
  • About
    • Who are we ? photo

      Who are we ?

      Who are we?

    • Learn More
    • Twitter
    • Facebook
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Powershellで関数とクラスのreturnの違い

17 May 2017

Reading time ~1 minute

関数のreturn

  • 現在のスコープを抜ける
  • returnはあってもなくてもいい
  • return以外の他のオブジェクトも返す
function Test-Return
{
	'Hello'
	return 12
	'Good'
}

Test-Return
# Hello
# 12

クラスのreturn

  • 現在のスコープを抜ける
  • returnは必須(ない場合errorになる)
  • returnで指定されたものだけ返す
class ReturnTester
{
    [int32]TestReturn()
    {
        'Hello'
        return 12
        'Good'
    }
}

$return = New-Object -TypeName ReturnTester
$return.TestReturn()
# 12


powershell Share Tweet +1