GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • C# Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Variable Declaration in C# - SyntaxDB - C# Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Write to Console
      • Commenting
      • User Input
      • Read from File
      • Write to File
      • Namespace
    • Variables
      • Primitive Data Types
      • Variable Declaration
      • Parse
      • Constants
      • Pointers and References
      • Arrays
      • Lists
      • Dictionaries
      • Type Conversion
      • Structures
    • Control Flow
      • If Statement
      • Switch Case
      • For Loop
      • For Each
      • While Loop
      • Do While Loop
      • Try Catch Block
    • Object Oriented Programming
      • Class Declaration and Structure
      • Access Modifiers
      • Class Variables
      • Standard Methods
      • Static Methods
      • Constructor
      • Instantiation
      • Interfaces
      • Abstract Methods and Classes
      • Inheritance
    • Strings
      • String Variables
      • String Length
      • String Compare
      • String Trim
      • String Split
      • String Representation
Categories
Language
C#
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 5

Categories

  • User Program Communication
    • Write to Console
    • Commenting
    • User Input
    • Read from File
    • Write to File
    • Namespace
  • Variables
    • Primitive Data Types
    • Variable Declaration
    • Parse
    • Constants
    • Pointers and References
    • Arrays
    • Lists
    • Dictionaries
    • Type Conversion
    • Structures
  • Control Flow
    • If Statement
    • Switch Case
    • For Loop
    • For Each
    • While Loop
    • Do While Loop
    • Try Catch Block
  • Object Oriented Programming
    • Class Declaration and Structure
    • Access Modifiers
    • Class Variables
    • Standard Methods
    • Static Methods
    • Constructor
    • Instantiation
    • Interfaces
    • Abstract Methods and Classes
    • Inheritance
  • Strings
    • String Variables
    • String Length
    • String Compare
    • String Trim
    • String Split
    • String Representation

Variables

Variable Declaration in C#

Used to declare a variable. Variables can be implicitly or explicitly typed.

Variables declared this way (without a static modifier) within classes are called instance variables. They belong to an instance of the class (i.e. an object).


Syntax
modifier dataType variableName; ///modifier is optional

///variables can be assigned values either separately or on declaration
variableName = value; ///separate line assignment

modifier dataType variableName = value; ///same line assignment

///implicitly typed variable
var variable2 = value;

Notes

The modifier (public, private) permits or restricts direct access to the variable with respect to its scope (class, method, etc.). Variables declared within a class are called fields.

Variables without a modifier are known as local variables, typically used within a method. They are temporary and only exist within the scope of the where its declared method.

dataType is the data type of the variable.


Example
public class Car { 
    private int speed; ///private variable declaration
    public int wheels; ///public variable declaration
    
    /*...constructor, etc...*/

    public void speedUp() {
        ///local variable declaration, in line assignment, only seen within speedUp method
        int speedIncrease = 10;
        speed += speedIncrease;
    }
}

See Also
Related

Access Modifiers
Primitive Data Types

Documentation

Types (C# Programming Reference) - MSDN

Add to Slack

< Primitive Data Types   |   Parse >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.