Var Variable
In C#
Var Variables:
·
Var
variables are simply can be defined under the category of strongly Typed
variables.
·
These Strongly
typed variables are introduced in the version of 3.0.
·
These
variables know the data type at the compile time or simply we can say before
the run time. But basically, this will be identified by the value assigned for
the variable
·
This type
of variable can be called as inferred typed variable or can be defined as data
type known at the compile time.
·
These
variables once a value is assigned or its mandate to assign the value at the
time of declaration
·
If assigned,
then the type of variable cannot be changed like string to int or int to string
etc.
·
Var variable cannot be defined Globally, can be used only inside
methods or properties.
Syntax:
[Var] [variableName] = [assignment]
Example,
Invalid Example:
Valid Example:
#region Var Variable in c#
public class VarVariableDemo
{
public void callingMethod()
{
//Valid Declaration for var variable
var validVariable = 10;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("==============Var
variable Output Starts===============");
stringBuilder.AppendLine($"Type of
variable - {validVariable.GetType()}");
stringBuilder.AppendLine($"Value for
variable - {
validVariable}");
stringBuilder.AppendLine("================Var
variable Output Ends==============");
//prinitng values
MessageBox.Show(stringBuilder.ToString());
}
}
private void button9_Click(object sender, EventArgs e)
{
VarVariableDemo
varVariableDemoObject = new VarVariableDemo();
varVariableDemoObject.callingMethod();
}
#endregion
Output
Comments
Post a Comment