C# 控制台如何工作?

标签 c# oop

这是一个非常基本的问题,

但是怎么样,比如

Console.Write("test");

工作?

Console 是一个类,而不是一个对象。

我以控制台为例,因为它很常用,但我见过很多使用 Class.method() 而不是 object.method() 的例子。

最佳答案

该方法称为静态方法:Static Classes and Static Class Members (C# Programming Guide) .

您不需要实例来调用静态类成员:

A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

关于C# 控制台如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409971/

相关文章:

c# - 如何从控制面板的区域配置中读取 "List Separator"设置?

c# - 如果从未读取值,让多个线程写入同一个 bool 是否安全?

用于检测未使用方法的 C# 属性

c# - 为什么我在使用带有安全 URL 的 HttpResponseMessage 时得到 401 未经授权?

java - 责任链模式是否可以很好地替代一系列条件?

c# - CS 的 MacVIM 语法文件

php - 从类外部访问类属性

C++ 嵌套类可访问性

Java:是否有一种数据结构可以像多重映射一样工作,但接受重复的键?

php - OOP PHP 混淆,从过程到 OO PHP 的过渡