官术网_书友最值得收藏!

Variables in C#

In the previous code, you might have noticed that we created a few variables. A variable is something that varies, which means it is not constant. In programming, when we create a variable, the computer actually allocates a space in memory for it so that a value of the variable can be stored there.

Let's assign some values to the variables of the objects that we created in the previous section. We will first work with the customer1 object, as shown in the following code:

using System;

namespace Chapter2
{
public class Code_2_2
{
static void Main(string[] args)
{
Customer customer1 = new Customer();
customer1.firstName = "Molly";
customer1.lastName = "Dolly";
customer1.phoneNumber = "98745632";
customer1.emailAddress = "mollydolly@email.com";

Console.WriteLine("First name is " + customer1.firstName);
Console.ReadKey();
}
}

public class Customer
{
public string firstName;
public string lastName;
public string phoneNumber;
public string emailAddress;

public string GetFullName()
{
return firstName + " " + lastName;
}
}
}

Here, we are assigning values to the customer1 object. The code instructs the computer to create a space in the memory and store the value in it. Later, whenever you access the variable, the computer will go to the memory location and find out the value of the variable. Now, if we write a statement that will print the value of the firstName variable with the additional string before it, it will look as follows:

Console.WriteLine("First name is " + customer1.firstName);

The output of this code will be as follows:

First name is Molly

主站蜘蛛池模板: 临桂县| 云和县| 建湖县| 平原县| 宁国市| 冀州市| 江孜县| 胶南市| 镇沅| 志丹县| 邵武市| 万全县| 辉县市| 内江市| 东海县| 衡阳市| 碌曲县| 扶余县| 临洮县| 江华| 东海县| 巨野县| 马公市| 南乐县| 开封县| 海林市| 灵璧县| 绥芬河市| 连江县| 广丰县| 桂林市| 台江县| 边坝县| 栖霞市| 鸡泽县| 夹江县| 瑞安市| 苗栗县| 土默特左旗| 乌兰察布市| 辽中县|