Pages

Monday, 15 June 2015

Variable

Variable

Variable is a name that represents data stored in memory during program execution, in the other words, it represents storage location that has a modifiable value.
C# provides four kind of variable like below:
Name Description
Local variable Holds temporary data within the scope of a method. Not a member of a type.
Field Holds data associated with a type or an instance of a type. Member of a type.
Parameter A temporary variable used to pass data from one method to another method.
Array element One member of a sequenced collection of (usually) homogeneous data items. Can be either local or a member of a type.

Let's take a look to the example:
using System;

namespace Program2
{
 class person
 {
  public string firstName;
  public string lastName;
  public ushort age;

  public void greet()
  {
   Console.WriteLine("Hello "+ firstName + " " + lastName);
  }

  public void talk(string words)
  {
   Console.WriteLine(firstName+" "+ lastName + " say "+ words);
  }
 }

 class Program
 {
  static void Main(string[] args)
  {
   person me=null;

   string personFirstName = "Yang";
   string personLastName = "Sopiana";
   ushort personAge = 29;
   string personSaidWord;

   me = new person();
   me.firstName = personFirstName;
   me.lastName = personLastName;
   me.age = personAge;
   me.greet();

   Console.Write("What Do you want to say:");
   personSaidWord = Console.ReadLine();

   me.talk(personSaidWord);
  }
 }
}
The above code will result:
Hello Yang Sopiana
What Do you want to say: Lets learn C#
Yang Sopiana say Lets learn C#

line :5-20 it's userdefined type definition to make our own-defined type named person
line :7-9 it's declaration of field firstName,lastName and age
[Type Name] [Variable Name]
line :11 & 16 it's method declaration
line :16 (string words) is declaration of parameter named words
line :18 This is the way to use variable value and show it into screen
line :26 Instantiation of userdifined type person to variable named me
line :28-30 Declaration local variable with initial value
[Type Name] [Variable Name] = [Value]
line :23-36 Asignment of me object's field
line :33 & 42 Method greet & talkinvocation.

Automatic Initialization

In previous code, we see some variables has initializer and some aren't.
Some kinds of variables are automatically set to default values if they are declared without an initializer, and others are not. Variables that are not automatically initialized to default values contain undefined values until the program assigns them a value.
Variable Stored In Auto-initialized Use
Local variables Stack or stack and heap No Used for local computation inside a function member
Class fields Heap Yes Members of a class
Struct fields Stack or heap Yes Members of a struct
Parameters Stack No Used for passing values into and out of a method
Array elements Heap Yes Members of an array

null and void

null indicates that a variable is set to nothing. Only reference types can be assigned the value null.

It is important to note that assigning the value null to a reference type is distinct from not assigning it at all. In other words, a variable that has been assigned null has still been set, and a variable with no assignment has not been set and therefore will likely cause a compile error if used prior to assignment.

Sometimes the C# syntax requires a data type to be specified but no data is passed. For example, if no return from a method is needed, C# allows the use of void to be specified as the data type instead. The use of void as the return type indicates that the method is not returning any data and tells the compiler not to expect a value. void is not a data type per se, but rather an identification of the fact that there is no data type.

Nullable Modifier

As I pointed out earlier, value types cannot be assigned null because, by definition, they can’t contain references, including references to nothing. However, this cause a problem in the real world, where values are missing or maybe unknown.
To declare variables that can store null you use the nullable modifier, ?.
int count = null;               //compile error because int can't be assigned to null
int? counter2 = null;   //can be compiled

Assigning null to value types is especially attractive in database programming. Frequently, value type columns in database tables allow nulls. Retrieving such columns and assigning them to corresponding fields within C# code is problematic, unless the fields can contain null as well. Fortunately, the nullable modifier is designed to handle such a scenario specifically.

0 comments:

Post a Comment