DeğişkenlerVariables

Değişkenler, depolama konumlarını temsil eder.Variables represent storage locations. Her değişken, değişkende hangi değerlerin depolanabileceğini belirleyen bir tür içerir.Every variable has a type that determines what values can be stored in the variable. C#, tür açısından güvenli bir dildir ve C# derleyicisi, değişkenlerde depolanan değerlerin her zaman uygun türde olmasını garanti eder.C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type. Bir değişkenin değeri atama yoluyla veya ++ ve işleçleri kullanılarak değiştirilebilir -- .The value of a variable can be changed through assignment or through use of the ++ and -- operators.

Değerin alınabilmesi için önce bir değişken kesin olarak atanmalıdır (kesin atama).A variable must be definitely assigned (Definite assignment) before its value can be obtained.

Aşağıdaki bölümlerde açıklandığı gibi, değişkenler *Başlangıçta atandı _ veya _ Başlangıçta atanmamış * şeklindedir.As described in the following sections, variables are either initially assigned _ or _initially unassigned**. Başlangıçta atanan değişkenin iyi tanımlanmış bir başlangıç değeri vardır ve her zaman kesinlikle atanan olarak kabul edilir.An initially assigned variable has a well-defined initial value and is always considered definitely assigned. Başlangıçta atanmamış bir değişkenin ilk değeri yok.An initially unassigned variable has no initial value. Başlangıçta atanmamış bir değişkenin belirli bir konumda kesinlikle atanabileceği kabul edilmesi için, değişkene bir atama, bu konuma yönelik her olası yürütme yolunda gerçekleşmelidir.For an initially unassigned variable to be considered definitely assigned at a certain location, an assignment to the variable must occur in every possible execution path leading to that location.

Değişken kategorileriVariable categories

C#, değişkenlerin yedi kategorisini tanımlar: statik değişkenler, örnek değişkenleri, dizi öğeleri, değer parametreleri, başvuru parametreleri, çıkış parametreleri ve yerel değişkenler.C# defines seven categories of variables: static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables. Aşağıdaki bölümler bu kategorilerin her birini anlatmaktadır.The sections that follow describe each of these categories.

ÖrnekteIn the example

class A
{
    public static int x;
    int y;

    void F(int[] v, int a, ref int b, out int c) {
        int i = 1;
        c = a + b++;
    }
}

x statik bir değişkendir, bir y örnek değişkenidir, bir v[0] dizi öğesidir, bir değer parametresidir, bir başvuru parametresidir, bir a b c çıkış parametresidir ve i yerel bir değişkendir.x is a static variable, y is an instance variable, v[0] is an array element, a is a value parameter, b is a reference parameter, c is an output parameter, and i is a local variable.

Statik değişkenlerStatic variables

Değiştiriciyle belirtilen bir alana static statik değişken denir.A field declared with the static modifier is called a static variable. Statik bir değişken, kendi kapsayıcı türü için statik oluşturucunun (statik oluşturucular) yürütülmesinden önce varlığına gelir ve ilişkili uygulama etki alanı mevcut olduğunda var olmaya erer.A static variable comes into existence before execution of the static constructor (Static constructors) for its containing type, and ceases to exist when the associated application domain ceases to exist.

Statik bir değişkenin ilk değeri, değişkenin türünün varsayılan değeridir (varsayılan değerlerdir).The initial value of a static variable is the default value (Default values) of the variable's type.

Kesin atama denetimi amacıyla, statik bir değişken başlangıçta atanan olarak kabul edilir.For purposes of definite assignment checking, a static variable is considered initially assigned.

Örnek değişkenleriInstance variables

Değiştirici olmadan tanımlanan bir alana static örnek değişkeni denir.A field declared without the static modifier is called an instance variable.

Sınıflarda örnek değişkenleriInstance variables in classes

Bir sınıfın örnek değişkeni, bu sınıfın yeni bir örneği oluşturulduğunda var olur ve bu örneğe bir başvuru olmadığında ve örneğin yok edicinin (varsa) yürütüldüğünden, var olmaya erer.An instance variable of a class comes into existence when a new instance of that class is created, and ceases to exist when there are no references to that instance and the instance's destructor (if any) has executed.

Bir sınıfın örnek değişkeninin ilk değeri, değişkenin türünün varsayılan değeridir (varsayılan değerlerdir).The initial value of an instance variable of a class is the default value (Default values) of the variable's type.

Kesin atama denetimi amacıyla, bir sınıfın örnek değişkeni başlangıçta atanan olarak kabul edilir.For the purpose of definite assignment checking, an instance variable of a class is considered initially assigned.

Yapılarda örnek değişkenleriInstance variables in structs

Bir yapının örnek değişkeni, ait olduğu yapı değişkeniyle tam olarak aynı yaşam süresine sahiptir.An instance variable of a struct has exactly the same lifetime as the struct variable to which it belongs. Diğer bir deyişle, bir yapı türü değişkeni var olduğunda veya varsa, yapının örnek değişkenlerini çok da yapabilirsiniz.In other words, when a variable of a struct type comes into existence or ceases to exist, so too do the instance variables of the struct.

Bir yapının örnek değişkeninin ilk atama durumu, kapsayan yapı değişkeni ile aynıdır.The initial assignment state of an instance variable of a struct is the same as that of the containing struct variable. Diğer bir deyişle, bir struct değişkeni başlangıçta atanan olarak kabul edildiğinde, bu yüzden örnek değişkenleri çok fazla olur ve bir struct değişkeni başlangıçta atanmamış olarak kabul edildiğinde, örnek değişkenleri de aynı şekilde atanmamış olur.In other words, when a struct variable is considered initially assigned, so too are its instance variables, and when a struct variable is considered initially unassigned, its instance variables are likewise unassigned.

Dizi öğeleriArray elements

Bir dizinin öğeleri, bir dizi örneği oluşturulduğunda varlığına gelir ve bu dizi örneğine bir başvuru olmadığında var olmaya sona bırakılır.The elements of an array come into existence when an array instance is created, and cease to exist when there are no references to that array instance.

Bir dizinin öğelerinin ilk değeri, dizi öğelerinin türünün varsayılan değeridir (varsayılan değerlerdir).The initial value of each of the elements of an array is the default value (Default values) of the type of the array elements.

Kesin atama denetimi amacıyla, bir dizi öğesi başlangıçta atanan olarak kabul edilir.For the purpose of definite assignment checking, an array element is considered initially assigned.

Değer parametreleriValue parameters

Or değiştiricisi olmadan belirtilen bir ref parametre out bir değer parametresidir.A parameter declared without a ref or out modifier is a value parameter.

Bir değer parametresi, işlev üyesi (yöntem, örnek Oluşturucu, erişimci veya işleç) veya parametrenin ait olduğu anonim işlev çağrılandan sonra var olur ve bu, çağrısında verilen bağımsız değişkenin değeri ile başlatılır.A value parameter comes into existence upon invocation of the function member (method, instance constructor, accessor, or operator) or anonymous function to which the parameter belongs, and is initialized with the value of the argument given in the invocation. İşlev üyesi veya anonim işlev geri alındıktan sonra normalde bir değer parametresi yok olarak sona erer.A value parameter normally ceases to exist upon return of the function member or anonymous function. Ancak, değer parametresi anonim bir işlev (anonim işlev ifadeleri) tarafından yakalandıysa, bu anonim işlevden oluşturulan temsilci veya ifade ağacı çöp toplama için uygun olana kadar en az bir değeri uzatır.However, if the value parameter is captured by an anonymous function (Anonymous function expressions), its life time extends at least until the delegate or expression tree created from that anonymous function is eligible for garbage collection.

Kesin atama denetimi amacıyla bir değer parametresi başlangıçta atanan olarak kabul edilir.For the purpose of definite assignment checking, a value parameter is considered initially assigned.

Başvuru parametreleriReference parameters

Değiştirici ile belirtilen bir parametre ref bir başvuru parametresidir.A parameter declared with a ref modifier is a reference parameter.

Başvuru parametresi yeni bir depolama konumu oluşturmaz.A reference parameter does not create a new storage location. Bunun yerine, bir başvuru parametresi, işlev üyesinde bağımsız değişken olarak verilen değişken ile aynı depolama konumunu temsil eder veya anonim işlev çağırma.Instead, a reference parameter represents the same storage location as the variable given as the argument in the function member or anonymous function invocation. Bu nedenle, başvuru parametresinin değeri her zaman temeldeki değişkenle aynıdır.Thus, the value of a reference parameter is always the same as the underlying variable.

