- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 254字
- 2021-07-02 12:44:41
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
- 零起步玩轉(zhuǎn)掌控板與Mind+
- Learning Selenium Testing Tools with Python
- 羅克韋爾ControlLogix系統(tǒng)應(yīng)用技術(shù)
- PHP 編程從入門(mén)到實(shí)踐
- Podman實(shí)戰(zhàn)
- 學(xué)Python也可以這么有趣
- Highcharts Cookbook
- Hands-On Full Stack Development with Go
- Android開(kāi)發(fā)案例教程與項(xiàng)目實(shí)戰(zhàn)(在線實(shí)驗(yàn)+在線自測(cè))
- Spring核心技術(shù)和案例實(shí)戰(zhàn)
- 區(qū)塊鏈技術(shù)進(jìn)階與實(shí)戰(zhàn)(第2版)
- Scala Functional Programming Patterns
- AMP:Building Accelerated Mobile Pages
- 大話代碼架構(gòu):項(xiàng)目實(shí)戰(zhàn)版
- Oracle SOA Suite 12c Administrator's Guide