A question about static and non-static in C #

Source: Internet
Author: User

The difference between static methods, variables and non-static methods, variables, the big God in the garden has long been a lot of detailed summary, the individual feel that static methods, variables and non-static methods, variables can be summed up in the following two sentences:

Static is a class-owned

Non-static is an object-owned

This means that Classname.staticmethod () can be used directly when calling static methods and variables, and calling non-static methods and variables can only create an instance of a class first ClassName instance = new Calssname (); The instance is then called. Method ();

But many people say that static methods can only access static methods and variables in the class, and have great doubts about them:

        Static voidMain (string[] args) {test test=NewTest (); Test.        Printinnotstatic (); }        classTest { Public Static voidprintinstatic () {Console.WriteLine ("This was in the static method"); }             Public voidprintinnotstatic () {Console.WriteLine ("This was in the not static method"); }        }            

I am not calling a non-static method here? What's going on here?
Through their own access to information and thinking: found that they had jumped into a pit, did not notice that the static method can only access static methods and variables in the class of the static members in the sentence .

I have created an object in the code above and then called it through the object, rather than calling the method directly with the class. It is easy to understand the meaning of the sentence as long as it understands the difference between the class and the object.

Keep a record of your doubts and hope to help the friends who have this question.

This article belongs to the Novice post, there is the wrong place please the great God to advise.

A question about static and non-static in C #

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.