Aşağıdaki kesin atama kuralları başvuru parametreleri için geçerlidir.The following definite assignment rules apply to reference parameters. Çıkış parametrelerinde açıklanan çıkış parametrelerine yönelik farklı kurallara göz önünde edin.Note the different rules for output parameters described in Output parameters.

  • Bir değişken, bir işlev üyesine veya temsilci çağrısına başvuru parametresi olarak geçirilebilmesi için kesinlikle atanmalı (kesin atama).A variable must be definitely assigned (Definite assignment) before it can be passed as a reference parameter in a function member or delegate invocation.
  • Bir işlev üyesi veya anonim işlev içinde, başlangıçta atanan bir başvuru parametresi olarak kabul edilir.Within a function member or anonymous function, a reference parameter is considered initially assigned.

Bir yapı türünün örnek metodu veya örnek erişimcisi içinde, this anahtar sözcüğü tam olarak yapı türünün başvuru parametresi olarak davranır (Bu erişim).Within an instance method or instance accessor of a struct type, the this keyword behaves exactly as a reference parameter of the struct type (This access).

Çıktı parametreleriOutput parameters

Değiştirici ile belirtilen bir parametre bir out çıkış parametresidir.A parameter declared with an out modifier is an output parameter.

Çıkış parametresi yeni bir depolama konumu oluşturmaz.An output parameter does not create a new storage location. Bunun yerine, bir çıkış parametresi işlev üyesinde veya temsilci çağrısında bağımsız değişken olarak verilen değişkenle aynı depolama konumunu temsil eder.Instead, an output parameter represents the same storage location as the variable given as the argument in the function member or delegate invocation. Bu nedenle, bir çıktı parametresinin değeri her zaman temeldeki değişkenle aynıdır.Thus, the value of an output parameter is always the same as the underlying variable.

Aşağıdaki kesin atama kuralları çıkış parametreleri için geçerlidir.The following definite assignment rules apply to output parameters. Başvuruparametrelerinde açıklanan başvuru parametrelerine yönelik farklı kurallara göz önünde edin.Note the different rules for reference parameters described in Reference parameters.

  • Bir değişken, bir işlev üyesinde veya temsilci çağrısında çıkış parametresi olarak geçirilebilmesi için kesinlikle atanmamalıdır.A variable need not be definitely assigned before it can be passed as an output parameter in a function member or delegate invocation.
  • Bir işlev üyesinin veya temsilci çağrısının normal tamamlanmasını takip eden bir çıktı parametresi olarak geçirilen her değişken, o yürütme yolunda atanmış olarak değerlendirilir.Following the normal completion of a function member or delegate invocation, each variable that was passed as an output parameter is considered assigned in that execution path.
  • Bir işlev üyesi veya anonim işlev içinde, bir çıkış parametresi başlangıçta atanmamış olarak değerlendirilir.Within a function member or anonymous function, an output parameter is considered initially unassigned.
  • İşlev üyesi veya anonim işlev normal bir şekilde döndürüldüğünden, bir işlev üyesinin veya anonim işlevin her çıkış parametresi kesinlikle atanmalıdır (kesin atama).Every output parameter of a function member or anonymous function must be definitely assigned (Definite assignment) before the function member or anonymous function returns normally.

Yapı türünün örnek Oluşturucusu içinde this anahtar sözcüğü, yapı türünün (Bu erişim) bir çıkış parametresi olarak tam olarak davranır.Within an instance constructor of a struct type, the this keyword behaves exactly as an output parameter of the struct type (This access).

Yerel değişkenlerLocal variables

*Yerel değişkeni _, bir blok, for_statement, switch_statement veya using_statement olabilecek bir _local_variable_declaration * tarafından bildirilmiştir; ya da bir try_statement için bir foreach_statement veya specific_catch_clause .A local variable _ is declared by a _local_variable_declaration, which may occur in a block, a for_statement, a switch_statement or a using_statement; or by a foreach_statement or a specific_catch_clause for a try_statement.

Yerel bir değişkenin ömrü, program yürütmenin, bu için ayrılan depolama garantisi olan bölümüdür.The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. Bu yaşam süresi, bu blok, specific_catch_clause , for_statement, switch_statement, using_statement veya foreach_statement sonlanana kadar, ilişkili olduğu bloğa, for_statement, switch_statement, using_statement, foreach_statement veya specific_catch_clause arasında en az girişi uzatır.This lifetime extends at least from entry into the block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause with which it is associated, until execution of that block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause ends in any way. (Bir kapalı blok girildiğinde veya bir yöntemi çağırmak askıya alır, ancak geçerli blok, for_statement, switch_statement, using_statement, foreach_statement veya specific_catch_clause yürütmeyi sonlandırmaz.) Yerel değişken anonim bir işlev (yakalanan dış değişkenler) tarafından yakalandıysa, bu, bir anonim işlevden oluşturulan temsilci veya ifade ağacı, yakalanan değişkene başvuruda bulunan diğer nesnelerle birlikte çöp toplama için uygun olana kadar en az ' ı uzatır.(Entering an enclosed block or calling a method suspends, but does not end, execution of the current block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause.) If the local variable is captured by an anonymous function (Captured outer variables), its lifetime extends at least until the delegate or expression tree created from the anonymous function, along with any other objects that come to reference the captured variable, are eligible for garbage collection.

Üst blok, for_statement, switch_statement, using_statement, foreach_statement veya specific_catch_clause yinelemeli olarak girilirse, her seferinde yerel değişkenin yeni bir örneği oluşturulur ve varsa local_variable_initializer her seferinde değerlendirilir.If the parent block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause is entered recursively, a new instance of the local variable is created each time, and its local_variable_initializer, if any, is evaluated each time.

Local_variable_declaration tarafından tanıtılan yerel bir değişken otomatik olarak başlatılmaz ve bu nedenle varsayılan değer yoktur.A local variable introduced by a local_variable_declaration is not automatically initialized and thus has no default value. Kesin atama denetimi amacıyla, bir local_variable_declaration tarafından tanıtılan yerel bir değişken başlangıçta atanmamış olarak değerlendirilir.For the purpose of definite assignment checking, a local variable introduced by a local_variable_declaration is considered initially unassigned. Local_variable_declaration , bir local_variable_initializer içerebilir ve bu durumda değişken yalnızca başlatma ifadesinden (bildirim deyimleri) sonra kesin olarak atanır.A local_variable_declaration may include a local_variable_initializer, in which case the variable is considered definitely assigned only after the initializing expression (Declaration statements).

Bir local_variable_declaration tarafından tanıtılan yerel bir değişken kapsamında, bu yerel değişkene, local_variable_declarator önündeki metinsel bir konumda başvuruda bulunmak için derleme zamanı hatası vardır.Within the scope of a local variable introduced by a local_variable_declaration, it is a compile-time error to refer to that local variable in a textual position that precedes its local_variable_declarator. Yerel değişken bildirimi örtük (yerel değişken bildirimleri) ise, değişkenin local_variable_declarator içindeki değişkenine başvurmasının de bir hatadır.If the local variable declaration is implicit (Local variable declarations), it is also an error to refer to the variable within its local_variable_declarator.

Bir foreach_statement veya specific_catch_clause tarafından tanıtılan yerel bir değişken, tüm kapsamda kesin olarak atanmış olarak değerlendirilir.A local variable introduced by a foreach_statement or a specific_catch_clause is considered definitely assigned in its entire scope.

Yerel bir değişkenin gerçek yaşam süresi uygulamaya bağımlıdır.The actual lifetime of a local variable is implementation-dependent. Örneğin, bir derleyici bir bloktaki yerel değişkenin yalnızca o bloktaki küçük bir bölüm için kullanıldığını statik olarak belirleyebilir.For example, a compiler might statically determine that a local variable in a block is only used for a small portion of that block. Derleyici bu analizi kullanarak, değişkenin depolamanın, kapsayan bloğundan daha kısa bir yaşam süresine sahip olan bir kod üretebilir.Using this analysis, the compiler could generate code that results in the variable's storage having a shorter lifetime than its containing block.

Yerel başvuru değişkeni tarafından başvurulan depolama alanı, yerel başvuru değişkeninin (Otomatik bellek yönetimi) yaşam süresinden bağımsız olarak geri kazanılır.The storage referred to by a local reference variable is reclaimed independently of the lifetime of that local reference variable (Automatic memory management).

Varsayılan değerlerDefault values

Aşağıdaki değişken kategorileri varsayılan değerlerine otomatik olarak başlatılır:The following categories of variables are automatically initialized to their default values:

  • Statik değişkenler.Static variables.
  • Sınıf örneklerinin örnek değişkenleri.Instance variables of class instances.
  • Dizi öğeleri.Array elements.

