. NET basics step by step: [object-oriented static and non-static],. net Object-Oriented

Source: Internet
Author: User

. NET basics step by step: [object-oriented static and non-static],. net Object-Oriented

Static and non-static

Let's take a look at a piece of code to distinguish static and non-static:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; differences between static and non-static namespace {/*** non-static class * can define static fields, static attributes, and static methods. * You can also define non-static (instance Member) fields, non-static (instance Member) attributes, and non-static (instance Member) static methods. ** // Non-static class public class Person1 {// instance Member (non-static) private int _ id; // instance attribute (non-static) public int ID {get {return _ id;} set {_ id = value ;}// instance method (non-static) public void Showinfo () {}/ * ------------------------------------------------------------------ * // static field private static string _ name; // static attribute public static string Name {get {return _ name ;} set {_ name = value ;}}// static method public static void Sho (){}}}

It can be seen that the difference between static and non-static is the keyword:Static

 

Static and non-static differences:

1) in a non-static class, you can have both instance members and static members.

2) When calling an instance member, you need to use the object name. instance Member;

When calling a static member, you must use the class name. static member name;

3) non-static class: You can define static fields, static attributes, static methods, non-static (instance Member) fields, non-static (instance Member) attribute, non-static (instance Member) method.

 

Now let's look at a small case of static and non-static:

Static void Main (string [] args) {// call the instance Member Person p = new Person (); // non-static method p. m1 (); // static method Person. m2 (); Console. writeLine (); Console. readKey ();} public class Person {private static string _ name; public static string Name {get {return Person. _ name;} set {Person. _ name = value ;}} private char _ gender; public char Gender {get {return _ gender;} set {_ gender = value ;}} public void M1 () {Console. writeLine ("My non-static method");} public static void M2 () {Console. writeLine ("I Am a static method ");}}

Running result:

 

Summary:

Static members must be called using class names, while instance members use object names.

In a static function, you can only access static members, but not instance members.

In Instance functions, you can use both static members and instance members.

Only static members are allowed in a static class, and instance members are not allowed.

Usage:

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

2) Static classes share resources throughout the project. The static class releases resources only after all programs are completed.

 

This article is here, and finally a small advertisement: QQ group: . NET Step by stepGroup Number:590170361 (Add group remarks: what you see in the blog Park)

 

Related Article

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.