Home  >  Article  >  Backend Development  >  Sharing methods for flexible use of classes in C#

Sharing methods for flexible use of classes in C#

黄舟
黄舟Original
2017-03-21 11:20:271443browse

This article mainly introduces the methods of using classes flexibly in C#, which has a good reference value. Let’s take a look at it with the editor

Constructor

Summary: The constructor is a special method in a class, which mainly completes the initialization of the object and completes the specified work when creating the object. Moreover, the constructor method name is the same as the class name, and there is no return value type.

No-argument constructor

By default, the system will assign a no-argument constructor to the class, and there is no method body. But we can also customize a parameterless constructor to automatically give the properties a default value when creating the object.

class Demo
 {
 public string DemoName { get; set; }
 public Demo() //创建无参构造函数
 {
 this.DemoName = "无参构造函数"; //方法体内写在创建对象时需要初始化的属性
 }
 }

Tips: The shortcut to create a parameterless constructor in Visual Studio is 'ctor' + two tab keys; the shortcut to create a property is 'prop' + two tab keys.

Constructor with parameters

#Sometimes we need to specify some values ​​for the properties of the object when creating the object, and these values ​​​​are certain , so we need a parameterized constructor.

class Demo
 {
 public string DemoName { get; set; }
 public Demo(string DemoName) //创建带参构造函数
 {
 this.DemoName = DemoName;
 }
 }
 class Test
 {
 Demo demo = new Demo("带参构造函数"); //创建对象时在小括号内指定值
 }

In addition, there are a few things to note:

1. When creating an object, the parameters given in parentheses must be constructed with parametersThe parameter list of function is the same.

 2. After there is a constructor with parameters in the class, the corresponding value must be given in parentheses when creating the object. Because in C# once a class has a constructor, the constructor is no longer automatically assigned. (It is recommended to create a parameterless constructor after creating a parameterized constructor.)

Added:

There is another shortcut The method of initializing properties, that is, the object initializer.

Usage: Demo demo = new Demo(){ Name = "Object Initializer"};

Or: Demo demo = new Demo{ Name = "Object Initializer"};

Method overloading

Summary: The same thing, different operations are performed based on different parameters, that is, method overloading.

Features: Each method has the same name, different parameter list, and is in the same class.

Note: Different parameter lists mean that each method has different parameter types or different parameter orders or different number of parameters.

Merely different return values ​​do not constitute method overloading.

Example:

 class Demo
 {
 public string DemoName { get; set; }
 /*********构造函数重载********/
 public Demo()
 {
 this.DemoName = "无参构造函数";
 }
 public Demo(string demoName)
 {
 this.DemoName = demoName;
 }
 /**********方法重载**********/
 public void SayHello(string name)
 {
 Console.WriteLine("你好,我是{0},很高兴认识你。", name);
 }
 public void SayHello(string name, int age)
 {
 Console.WriteLine("你好,我是{0},今年{1}岁,很高兴认识你。", name, age);
 }
 }
 class Test
 {
 /*********构造函数重载********/
 Demo demo = new Demo();
 Demo demo1 = new Demo("带参构造函数");
 /**********方法重载**********/
 public void Test()
 {
 demo.SayHello("小明");
 demo.SayHello("小明", 18);
 }
 }

The above is the detailed content of Sharing methods for flexible use of classes in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn