Lecture overview -- Keyboard shortcut: 'u'  Previous page: Methods in C# -- Keyboard shortcut: 'p'  Next page: Parameters -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 19 : 29
Object-oriented Programming in C#
Data Access, Properties, and Methods
Local variables in methods

We start with a brief overview of local variables, and in particular their initialization compared with the initialization of instance variables contained in classes.

Variables declared in the statements part - the block - of a method are called local variables

  • Local variables:

    • May be declared anywhere in the block

      • Not necessarily in the initial part of the block

    • Can be initialized by an initializer

    • Can alternatively be declared without an initializer

      • No default value is assigned to a local variable

        • Different from instance and class variables which are given default values

      • The compiler will complain if a local variable is referred without prior assignment

/user/normark/oop-csharp-1/sources/c-sharp/initialization/init.csLocal variables and instance variables - initialization and default values. This program is explained

In this example we illustrate differences between local variables and instance variables with respect to initialization.

Read more about local variables in the text book version of this material.