C#静态方法与非静态方法实例分析
这篇文章主要介绍了C#静态方法与非静态方法,重点讲述了静态方法的实际应用,有助于进一步加深对C#程序设计的理解,需要的朋友可以参考下
本文实例分析了C#静态方法与非静态方法,并对其用法进行了较为全面的分析。分享给大家供大家参考。具体分析如下:
通常来说,C#的类中可以包含两种方法:静态方法和非静态方法。
使用了static 修饰符的方法为静态方法,反之则是非静态方法。
静态方法是一种特殊的成员方法,它不属于类的某一个具体的实例,而是属于类本身。所以对静态方法不需要首先创建一个类的实例,而是采用 类名.静态方法 的格式 。
1)static方法是类中的一个成员方法,属于整个类,即不用创建任何对象也可以直接调用。
static内部只能出现static变量和其他static方法,而且static方法中还不能使用this等关键字,因为它是属于整个类。
2)静态方法效率上要比实例化高,静态方法的缺点是不自动进行销毁,而实例化的则可以做销毁。
3)静态方法和静态变量创建后始终使用同一块内存,而使用实例的方式会创建多个内存。
4)那么在程序中什么地方可以利用静态字段和静态构造方法,通常适用于于一些不会经常变化而又频繁使用的数据,比如连接字符串,配置信息等,当满足上面所说的两点时,进行一次读取,以后就可以方便的使用了,同时也节约了托管资源,因为对于静态成员,一个静态字段只标识一个存储位置。对一个类无论创建了多少个实例,它的静态字段永远都只有一个副本。
静态成员存在于内存,非静态成员需要实例化才会分配内存,,所以静态成员不能访问非静态的成员。因为静态成员存在于内存,所以非静态成员可以直接访问类中静态的成员。公用的处理函数,使用静态方法应该没有问题,牵涉到数据共享,静态变量的函数要多考虑,静态变量要小心使用。
静态方法:
原理就是共享代码段;
共享代码段不会产生任何问题;
因为代码段都是给CPU作为"读取"用的,除非你进行恶意"修改"运行时的代码段;
所以静态方法是可以放心使用的;
静态变量:
原理就是共享数据段;
只要没有进行"写入"操作就不会产生问题,但是数据通常都是用于读和写,所以静态变量要注意使用;
下面是一个使用静态方法的简单例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int i = MyClass.Add(7, 11); //调用静态方法 Console.WriteLine(i); Console.ReadKey(); } } class MyClass { public static int Add(int x, int y) { return x + y; } } }
输出结果:18
希望本文所述对大家C#程序设计的学习有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