Bir değişkenin varsayılan değeri, değişkenin türüne bağlıdır ve aşağıdaki şekilde belirlenir:The default value of a variable depends on the type of the variable and is determined as follows:

  • Bir value_type değişkeni için varsayılan değer, value_type varsayılan Oluşturucusu (Varsayılan oluşturucular) tarafından hesaplanan değerle aynıdır.For a variable of a value_type, the default value is the same as the value computed by the value_type's default constructor (Default constructors).
  • Reference_type bir değişken için, varsayılan değer null .For a variable of a reference_type, the default value is null.

Varsayılan değerlere başlatma işlemi, kullanım için ayrılmadan önce bellek Yöneticisi veya çöp toplayıcı başlatma belleğini tümüyle bit sıfır olarak başlatmaya gerek kalmadan yapılır.Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. Bu nedenle, null başvuruyu göstermek için tümü-bit-sıfır kullanmak kullanışlıdır.For this reason, it is convenient to use all-bits-zero to represent the null reference.

Kesin atamaDefinite assignment

Bir işlev üyesinin çalıştırılabilir kodundaki belirli bir konumda, derleyici, belirli bir statik Akış Analizi (kesin atamayı belirlemek Için kesin kurallar), değişkenin otomatik olarak başlatıldığını veya en az bir atamanın hedefi olduğunu kanıtlayabiliyorsa, bir değişken kesin bir şekilde atanır .At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by a particular static flow analysis (Precise rules for determining definite assignment), that the variable has been automatically initialized or has been the target of at least one assignment. Belirli bir deyişle, kesin atama kuralları şunlardır:Informally stated, the rules of definite assignment are:

Yukarıdaki resmi olmayan kuralları temel alan biçimsel belirtim, Başlangıçta atanan değişkenler, Başlangıçta atanmamış değişkenlerve kesin atamayı belirlemek için tam kurallarbölümünde açıklanmaktadır.The formal specification underlying the above informal rules is described in Initially assigned variables, Initially unassigned variables, and Precise rules for determining definite assignment.

Bir struct_type değişkeninin örnek değişkenlerinin kesin atama durumları, tek tek ve toplu olarak izlenir.The definite assignment states of instance variables of a struct_type variable are tracked individually as well as collectively. Yukarıdaki kurallara ek olarak, aşağıdaki kurallar struct_type değişkenleri ve bunların örnek değişkenleri için geçerlidir:In addition to the rules above, the following rules apply to struct_type variables and their instance variables:

  • Bir örnek değişkeni, kendi kapsayıcı struct_type değişkeni kesin olarak atanmış olarak kabul edildiğinde, kesin olarak atanır.An instance variable is considered definitely assigned if its containing struct_type variable is considered definitely assigned.
  • Örnek değişkenlerinin her biri kesinlikle atanmış olarak kabul edildiğinde, bir struct_type değişken kesin olarak atanır.A struct_type variable is considered definitely assigned if each of its instance variables is considered definitely assigned.

Kesin atama, aşağıdaki bağlamlarda gereksinimdir:Definite assignment is a requirement in the following contexts:

  • Bir değişken, değerinin alındığı her konumda kesinlikle atanmalıdır.A variable must be definitely assigned at each location where its value is obtained. Bu, tanımsız değerlerin hiçbir şekilde gerçekleşmemesini sağlar.This ensures that undefined values never occur. Bir ifadede bir değişkenin oluşma nedeni, değişkenin değerini elde etmek için kabul edilir, ancakThe occurrence of a variable in an expression is considered to obtain the value of the variable, except when
    • değişken, basit bir atamanın sol işleneni,the variable is the left operand of a simple assignment,
    • değişken, çıkış parametresi olarak geçirilir veyathe variable is passed as an output parameter, or
    • değişken bir struct_type değişkenidir ve bir üye erişiminin sol işleneni olarak oluşur.the variable is a struct_type variable and occurs as the left operand of a member access.
  • Bir değişken, başvuru parametresi olarak geçirildiği her bir konumda kesinlikle atanmalıdır.A variable must be definitely assigned at each location where it is passed as a reference parameter. Bu, çağrılan işlev üyesinin başlangıçta atanan başvuru parametresini düşünebilmesini sağlar.This ensures that the function member being invoked can consider the reference parameter initially assigned.
  • İşlev üyesinin tüm çıkış parametreleri, işlev üyesinin döndürdüğü her konumda kesinlikle atanmalıdır (bir return ifadesiyle veya işlev üyesi gövdesinin sonuna ulaşan yürütme yoluyla).All output parameters of a function member must be definitely assigned at each location where the function member returns (through a return statement or through execution reaching the end of the function member body). Bu, işlev üyelerinin çıkış parametrelerinde tanımsız değerler döndürmemesini sağlar, bu sayede derleyicinin değişken atama ile eşdeğer bir çıkış parametresi olarak bir değişken alan bir işlev üye çağrısını düşünmesini sağlar.This ensures that function members do not return undefined values in output parameters, thus enabling the compiler to consider a function member invocation that takes a variable as an output parameter equivalent to an assignment to the variable.
  • thisBir struct_type örneği oluşturucusunun değişkeni, örnek oluşturucunun döndürdüğü her bir konumda kesinlikle atanmalıdır.The this variable of a struct_type instance constructor must be definitely assigned at each location where that instance constructor returns.

Başlangıçta atanan değişkenlerInitially assigned variables

Aşağıdaki değişken kategorileri başlangıçta atanan olarak sınıflandırılır:The following categories of variables are classified as initially assigned:

  • Statik değişkenler.Static variables.
  • Sınıf örneklerinin örnek değişkenleri.Instance variables of class instances.
  • Başlangıçta atanan yapı değişkenlerinin örnek değişkenleri.Instance variables of initially assigned struct variables.
  • Dizi öğeleri.Array elements.
  • Değer parametreleri.Value parameters.
  • Başvuru parametreleri.Reference parameters.
  • Bir catch yan tümce veya deyime göre belirtilen değişkenler foreach .Variables declared in a catch clause or a foreach statement.

Başlangıçta atanmamış değişkenlerInitially unassigned variables

Aşağıdaki değişken kategorileri başlangıçta atanmamış olarak sınıflandırılır:The following categories of variables are classified as initially unassigned:

  • Başlangıçta atanmamış yapı değişkenlerinin örnek değişkenleri.Instance variables of initially unassigned struct variables.
  • Yapı örneği oluşturucularının değişkeni de dahil olmak üzere çıkış parametreleri this .Output parameters, including the this variable of struct instance constructors.
  • Bir catch yan tümce veya bir bildirimde tanımlananlar hariç yerel değişkenler foreach .Local variables, except those declared in a catch clause or a foreach statement.

Kesin atamayı belirlemek için kesin kurallarPrecise rules for determining definite assignment

Kullanılan her değişkenin kesinlikle atandığını tespit etmek için derleyicinin bu bölümde açıklanacak bir işlem kullanması gerekir.In order to determine that each used variable is definitely assigned, the compiler must use a process that is equivalent to the one described in this section.

Derleyici, bir veya daha fazla başlangıçta atanmamış değişkenine sahip her bir işlev üyesinin gövdesini işler.The compiler processes the body of each function member that has one or more initially unassigned variables. Her başlangıçta atanmamış her değişken için, derleyici, işlev üyesinde aşağıdaki noktaların her birinde bir * _v * için *kesin atama durumu _ ' i belirler:For each initially unassigned variable v, the compiler determines a definite assignment state _ for _v at each of the following points in the function member:

  • Her deyimin başlangıcındaAt the beginning of each statement
  • Her deyimin bitiş noktasında (bitiş noktaları ve ulaşılabilirlik)At the end point (End points and reachability) of each statement
  • Her bir yay üzerinde, denetimi başka bir bildirime veya bir deyimin bitiş noktasına aktarırOn each arc which transfers control to another statement or to the end point of a statement
  • Her ifadenin başlangıcındaAt the beginning of each expression
  • Her ifadenin sonundaAt the end of each expression

V 'nin kesin atama durumu aşağıdakilerden biri olabilir:The definite assignment state of v can be either:

  • Kesinlikle atandı.Definitely assigned. Bu, tüm olası denetimde bu noktaya akar, v 'nin bir değer atandığını gösterir.This indicates that on all possible control flows to this point, v has been assigned a value.
  • Kesin olarak atanmadı.Not definitely assigned. Türünde bir ifadenin sonundaki bir değişkenin durumu için bool , kesinlikle atanmayan bir değişkenin durumu (ancak gerekli değildir) aşağıdaki alt durumlardan birine denk gelebilir:For the state of a variable at the end of an expression of type bool, the state of a variable that isn't definitely assigned may (but doesn't necessarily) fall into one of the following sub-states:
    • Doğru ifadeden sonra kesinlikle atandı.Definitely assigned after true expression. Bu durum, Boole ifadesi doğru olarak değerlendiriliyorsa, ancak Boolean ifadesi false olarak değerlendiriliyorsa atanmadığında, v 'nin kesinlikle atandığını gösterir.This state indicates that v is definitely assigned if the boolean expression evaluated as true, but is not necessarily assigned if the boolean expression evaluated as false.
    • Yanlış ifadeden sonra kesinlikle atandı.Definitely assigned after false expression. Bu durum, Boole ifadesi false olarak değerlendirilirse, ancak Boolean ifadesi doğru olarak değerlendiriliyorsa atanmadığında, v 'nin kesinlikle atandığını gösterir.This state indicates that v is definitely assigned if the boolean expression evaluated as false, but is not necessarily assigned if the boolean expression evaluated as true.

