Cobra/Keywords/using: различия между версиями

Содержимое удалено Содержимое добавлено
м Замена <tt /> на <code />; избыточные <big /> и <font /> вокруг <source />; {{BookCat}}; пробелы.
м <source> -> <syntaxhighlight> (phab:T237267)
 
Строка 6:
В использовании блока, объект только для чтения и не может быть изменены или переназначен.
== Синтаксис ==
<sourcesyntaxhighlight lang="python">
using <name> = <expression>
<statements>
</syntaxhighlight>
</source>
== Пример ==
<sourcesyntaxhighlight lang="python">
using font1 = new Font("Arial", 10.0f)
charset = font1.GdiCharSet
# do domething with charset
# font1 disposed of (and out of scope) here
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="python">
using f = File.createText(scriptName)
f.writeLine('#!/bin/sh')
f.writeLine('echo "Running a shell script made from cobra-[CobraCore.version]" "$@"')
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="python">
using resultsWriter = File.appendText(resultsFileName)
print to resultsWriter, 'Results of Run [date]'
.printTotals(resultsWriter to !)
</syntaxhighlight>
</source>
Вы можете достичь аналогичного результата внутри блока try и вызова .dispose в блоке finally.
<sourcesyntaxhighlight lang="python">
font1 = new Font("Arial", 10.0f)
try
Строка 35:
finally
if font1, (font1 to IDisposable).dispose
</syntaxhighlight>
</source>
 
{{BookCat}}