C # The difference between static and non-static members of classes in object-oriented

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacethe difference between static and non-static { Public  classPerson { Public voidM1 () {Console.WriteLine ("I am a non-static method"); }             Public Static voidM2 () {Console.WriteLine ("I am a static method"); }        }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacethe difference between static and non-static {classProgram {Static voidMain (string[] args) {Person P=NewPerson (); P.M1 ();//instance MethodPerson. M2 ();//Static MethodsConsole.ReadLine (); }    }}

The difference between static and non-static:

1) in non-static, there can be either an instance member or a static member.

2) When invoking an instance member, you need to use the object name. Instance Members

When you call a static member, you need to use the class name. Static member name

Summarize:

1) Static members must be called using the class name, and instance members are invoked using the object name

2) In an instance function, you can use either a static member or an instance member

3) in a static function, only static members are accessible, and access to instance members is not allowed

Use:

1) If you want your class to be used as a "tool class," consider writing the class as a static

2) Static classes share resources throughout the project

C # The difference between static and non-static members of classes in object-oriented

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.