Aşağıdaki kurallar, her konumda bir v değişkeninin durumunun nasıl belirlendiğini yönetir.The following rules govern how the state of a variable v is determined at each location.

Deyimler için genel kurallarGeneral rules for statements

  • bir işlev üye gövdesinin başlangıcında, v kesinlikle atanmaz.v is not definitely assigned at the beginning of a function member body.
  • v , herhangi bir ulaşılamaz deyimin başlangıcında kesinlikle atanır.v is definitely assigned at the beginning of any unreachable statement.
  • Diğer herhangi bir deyimin başlangıcında bulunan v 'nin kesin atama durumu, o deyimin başlangıcını hedefleyen tüm denetim akış aktarımları üzerinde v 'nin kesin atama durumu denetlenerek belirlenir.The definite assignment state of v at the beginning of any other statement is determined by checking the definite assignment state of v on all control flow transfers that target the beginning of that statement. (Ve yalnızca Bu) tüm denetim akışı aktarımlarına açık bir şekilde atanmışsa, v , deyimin başlangıcında kesinlikle atanır.If (and only if) v is definitely assigned on all such control flow transfers, then v is definitely assigned at the beginning of the statement. Olası denetim akış aktarımları kümesi, bildirime ulaşılabilirlik (bitiş noktaları ve erişilebilirlik) gibi şekilde belirlenir.The set of possible control flow transfers is determined in the same way as for checking statement reachability (End points and reachability).
  • Bir bloğun,,,,,,,,,,,,,, ya da deyimin son noktasındaki v 'nin kesin atama durumu, checked unchecked if while do for foreach lock using switch o deyimin bitiş noktasını hedefleyen tüm denetim akışı aktarımları üzerinde v 'nin kesin atama durumu denetlenerek belirlenir.The definite assignment state of v at the end point of a block, checked, unchecked, if, while, do, for, foreach, lock, using, or switch statement is determined by checking the definite assignment state of v on all control flow transfers that target the end point of that statement. Bu , her türlü denetim akışı aktarımına açık bir şekilde atanırsa, v , deyimin bitiş noktasında kesin olarak atanır.If v is definitely assigned on all such control flow transfers, then v is definitely assigned at the end point of the statement. Güvenmiyorsanız v , deyimin bitiş noktasında kesin olarak atanmaz.Otherwise; v is not definitely assigned at the end point of the statement. Olası denetim akış aktarımları kümesi, bildirime ulaşılabilirlik (bitiş noktaları ve erişilebilirlik) gibi şekilde belirlenir.The set of possible control flow transfers is determined in the same way as for checking statement reachability (End points and reachability).

Block deyimleri, Checked ve unchecked deyimleriBlock statements, checked, and unchecked statements

Denetim üzerindeki (ya da bloğun bitiş noktasındaki) blok,, veya deyimdeki deyimin kesin atama bildirimiyle aynı olan sanal sunucudan oluşan kesin atama durumu, ' ın, blok, veya deyimden önceki tüm v atama bildirimiyle aynıdır checked unchecked .The definite assignment state of v on the control transfer to the first statement of the statement list in the block (or to the end point of the block, if the statement list is empty) is the same as the definite assignment statement of v before the block, checked, or unchecked statement.

İfade deyimleriExpression statements

Expr ifadesi içeren bir ifade deyimi için:For an expression statement stmt that consists of the expression expr:

  • v , ifadenin başlangıcında, stmt'ın başlangıcında aynı kesin atama durumuna sahiptir.v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
  • Eğer, ifadenin sonunda kesin olarak atanmışsa, bu , stmt öğesinin son noktasında kesinlikle atanır; güvenmiyorsanız Bu, stmt öğesinin son noktasında kesinlikle atanmaz.If v if definitely assigned at the end of expr, it is definitely assigned at the end point of stmt; otherwise; it is not definitely assigned at the end point of stmt.

Bildirim deyimleriDeclaration statements

  • Stmt , başlatıcılar olmadan bir bildirim bildirimidir, bu durumda,, stmt'ın sonunda, stmt öğesinin son noktasında aynı kesin atama durumuna sahip olur .If stmt is a declaration statement without initializers, then v has the same definite assignment state at the end point of stmt as at the beginning of stmt.
  • Stmt , başlatıcıları olan bir bildirim bildirimidir, bu durumda, bir Başlatıcı içeren her bildirim için bir atama bildirimiyle birlikte, v için kesin atama durumu;If stmt is a declaration statement with initializers, then the definite assignment state for v is determined as if stmt were a statement list, with one assignment statement for each declaration with an initializer (in the order of declaration).

If deyimleriIf statements

Şu if biçimdeki bir ifade için:For an if statement stmt of the form:

if ( expr ) then_stmt else else_stmt
  • v , ifadenin başlangıcında, stmt'ın başlangıcında aynı kesin atama durumuna sahiptir.v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
  • V , ifadenin sonuna kesinlikle atanmışsa, bu, then_stmt için denetim akışı aktarımına ve herhangi bir else tümcesi yoksa, else_stmt ya da stmt öğesinin bitiş noktasına kesinlikle atanır.If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to then_stmt and to either else_stmt or to the end-point of stmt if there is no else clause.
  • Eğer v , ifadenin sonunda "kesin ifadeden sonra kesin olarak atandı" durumuna sahipse, bu durumda then_stmt için denetim akışı aktarımına kesin bir şekilde atanır ve herhangi bir else yan tümcesi yoksa, else_stmt ya da deyimin bitiş noktasına kesin olarak atanmaz.If v has the state "definitely assigned after true expression" at the end of expr, then it is definitely assigned on the control flow transfer to then_stmt, and not definitely assigned on the control flow transfer to either else_stmt or to the end-point of stmt if there is no else clause.
  • Eğer v , ifadenin sonundaki "yanlış ifadeden sonra kesin olarak atandı" durumuna sahipse, bu durumda else_stmt için denetim akışı aktarımına kesinlikle atanır ve then_stmt için denetim akışı aktarımına kesinlikle atanmamıştır.If v has the state "definitely assigned after false expression" at the end of expr, then it is definitely assigned on the control flow transfer to else_stmt, and not definitely assigned on the control flow transfer to then_stmt. Kesin olarak, yalnızca then_stmt bitiş noktasında kesin olarak atanmışsa, bu, yalnızca bir stmt öğesinin son noktasında atanır.It is definitely assigned at the end-point of stmt if and only if it is definitely assigned at the end-point of then_stmt.
  • Aksi halde, v , denetim akışı aktarımına then_stmt veya else_stmt ya da Else yan tümcesi yoksa stmt öğesinin bitiş noktasına kesinlikle atanmamış olarak değerlendirilir.Otherwise, v is considered not definitely assigned on the control flow transfer to either the then_stmt or else_stmt, or to the end-point of stmt if there is no else clause.

Switch deyimleriSwitch statements

switchDenetim ifadesi içeren bir deyim deyiminde :In a switch statement stmt with a controlling expression expr:

  • İfadenin başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v durumu ile aynıdır.The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
  • Denetim akışı 'nın erişilebilir anahtar bloğu bildirim listesine aktarımı üzerindeki kesin atama durumu, ifadenin sonundaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v on the control flow transfer to a reachable switch block statement list is the same as the definite assignment state of v at the end of expr.

While deyimleriWhile statements

Şu while biçimdeki bir ifade için:For a while statement stmt of the form:

while ( expr ) while_body
  • v , ifadenin başlangıcında, stmt'ın başlangıcında aynı kesin atama durumuna sahiptir.v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
  • V , ifadenin sonuna kesinlikle atanmışsa, bu, while_body ve stmt bitiş noktasına denetim akışı aktarımına kesinlikle atanır.If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to while_body and to the end point of stmt.
  • Eğer v , ifadenin sonunda "kesin ifadeden sonra kesin olarak atandı" durumuna sahipse, bu durumda while_body için denetim akışı aktarımına kesin bir şekilde atanır, ancak bu, stmt öğesinin son noktasında kesin olarak atanmaz.If v has the state "definitely assigned after true expression" at the end of expr, then it is definitely assigned on the control flow transfer to while_body, but not definitely assigned at the end-point of stmt.
  • Eğer v , ifadenin sonundaki "yanlış ifadeden sonra kesin olarak atandı" durumuna sahipse, bu durumda, denetim akışı aktarımının sonuna kesin olarak atanwhile_body mamıştır.If v has the state "definitely assigned after false expression" at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control flow transfer to while_body.

Do deyimleriDo statements

Şu do biçimdeki bir ifade için:For a do statement stmt of the form:

do do_body while ( expr ) ;
  • v , stmt 'ın başından başlayarak do_body olarak, denetim akışı aktarımında aynı kesin atama durumuna sahiptir.v has the same definite assignment state on the control flow transfer from the beginning of stmt to do_body as at the beginning of stmt.
  • v , do_body bitiş noktasında ifadenin başlangıcında aynı kesin atama durumuna sahiptir.v has the same definite assignment state at the beginning of expr as at the end point of do_body.
  • V , Expr'nin sonuna kesinlikle atanmışsa, bu, bir dizi sonunda denetim akışı aktarımına kesin olarak atanır.If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt.
  • Eğer v , ifadenin sonunda "false ifadeden sonra kesin olarak atandı" durumuna sahipse, bu durumda kesin bir şekilde, stmt öğesinin bitiş noktasına bir denetim akışı aktarımına atanır.If v has the state "definitely assigned after false expression" at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt.

For deyimleriFor statements

Formun bir ifadesine yönelik kesin atama denetimi for :Definite assignment checking for a for statement of the form:

for ( for_initializer ; for_condition ; for_iterator ) embedded_statement

deyimin yazıldığı gibi yapılır:is done as if the statement were written:

{
    for_initializer ;
    while ( for_condition ) {
        embedded_statement ;
        for_iterator ;
    }
}

For_condition for deyimden atlanırsa, kesin atama değerlendirmesi, for_condition yukarıdaki genişlemede değiştirilmiş gibi devam eder true .If the for_condition is omitted from the for statement, then evaluation of definite assignment proceeds as if for_condition were replaced with true in the above expansion.

Break, Continue ve goto deyimleriBreak, continue, and goto statements

,, Veya ifadesinin neden olduğu denetim akışı aktarımında bulunan v 'nin kesin atama durumu, break continue goto bildiriminin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v on the control flow transfer caused by a break, continue, or goto statement is the same as the definite assignment state of v at the beginning of the statement.

Throw deyimleriThrow statements

Formun bir ifade alanı içinFor a statement stmt of the form

throw expr ;

İfadenin başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of expr is the same as the definite assignment state of v at the beginning of stmt.

Return deyimleriReturn statements

Formun bir ifade alanı içinFor a statement stmt of the form

return expr ;
  • İfadenin başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of expr is the same as the definite assignment state of v at the beginning of stmt.
  • V bir çıkış parametresi ise, bu durumda kesinlikle atanmalıdır:If v is an output parameter, then it must be definitely assigned either:
    • ifadenin ardındanafter expr
    • ya da finally try - finally try - catch - finally return ifadesini kapsayan bir veya bloğunun sonunda.or at the end of the finally block of a try-finally or try-catch-finally that encloses the return statement.

Şu biçimdeki bir ifade için:For a statement stmt of the form:

return ;
  • V bir çıkış parametresi ise, bu durumda kesinlikle atanmalıdır:If v is an output parameter, then it must be definitely assigned either:
    • stmt Beforebefore stmt
    • ya da finally try - finally try - catch - finally return ifadesini kapsayan bir veya bloğunun sonunda.or at the end of the finally block of a try-finally or try-catch-finally that encloses the return statement.

Try-catch deyimleriTry-catch statements

Şu biçimdeki bir ifade için :For a statement stmt of the form:

try try_block
catch(...) catch_block_1
...
catch(...) catch_block_n
  • Try_block başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of try_block is the same as the definite assignment state of v at the beginning of stmt.
  • Catch_block_i başlangıcında (herhangi bir ı için), v 'nin kesin atama durumu, stmt öğesinin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of catch_block_i (for any i) is the same as the definite assignment state of v at the beginning of stmt.
  • , (Ve yalnızca Eğer) v 'nin bitiş noktasında kesin olarak atanmasının (ve yalnızca 1 ile n arasında) catch_block_i try_block bitiş noktasında tam olarak atanmış olması durumunda, sanal bölge 'nin son noktasındaki kesin atama durumu kesinlikle atanır.The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) v is definitely assigned at the end-point of try_block and every catch_block_i (for every i from 1 to n).

Try-finally deyimleriTry-finally statements

Şu try biçimdeki bir ifade için:For a try statement stmt of the form:

try try_block finally finally_block
  • Try_block başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of try_block is the same as the definite assignment state of v at the beginning of stmt.
  • Finally_block başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of finally_block is the same as the definite assignment state of v at the beginning of stmt.
  • Stmt öğesinin son noktasında kesin atama durumu, (ve yalnızca) aşağıdakilerden en az biri doğru ise, kesin olarak atanır:The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) at least one of the following is true:
    • v , try_block bitiş noktasında kesinlikle atanırv is definitely assigned at the end-point of try_block
    • v , finally_block bitiş noktasında kesinlikle atanırv is definitely assigned at the end-point of finally_block

