Flexible Use of class methods

Source: Internet
Author: User

Flexible Use of class methods
Constructor: constructor is a special method in the class. It mainly completes object initialization and the specified work when creating an object. In addition, the constructor method name and class name are the same and there is no return value type. No-argument Constructor

By default, the system assigns a non-argument constructor to the class without a method body. However, we can also customize a no-argument constructor.

When you create an object, a default value is automatically assigned to the property.

1 class Demo2 {3 public string DemoName {get; set;} 4 public Demo () // create a non-argument constructor 5 {6 this. demoName = "No parameter constructor"; // attributes to be initialized during object creation in the method body 7} 8}

TIPS: the shortcut for creating a non-argument constructor in Visual Studio is 'cid' + two tab keys. The shortcut for creating an attribute is 'prop' +

Two tab keys.

Constructors with Parameters

Sometimes we need to specify some values for the object attributes when creating the object, and these values are certain, so we need to include the parameter constructor.

1 class Demo 2 {3 public string DemoName {get; set;} 4 public Demo (string DemoName) // create a constructor with parameters 5 {6 this. demoName = DemoName; 7} 8} 9 10 class Test11 {12 Demo demo = new Demo ("constructor with Parameters"); // specify the value 13 in parentheses when creating an object}

Note the following:

1. When creating an object, the parameters in the parentheses must be the same as the list of parameters with the parameter constructor.

2. After the class has a constructor with parameters, the corresponding value must be given in parentheses when the object is created. Because in C #, once the class has a constructor,

No longer automatically assign constructors. (We recommend that you create another non-parametric constructor after creating a constructor with parameters .)

 

Append:

There is also a quick method for initializing attributes, that is, the object initializer.

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

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

              

 

Method overloading: perform different operations based on different parameters for the same thing, that is, method overloading. Features: each method has the same name, different parameter lists, and is in the same class.

Note: different parameter lists indicate different parameter types of each method, different parameter orders, or different number of parameters.

Different return values do not constitute method overloading.

Example:
1 class Demo 2 {3 public string DemoName {get; set ;} 4. demoName = "No parameter constructor"; 8} 9 public Demo (string demoName) 10 {11 this. demoName = demoName; 12} 13 14/*********** method overloading **********/15 public void SayHello (string name) 16 {17 Console. writeLine ("Hello, I'm {0}. I'm glad to meet you. ", Name); 18} 19 public void SayHello (string name, int age) 20 {21 Console. writeLine ("Hello, I'm {0}. I'm {1} years old. I'm glad to meet you. ", Name, age ); 22} 23} 24 25 class Test26 {27/********** heavy-duty constructor ********/28 Demo demo = new Demo (); 29 Demo demo1 = new Demo ("constructor with Parameters "); 30 31/*/32 public void Test () 33 {34 demo. sayHello ("James"); 35 demo. sayHello ("James", 18); 36} 37}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.