C # Method

Source: Internet
Author: User

I. static keywords

1. Differences between static methods and instantiation methods:
A) access and call methods:
Static Method: Initialize before calling
Instantiation method: Initialize an object
B) static fields: space is allocated in advance during program compilation, which makes access more efficient.
Instantiated field: space is allocated only when the code segment is executed. The keyword new is required.
2. static call Keyword: static
The static method can access other static members, but cannot access instance members.

Example:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Static namespace Method
{
Public class Test
{
Private static int member;

// Access static fields by instance method
Public void Way (int num)
{
Member = num;
}

// Use static methods to access static Fields
Public static void Show ()
{
Console. WriteLine ("Value of member:" + member );
}
}
Class Program
{
Static void Main (string [] args)
{
Test test = new Test ();
// Object call instance method
Test. Way (40 );
Test. Show ();
}
}
}

3. static class: If the static keyword is added to the class, the Members in the class must be static members and cannot inherit static classes.

 

Ii. Method Overloading
1. Meaning: methods with the same method name and different parameter data types, numbers, or sequences in the same class

2. Example:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace ConsoleApplication4
{
Public class Student
{
Public void Show1 (int num1, int num2)
{
Console. WriteLine ("adding two integers: {0}", num1 + num2 );
}
Public void Show2 (string sign1, string sign2)
{
Console. WriteLine ("adding two strings equals: {0}", sign1 + sign2 );
}
}

Class Program
{
Static void Main (string [] args)
{
Student student = new Student (); // get the Student Class Object
Student. Show1 (12, 21 );
Student. Show2 ("Hello", "girl ");
}
}
}

3. constructor: Initialize member variables with the same class name
1. features:
A): No Return Value
B): Reload allowed
C): To create an object successfully, you must call the corresponding constructor before assigning values to the attribute.
2. Syntax:
Access modifier Class Name (parameter list)
{
// Constructor
}


3. default constructor
A) if no defined constructor is displayed in the class, the compiler automatically provides an implicit default constructor without parameters. When the displayed constructor is defined, the compiler does not provide this function. To use the default constructor, the definition must be displayed.

For example, the Student class declares two constructors.


Public class Student
{
Private int no;
Private string name;
// Define a constructor with int Parameters
Public Student (int num ){}
// Define a constructor with string Parameters
Public Student (string name ){}
} Call the constructor to instantiate an object.

Student student = new Student (); // The constructor without parameters is incorrectly defined. You must manually define a non-parameter constructor in the Student class.

Public Student () {} 4. constructor with Parameters

Example:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Constructors with parameters in namespace
{
Public class Program
{
Public int num;
Public string name;
// Initialize two fields in a constructor without Parameters
Public Program ()
{
Num = 39;
Name = "Shelly ";
}

// Constructor with int Parameters
Public Program (int id)
{
Num = id;
Name = "Shelly ";
}
// Constructor with string Parameters
Public Program (string sname)
{
Num = 49;
Name = sname;
}
Public void Print ()
{
Console. WriteLine ("name = {0}, num = {1}", name, num );
}
Static void Main (string [] args)
{
// Instantiate the object
Program p1 = new Program ();
P1.Print ();

// Int
Program p2 = new Program (29 );
P2.Print ();

// String
Program p3 = new Program ("Hi ");
P3.Print ();
}
}
}

 

5. Static constructor: no access modifier is required
Example:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace static Constructor
{
Public class Test
{
// Static field
Private static int num;
// Static Constructor
Static Test ()
{
Num = 10;
Console. WriteLine ("num =" + num );
}

Static void Main (string [] args)
{
Test test = new Test ();
}
}
}

6. destructor

Important Content of the Destructor:
1) without Parameters
2) each class has only one
3) without access Modifiers
4) Same as the class name, ~ Prefix
5) You cannot display and call the destructor in the Code. During the garbage collection process, the spammers analyze the code and make sure that there is no way to reference this object in the code.

Note: If you do not need it, you should only release external resources of the object, and do not access other objects.


Example:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace destructor
{
Public class ShowClass1
{
Public ShowClass1 ()
{
Console. WriteLine ("Call constructor: ShowClass1 ");
}

~ ShowClass1 ()
{
Console. WriteLine ("Call destructor: ShowClass1 ");
}
}
Public class ShowClass2
{
Public ShowClass2 ()
{
Console. WriteLine ("Call constructor: ShowClass2 ");
}

~ ShowClass2 ()
{
Console. WriteLine ("Call destructor: ShowClass2 ");
}
Public void test ()
{
ShowClass1 sc1 = new ShowClass1 ();
ShowClass2 sc2 = new ShowClass2 ();
}

}
Class Program
{
Static void Main (string [] args)
{

// Destructor call
Program pro = new Program ();
Pro. test ();

Console. WriteLine ();
}

// Destructor instantiation
Public void test ()
{
ShowClass1 sc1 = new ShowClass1 ();
ShowClass2 sc2 = new ShowClass2 ();
}

}
}


Differences between constructor and destructor:
1) instantiation: constructor: called once when every new instance of the class is created
Destructor: called before each instance is destroyed
2) static: constructor: only called once, before any static variable of the class is accessed for the first time, or before the instance is created
Destructor: No. Only instances have destructor.

7. readonly keywords

1) differences with const:
Field initialization: readonly: It can be initialized when a field is declared or in the constructor.
Const: can only be initialized when declared
Field status: readonly: static pages can be non-static
Const: always static
2) Example:

 
Public class Evaluation
{
Public int x;
// Declare the readonly Field
Public readonly int y = 25; // initialization value during Declaration
Public readonly int z;
Public Evaluation (int x)
{
This. x = x;
// Initialize the value in the constructor
Z = 25;
Console. WriteLine ("x = {0}, y = {1}, z = {2}", x, y, z );
}
Public Evaluation (int num1, int num2, int num3)
{
X = num1;
Y = num2;
Z = num3;
Console. WriteLine ("x = {0}, y = {1}, z = {2}", x, y, z );
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Class Program
{
Static void Main (string [] args)
{
// The readonly keyword is initialized in the Declaration or constructor.
Evaluation show = new Evaluation (11,21, 31 );
Evaluation show2 = new Evaluation (22 );

}


}

 
View Code

8. this Keyword: access its own members in the class, which is often used in the indexer.
Usage scope: instantiate the strength accessors of constructors, instance methods, attributes and indexes, Members used for partition classification, local variables or parameters, and real parameters used as call Methods
Example:
Public Round (double r)
{
This. r = r; // used for grouping members and Parameters
}

 


9. indexer: array accessors of Objects
A) scope of use: It can be declared wherever attributes can be declared
B) Syntax:
Access modifier data type this [data type identifier]
{
Get {};
Set {};
}
C) Note: when there is only one element, there is no need to use the indexer for access. The indexer is dedicated to indexing arrays.

 

 

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.