Try_block içinde başlayan bir denetim akışı aktarımı (örneğin, bir goto ifade) yapılırsa ve try_block dışında sona erdiğinde, v kesin olarak finally_block bitiş noktasında atanırsa, bu denetim akış aktarımı üzerinde de kesin olarak atanır. If a control flow transfer (for example, a goto statement) is made that begins within try_block, and ends outside of try_block, then v is also considered definitely assigned on that control flow transfer if v is definitely assigned at the end-point of finally_block. (Bu yalnızca bir ise, bu denetim akışı aktarımındaki başka bir nedenden dolayı v 'nin kesin olarak atanması durumunda kesinlikle atanmamıştır.)(This is not an only if—if v is definitely assigned for another reason on this control flow transfer, then it is still considered definitely assigned.)

Try-catch-finally deyimleriTry-catch-finally statements

Formun bir ifadesiyle ilgili kesin atama Analizi try - catch - finally :Definite assignment analysis for a try-catch-finally statement of the form:

try try_block
catch(...) catch_block_1
...
catch(...) catch_block_n
finally *finally_block*

deyimin try - finally bir ifadeyi kapsayan bir ifade olması try - halinde yapılır catch :is done as if the statement were a try-finally statement enclosing a try-catch statement:

try {
    try try_block
    catch(...) catch_block_1
    ...
    catch(...) catch_block_n
}
finally finally_block

Aşağıdaki örnek, bir deyimin farklı bloklarının try (TRY ifadesinin) kesin atamayı nasıl etkilediğini gösterir.The following example demonstrates how the different blocks of a try statement (The try statement) affect definite assignment.

class A
{
    static void F() {
        int i, j;
        try {
            goto LABEL;
            // neither i nor j definitely assigned
            i = 1;
            // i definitely assigned
        }

        catch {
            // neither i nor j definitely assigned
            i = 3;
            // i definitely assigned
        }

        finally {
            // neither i nor j definitely assigned
            j = 5;
            // j definitely assigned
            }
        // i and j definitely assigned
        LABEL:;
        // j definitely assigned

    }
}

Foreach deyimleriForeach statements

Şu foreach biçimdeki bir ifade için:For a foreach statement stmt of the form:

foreach ( type identifier in expr ) embedded_statement
  • İfadenin başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v durumu ile aynıdır.The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
  • Embedded_statement veya bitiş noktasına veya sonuna kadar olan denetim akışı aktarımı üzerindeki v 'nin kesin atama durumu, Expr'nin sonundaki v durumuyla aynıdır.The definite assignment state of v on the control flow transfer to embedded_statement or to the end point of stmt is the same as the state of v at the end of expr.

Using deyimleriUsing statements

Şu using biçimdeki bir ifade için:For a using statement stmt of the form:

using ( resource_acquisition ) embedded_statement
  • Resource_acquisition başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v durumu ile aynıdır.The definite assignment state of v at the beginning of resource_acquisition is the same as the state of v at the beginning of stmt.
  • Embedded_statement için denetim akışı aktarımında bulunan v 'nin kesin atama durumu, resource_acquisition sonundaki v durumuyla aynıdır.The definite assignment state of v on the control flow transfer to embedded_statement is the same as the state of v at the end of resource_acquisition.

Lock deyimleriLock statements

Şu lock biçimdeki bir ifade için:For a lock statement stmt of the form:

lock ( expr ) embedded_statement
  • İfadenin başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v durumu ile aynıdır.The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
  • Embedded_statement için denetim akışı aktarımında bulunan v 'nin kesin atama durumu, Expr'nin sonundaki v durumuyla aynıdır.The definite assignment state of v on the control flow transfer to embedded_statement is the same as the state of v at the end of expr.

Yield deyimleriYield statements

Şu yield return biçimdeki bir ifade için:For a yield return statement stmt of the form:

yield return expr ;
  • İfadenin başındaki v 'nin kesin atama durumu, stmt öğesinin başındaki v durumu ile aynıdır.The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
  • Stmt 'in sonundaki v 'nin kesin atama durumu, ifadenin sonundaki v durumu ile aynıdır.The definite assignment state of v at the end of stmt is the same as the state of v at the end of expr.
  • Bir yield break deyimin, kesin atama durumu üzerinde hiçbir etkisi yoktur.A yield break statement has no effect on the definite assignment state.

Basit ifadeler için genel kurallarGeneral rules for simple expressions

Aşağıdaki kural bu tür ifadeler için geçerlidir: değişmez değerler (değişmez değerler), basit adlar (basit adlar), üye erişim ifadeleri (üye erişimi), dizinlenmemiş taban erişim ifadeleri (taban erişimi), typeof ifadeler (typeof işleci), varsayılan değer ifadeleri (varsayılan değer ifadeleri) ve nameof ifadeler (NameOf ifadeleri).The following rule applies to these kinds of expressions: literals (Literals), simple names (Simple names), member access expressions (Member access), non-indexed base access expressions (Base access), typeof expressions (The typeof operator), default value expressions (Default value expressions) and nameof expressions (Nameof expressions).

  • Böyle bir ifadenin sonundaki v 'nin kesin atama durumu, ifadenin başındaki v 'nin kesin atama durumuyla aynıdır.The definite assignment state of v at the end of such an expression is the same as the definite assignment state of v at the beginning of the expression.

Katıştırılmış ifadeleri olan ifadeler için genel kurallarGeneral rules for expressions with embedded expressions

Aşağıdaki kurallar bu tür ifadeler için geçerlidir: parantez içinde ifadeler (parantez içine alınmış ifadeler), öğe erişim ifadeleri(öğe erişimi), dizin oluşturma (taban erişimi), artırma ve azaltma ifadeleri (Sonek artışı ve azaltma işleçleri, önek artırma ve azaltma işleçleri), atama ifadeleri (atama ifadeleri), birli + , - ,, ifadeler, ikili,,,,,,,,,,,,,,,,, ~ * + - * / % << >> < <= > >= == != is as & | ^ ifadeler (Aritmetik işleçler, kaydırma işleçleri, ilişkisel ve tür-test işleçleri, mantıksal işleçler), bileşik atama ifadeleri (bileşik atama) checked ve ifadeler ( unchecked Checked ve unchecked işleçleri), ayrıca dizi ve temsilci oluşturma ifadeleri (Yeni işleç).The following rules apply to these kinds of expressions: parenthesized expressions (Parenthesized expressions), element access expressions (Element access), base access expressions with indexing (Base access), increment and decrement expressions (Postfix increment and decrement operators, Prefix increment and decrement operators), cast expressions (Cast expressions), unary +, -, ~, * expressions, binary +, -, *, /, %, <<, >>, <, <=, >, >=, ==, !=, is, as, &, |, ^ expressions (Arithmetic operators, Shift operators, Relational and type-testing operators, Logical operators), compound assignment expressions (Compound assignment), checked and unchecked expressions (The checked and unchecked operators), plus array and delegate creation expressions (The new operator).

Bu ifadelerin her birinde, sabit bir düzende koşulsuz olarak değerlendirilen bir veya daha fazla alt ifade vardır.Each of these expressions has one or more sub-expressions that are unconditionally evaluated in a fixed order. Örneğin, ikili % işleci işlecin sol tarafını, sonra sağ tarafını değerlendirir.For example, the binary % operator evaluates the left hand side of the operator, then the right hand side. Dizin oluşturma işlemi, dizinlenmiş ifadeyi değerlendirir ve sonra her bir dizin ifadesini soldan sağa sırayla değerlendirir.An indexing operation evaluates the indexed expression, and then evaluates each of the index expressions, in order from left to right. Bir ifade ifadesi için, E2,..., en, bu sırayla değerlendirilen alt ifadeler:For an expression expr, which has sub-expressions e1, e2, ..., eN, evaluated in that order:

  • E1 'ın başında bulunan v 'nin kesin atama durumu, ifadenin başındaki kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of e1 is the same as the definite assignment state at the beginning of expr.
  • Ei 'nin başındaki (bir kereden fazla) v 'nin kesin atama durumu, önceki alt ifadenin sonundaki kesin atama durumuyla aynıdır.The definite assignment state of v at the beginning of ei (i greater than one) is the same as the definite assignment state at the end of the previous sub-expression.
  • İfadenin sonundaki v 'nin kesin atama durumu, en sonunda kesin atama durumuyla aynıdırThe definite assignment state of v at the end of expr is the same as the definite assignment state at the end of eN

Çağırma ifadeleri ve nesne oluşturma ifadeleriInvocation expressions and object creation expressions

Formun çağırma ifadesi için :For an invocation expression expr of the form:

primary_expression ( arg1 , arg2 , ... , argN )

ya da formun nesne oluşturma ifadesi:or an object creation expression of the form:

new type ( arg1 , arg2 , ... , argN )
  • Bir çağırma ifadesi için, primary_expression önce v 'nin kesin atama durumu, ifadenin Before ifadesi ile aynı olur.For an invocation expression, the definite assignment state of v before primary_expression is the same as the state of v before expr.
  • Bir çağırma ifadesi için, arg1 Before the v 'nin kesin atama durumu primary_expression sonrasında v durumu ile aynı olur.For an invocation expression, the definite assignment state of v before arg1 is the same as the state of v after primary_expression.
  • Bir nesne oluşturma ifadesinde, arg1 Before the v 'nin kesin atama durumu, ifadenin Before ifadesi ile aynı olur.For an object creation expression, the definite assignment state of v before arg1 is the same as the state of v before expr.
  • Her bir bağımsız değişken için, Argi 'nin kesin atama durumu normal ifade kuralları tarafından belirlenir ve herhangi bir ref veya out değiştiricilerin yok sayılıyor.For each argument argi, the definite assignment state of v after argi is determined by the normal expression rules, ignoring any ref or out modifiers.
  • Birden çok daha büyük olan her bir bağımsız değişken için, Argi 'nin, önceki bağımsız değişkenden sonraki v durumuyla aynı olması için, o kadar kesin bir atama durumu.For each argument argi for any i greater than one, the definite assignment state of v before argi is the same as the state of v after the previous arg.
  • Sanal değişken, out bağımsız değişkenlerin herhangi bir bağımsız değişkeni olarak (yani, formun bir bağımsız değişkeni) olarak geçirilse out v , ifadenin sonunda v durumu kesinlikle atanır.If the variable v is passed as an out argument (i.e., an argument of the form out v) in any of the arguments, then the state of v after expr is definitely assigned. Güvenmiyorsanız ifade sonrasında v 'Nin durumu, argN sonrasında v durumu ile aynıdır.Otherwise; the state of v after expr is the same as the state of v after argN.
  • Dizi başlatıcıları (dizi oluşturma ifadeleri), nesne başlatıcıları (nesne başlatıcıları), koleksiyon başlatıcıları (koleksiyon başlatıcıları) ve anonim nesne başlatıcıları (anonim nesne oluşturma ifadeleri) için, kesin atama durumu, bu yapıların hükümlerde tanımlandığı genişlemeyle belirlenir.For array initializers (Array creation expressions), object initializers (Object initializers), collection initializers (Collection initializers) and anonymous object initializers (Anonymous object creation expressions), the definite assignment state is determined by the expansion that these constructs are defined in terms of.

Basit atama ifadeleriSimple assignment expressions

Formun bir ifadesi için w = expr_rhs :For an expression expr of the form w = expr_rhs:

  • Expr_rhs önce v 'nin kesin atama durumu, ifadenin önüne v 'nin kesin atama durumuyla aynı olur.The definite assignment state of v before expr_rhs is the same as the definite assignment state of v before expr.
  • İfadenin sonunda, belirli bir v atama durumu, tarafından belirlenir:The definite assignment state of v after expr is determined by:
    • W , v ile aynı değişkense, ifadenin sonunda o 'nun kesin atama durumu kesin olarak atanır.If w is the same variable as v, then the definite assignment state of v after expr is definitely assigned.
    • Aksi takdirde, atama bir yapı türünün örnek Oluşturucusu içinde oluşursa, w bir özellik erişimidir, oluşturulan örnekte p 'nin otomatik olarak uygulanan bir özellik olarak belirlenmesi ve v 'nin gizli yedekleme alanı, p'nin gizli olarak atanması durumunda v 'nin kesin atama durumu kesin olarak atanır.Otherwise, if the assignment occurs within the instance constructor of a struct type, if w is a property access designating an automatically implemented property P on the instance being constructed and v is the hidden backing field of P, then the definite assignment state of v after expr is definitely assigned.
    • Aksi takdirde, ifadenin sonunda v 'nin kesin atama durumu, expr_rhs sonrasında v 'nin kesin atama durumu ile aynıdır.Otherwise, the definite assignment state of v after expr is the same as the definite assignment state of v after expr_rhs.

&& (koşullu ve) ifadeler&& (conditional AND) expressions

Formun bir ifadesi için expr_first && expr_second :For an expression expr of the form expr_first && expr_second:

  • Expr_first önce v 'nin kesin atama durumu, ifadenin önüne v 'nin kesin atama durumuyla aynı olur.The definite assignment state of v before expr_first is the same as the definite assignment state of v before expr.
  • Expr_first sonrasında v 'nin durumu kesinlikle atandıktan sonra veya "doğru ifadeden sonra kesinlikle atanır" olarak expr_second önce, v 'nin kesin atama durumu kesinlikle atanır.The definite assignment state of v before expr_second is definitely assigned if the state of v after expr_first is either definitely assigned or "definitely assigned after true expression". Aksi takdirde, kesinlikle atanmaz.Otherwise, it is not definitely assigned.
  • İfadenin sonunda, belirli bir v atama durumu, tarafından belirlenir:The definite assignment state of v after expr is determined by:
    • Expr_first değeri olan bir sabit ifade ise false , ifadenin sonunda v 'nin kesin atama durumu, expr_first sonrasında v 'nin kesin atama durumuyla aynıdır.If expr_first is a constant expression with the value false, then the definite assignment state of v after expr is the same as the definite assignment state of v after expr_first.
    • Aksi halde, expr_first sonrasında v 'nin durumu kesin olarak atandıktan sonra, ifadenin sonunda v durumu kesinlikle atanır.Otherwise, if the state of v after expr_first is definitely assigned, then the state of v after expr is definitely assigned.
    • Aksi takdirde, expr_second sonrasında v 'nin durumu kesin olarak atandıktan ve expr_first sonra v 'nin durumu "kesinlikle yanlış ifadeden sonra atanır" ise, ifadenin sonunda v durumu kesinlikle atanır.Otherwise, if the state of v after expr_second is definitely assigned, and the state of v after expr_first is "definitely assigned after false expression", then the state of v after expr is definitely assigned.
    • Aksi halde, expr_second sonrasında v 'nin durumu kesinlikle atanırsa veya "doğru ifadeden sonra kesinlikle atanır" ise, ifadenin sonunda v durumu "kesinlikle doğru ifadeden sonra atanır" olur.Otherwise, if the state of v after expr_second is definitely assigned or "definitely assigned after true expression", then the state of v after expr is "definitely assigned after true expression".
    • Aksi halde, expr_first sonrasında v 'nin durumu "yanlış ifadeden sonra" kesinlikle atanır "ve expr_second sonra da" yanlış ifadeden sonra, "kesinlikle atanır" olduğunda, bu durumda v 'nin durumu "kesinlikle yanlış ifadeden sonra atanır " olur.Otherwise, if the state of v after expr_first is "definitely assigned after false expression", and the state of v after expr_second is "definitely assigned after false expression", then the state of v after expr is "definitely assigned after false expression".
    • Aksi halde, ifadenin sonunda v durumu kesinlikle atanmaz.Otherwise, the state of v after expr is not definitely assigned.

ÖrnekteIn the example

class A
{
    static void F(int x, int y) {
        int i;
        if (x >= 0 && (i = y) >= 0) {
            // i definitely assigned
        }
        else {
            // i not definitely assigned
        }
        // i not definitely assigned
    }
}

değişken, i bir deyimin gömülü deyimlerinden birinde kesin olarak kabul edilir if ancak diğeri de değildir.the variable i is considered definitely assigned in one of the embedded statements of an if statement but not in the other. ifYöntemi içindeki deyiminde F , i ifadenin yürütülmesi (i = y) her zaman bu ekli deyimin yürütülmesinden önce, değişken ilk eklenmiş ifadeye kesinlikle atanır.In the if statement in method F, the variable i is definitely assigned in the first embedded statement because execution of the expression (i = y) always precedes execution of this embedded statement. Buna karşılık, değişken, i yanlış bir şekilde x >= 0 test edilmiş olabilir ve değişken i atanmakta olan değişkenin atanmasından kaynaklanabilir.In contrast, the variable i is not definitely assigned in the second embedded statement, since x >= 0 might have tested false, resulting in the variable i being unassigned.

|| (koşullu OR) ifadeler|| (conditional OR) expressions

Formun bir ifadesi için expr_first || expr_second :For an expression expr of the form expr_first || expr_second:

  • Expr_first önce v 'nin kesin atama durumu, ifadenin önüne v 'nin kesin atama durumuyla aynı olur.The definite assignment state of v before expr_first is the same as the definite assignment state of v before expr.
  • Expr_first sonrasında v 'nin durumu kesinlikle atandıktan sonra veya "yanlış ifadeden sonra kesinlikle atanır" olarak expr_second önce, v 'nin kesin atama durumu kesinlikle atanır.The definite assignment state of v before expr_second is definitely assigned if the state of v after expr_first is either definitely assigned or "definitely assigned after false expression". Aksi takdirde, kesinlikle atanmaz.Otherwise, it is not definitely assigned.
  • İfadenin sonunda, kesin atama bildiriminin sonuna göre belirlenir:The definite assignment statement of v after expr is determined by:
    • Expr_first değeri olan bir sabit ifade ise true , ifadenin sonunda v 'nin kesin atama durumu, expr_first sonrasında v 'nin kesin atama durumuyla aynıdır.If expr_first is a constant expression with the value true, then the definite assignment state of v after expr is the same as the definite assignment state of v after expr_first.
    • Aksi halde, expr_first sonrasında v 'nin durumu kesin olarak atandıktan sonra, ifadenin sonunda v durumu kesinlikle atanır.Otherwise, if the state of v after expr_first is definitely assigned, then the state of v after expr is definitely assigned.
    • Aksi takdirde, expr_second sonrasında v 'nin durumu kesin olarak atandıktan ve expr_first sonra v 'nin durumu "kesin ifadeden sonra kesinlikle atanır" ise, ifadenin sonunda v durumu kesinlikle atanır.Otherwise, if the state of v after expr_second is definitely assigned, and the state of v after expr_first is "definitely assigned after true expression", then the state of v after expr is definitely assigned.
    • Aksi halde, expr_second sonrasında v 'nin durumu kesinlikle atanırsa veya "yanlış ifadeden sonra kesinlikle atanır" ise, ifadenin sonunda v durumu "kesinlikle yanlış ifadeden sonra atanır" olur.Otherwise, if the state of v after expr_second is definitely assigned or "definitely assigned after false expression", then the state of v after expr is "definitely assigned after false expression".
    • Aksi halde, expr_first sonrasında v 'nin durumu "kesin ifadeden sonra" kesinlikle atanır "ve expr_second sonrasında v 'nin durumu " kesinlikle doğru ifadeden sonra "açık olarak atandıktan" ise, ifadenin durumu "kesinlikle doğru şekilde atanır" olur.Otherwise, if the state of v after expr_first is "definitely assigned after true expression", and the state of v after expr_second is "definitely assigned after true expression", then the state of v after expr is "definitely assigned after true expression".
    • Aksi halde, ifadenin sonunda v durumu kesinlikle atanmaz.Otherwise, the state of v after expr is not definitely assigned.

ÖrnekteIn the example

class A
{
    static void G(int x, int y) {
        int i;
        if (x >= 0 || (i = y) >= 0) {
            // i not definitely assigned
        }
        else {
            // i definitely assigned
        }
        // i not definitely assigned
    }
}

değişken, i bir deyimin gömülü deyimlerinden birinde kesin olarak kabul edilir if ancak diğeri de değildir.the variable i is considered definitely assigned in one of the embedded statements of an if statement but not in the other. ifYöntemi içindeki deyiminde G , i ifadenin yürütülmesi (i = y) her zaman bu ekli deyimin yürütülmesinden önce, değişken ikinci gömülü ifadeye kesinlikle atanır.In the if statement in method G, the variable i is definitely assigned in the second embedded statement because execution of the expression (i = y) always precedes execution of this embedded statement. Buna karşılık, değişken, i doğru bir şekilde test edilmiş olabilir, bu da değişken x >= 0 atanmakta olan değişkenin atanmamış olması gibi, ilk ekli ifadede kesin olarak atanmaz i .In contrast, the variable i is not definitely assigned in the first embedded statement, since x >= 0 might have tested true, resulting in the variable i being unassigned.

!! (mantıksal değilleme) ifadeleri(logical negation) expressions

Formun bir ifadesi için ! expr_operand :For an expression expr of the form ! expr_operand:

  • Expr_operand önce v 'nin kesin atama durumu, ifadenin önüne v 'nin kesin atama durumuyla aynı olur.The definite assignment state of v before expr_operand is the same as the definite assignment state of v before expr.
  • İfadenin sonunda, belirli bir v atama durumu, tarafından belirlenir:The definite assignment state of v after expr is determined by:
    • * Expr_operand * sonrasında v 'nin durumu kesin olarak atandıktan sonra, Expr 'den sonra v durumu kesinlikle atanır.If the state of v after *expr_operand *is definitely assigned, then the state of v after expr is definitely assigned.
    • * Expr_operand * sonrasında v 'nin durumu kesin olarak atanmamışsa, ifadenin sonunda v durumu kesinlikle atanmaz.If the state of v after *expr_operand *is not definitely assigned, then the state of v after expr is not definitely assigned.
    • * Expr_operand * sonrasında v 'nin durumu "kesinlikle" yanlış ifadeden sonra atanır "ise, ifadenin sonunda v durumu" kesinlikle doğru ifadeden sonra atanır "olur.If the state of v after *expr_operand *is "definitely assigned after false expression", then the state of v after expr is "definitely assigned after true expression".
    • * Expr_operand * sonrasında v 'nin durumu "kesin ifadeden sonra kesinlikle atanır" ise, ifadenin sonunda v durumu "kesinlikle yanlış ifadeden sonra atanır" olur.If the state of v after *expr_operand *is "definitely assigned after true expression", then the state of v after expr is "definitely assigned after false expression".

???? (boş birleştirme) ifadeleri(null coalescing) expressions

Formun bir ifadesi için expr_first ?? expr_second :For an expression expr of the form expr_first ?? expr_second:

  • Expr_first önce v 'nin kesin atama durumu, ifadenin önüne v 'nin kesin atama durumuyla aynı olur.The definite assignment state of v before expr_first is the same as the definite assignment state of v before expr.
  • Expr_second önce v 'nin kesin atama durumu, expr_first sonrasında v 'nin kesin atama durumuyla aynı olur.The definite assignment state of v before expr_second is the same as the definite assignment state of v after expr_first.
  • İfadenin sonunda, kesin atama bildiriminin sonuna göre belirlenir:The definite assignment statement of v after expr is determined by:
    • Expr_first , null değeri olan bir sabit ifadesiyse (sabit deyimler), daha sonra, ifadenin sonunda v durumu, expr_second sonrasında v durumuyla aynıdır.If expr_first is a constant expression (Constant expressions) with value null, then the state of v after expr is the same as the state of v after expr_second.
  • Aksi halde, ifadenin sonunda v durumu, expr_first sonrasında v 'nin kesin atama durumuyla aynıdır.Otherwise, the state of v after expr is the same as the definite assignment state of v after expr_first.

?: (koşullu) ifadeler?: (conditional) expressions

Formun bir ifadesi için expr_cond ? expr_true : expr_false :For an expression expr of the form expr_cond ? expr_true : expr_false:

  • Expr_cond önce v 'nin kesin atama durumu, ifadenin önünde v durumu ile aynı olur.The definite assignment state of v before expr_cond is the same as the state of v before expr.
  • Expr_true önce ve yalnızca aşağıdakilerden biri varsa, önce v 'nin kesin atama durumu kesin olarak atanır:The definite assignment state of v before expr_true is definitely assigned if and only if one of the following holds:
    • expr_cond değeri olan sabit bir ifadedir falseexpr_cond is a constant expression with the value false
    • expr_cond sonrasında v 'nin durumu kesinlikle atanır veya "doğru ifadeden sonra kesinlikle atanır".the state of v after expr_cond is definitely assigned or "definitely assigned after true expression".
  • Expr_false önce ve yalnızca aşağıdakilerden biri varsa, önce v 'nin kesin atama durumu kesin olarak atanır:The definite assignment state of v before expr_false is definitely assigned if and only if one of the following holds:
    • expr_cond değeri olan sabit bir ifadedir trueexpr_cond is a constant expression with the value true
    • expr_cond sonrasında v 'nin durumu kesinlikle atanır veya "yanlış ifadeden sonra kesinlikle atanır".the state of v after expr_cond is definitely assigned or "definitely assigned after false expression".
  • İfadenin sonunda, belirli bir v atama durumu, tarafından belirlenir:The definite assignment state of v after expr is determined by:
    • Expr_cond değeri olan bir sabit Ifade (sabit deyimler) ise, true ifadenin sonrasında v durumu, expr_true sonrasında v durumu ile aynıdır.If expr_cond is a constant expression (Constant expressions) with value true then the state of v after expr is the same as the state of v after expr_true.
    • Aksi takdirde, expr_cond değeri olan bir sabit Ifade (sabit deyimler) ise, false ifadenin sonunda v durumu, expr_false sonrasında v durumu ile aynıdır.Otherwise, if expr_cond is a constant expression (Constant expressions) with value false then the state of v after expr is the same as the state of v after expr_false.
    • Aksi takdirde, expr_true sonrasında v 'nin durumu kesin olarak atandıktan ve expr_false daha sonra, v 'nin durumu kesin olarak atandıktan sonra, bu durumda, ifadenin durumu kesinlikle atanır.Otherwise, if the state of v after expr_true is definitely assigned and the state of v after expr_false is definitely assigned, then the state of v after expr is definitely assigned.
    • Aksi halde, ifadenin sonunda v durumu kesinlikle atanmaz.Otherwise, the state of v after expr is not definitely assigned.

Anonim işlevlerAnonymous functions

Bir lambda_expression veya anonymous_method_expression ifadesi için gövde ( blok veya ifade) gövdesi:For a lambda_expression or anonymous_method_expression expr with a body (either block or expression) body:

  • Gövdeden önceki bir dış değişken v 'nin kesin atama durumu, ifadenin önceki d durumuyla aynı olur.The definite assignment state of an outer variable v before body is the same as the state of v before expr. Diğer bir deyişle, dış değişkenlerin kesin atama durumu anonim işlev bağlamından devralınır.That is, definite assignment state of outer variables is inherited from the context of the anonymous function.
  • İfadenin sonrasında bir dış değişkenin kesin atama durumu, ifadenin önünde v durumu ile aynıdır.The definite assignment state of an outer variable v after expr is the same as the state of v before expr.

ÖrnekteThe example

delegate bool Filter(int i);

void F() {
    int max;

    // Error, max is not definitely assigned
    Filter f = (int n) => n < max;

    max = 5;
    DoWork(f);
}

maxanonim işlevin bildirildiği yere kesin olarak atanmadığından, derleme zamanı hatası oluşturur.generates a compile-time error since max is not definitely assigned where the anonymous function is declared. ÖrnekteThe example

delegate void D();

void F() {
    int n;
    D d = () => { n = 1; };

    d();

    // Error, n is not definitely assigned
    Console.WriteLine(n);
}

anonim işlevde olan atamanın, n anonim işlevin dışına yönelik kesin atama durumu üzerinde hiçbir etkisi olmadığından, derleme zamanı hatası da oluşturur n .also generates a compile-time error since the assignment to n in the anonymous function has no affect on the definite assignment state of n outside the anonymous function.

Değişken başvurularıVariable references

Variable_reference , değişken olarak sınıflandırılan bir ifadedir .A variable_reference is an expression that is classified as a variable. Variable_reference , her ikisi de geçerli değeri getirmek ve yeni bir değeri depolamak için erişilebilen bir depolama konumunu gösterir.A variable_reference denotes a storage location that can be accessed both to fetch the current value and to store a new value.

variable_reference
    : expression
    ;

C ve C++ ' da, bir variable_reference lvalue olarak bilinir.In C and C++, a variable_reference is known as an lvalue.

Değişken başvurularının Atomicity değeriAtomicity of variable references

Aşağıdaki veri türlerini okuma ve yazma işlemleri atomik:,,,,,,,, bool char byte sbyte short ushort uint int float ve başvuru türleridir.Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. Bunlara ek olarak, önceki listede bulunan temel bir türle birlikte sabit listesi türlerinin okuma ve yazma işlemleri de atomik bir yöntemdir.In addition, reads and writes of enum types with an underlying type in the previous list are also atomic. ,,, Ve gibi diğer türlerin okuma ve yazma işlemlerinin long ulong double decimal yanı sıra Kullanıcı tanımlı türlerin atomik olması garanti edilmez.Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic. Bu amaçla tasarlanan kitaplık işlevleri dışında, artış veya azaltma durumunda olduğu gibi atomik okuma-değiştirme-yazma garantisi yoktur.Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.