
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to initialize variables in C#?
A variable is a name given to a storage area that our programs can manipulate.
Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
To initialize a variable, first you need to define it.
int a;
Now to initialize, use an equal sign followed by a constant expression.
int a = 10;
We used the following syntax above.
variable_name = value;
Let us see a simple example to work with variable in C#.
Example
using System; namespace VariableDefinition { class Program { static void Main(string[] args) { short a; int b ; double c; a = 10; b = 20; c = a + b; Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c); Console.ReadLine(); } } }
Output
a = 10, b = 20, c = 30
- Related Articles
- Class with a constructor to initialize instance variables in Java
- How to initialize an array in C#?
- How to initialize jagged arrays in C#?
- How to initialize a vector in C++?
- How many ways are there to initialize the instance variables of a class in java?
- How to initialize an empty DateTime in C#
- How to initialize multi-dimensional arrays in C#?
- How to initialize two-dimensional arrays in C#?
- How to Initialize and Compare Strings in C#?
- How to initialize a rectangular array in C#?
- How to initialize a dynamic array in C++?
- How to initialize private static members in C++?
- How to initialize elements in an array in C#?
- Can we initialize static variables in a default constructor in Java?
- How to declare and initialize a dictionary in C#?

Advertisements