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

How to create objects

In C#, to create an object of a class, you have to use the new keyword. Let's look at an example of an object:

Customer customer1 = new Customer();

Here, we started by writing Customer, which is the name of the class. This represents the type of the object. After that, we gave the name of the object, which in this case is customer1. You can give any name to that object. For example, if the customer is Mr. Jones, we could name the object jackJones. After the object name, we then inserted an equals sign (=), which means that we are assigning a value to the customer1 object. After that, we entered a keyword called new, which is a special keyword that tells the compiler to create a new object of the class that is given next to it. Here, we gave Customer again with () next to it. When we place Customer(), we are actually calling the constructor of that class. We will talk about constructors in subsequent chapters.

We can create jackJones by using the following code:

Customer jackJones = new Customer();
主站蜘蛛池模板: 望都县| 祁东县| 丰都县| 七台河市| 安陆市| 布尔津县| 德令哈市| 泰和县| 大新县| 潮安县| 桓台县| 多伦县| 合作市| 武胜县| 个旧市| 江北区| 石嘴山市| 象州县| 梅州市| 堆龙德庆县| 井陉县| 信阳市| 宜州市| 吴川市| 武乡县| 罗定市| 虎林市| 崇阳县| 滦南县| 鞍山市| 余江县| 云霄县| 涟源市| 阳城县| 合肥市| 晋城| 盱眙县| 安阳市| 台东市| 安义县| 长沙市|