C#应该知道的简单的知识点
C#应该知道的简单的知识点
1.本地变量
一看这个标题你可能会一愣,这是个什么东东。看个小例子:
static void main()
{
int a=10;
MyClass mc=new MyClass();
}
呵呵,这里的a与mc就是本地变量,它和字段一样,也保存数据。字段通常保存和对象状态有关的数据,而创建本地变量经常用于保存本地或临时数据。呵呵,简单吧,但是它和实例字段有何区别:
实例字段
本地变量
生存期 从实例创建开始,直到实例不再被访问
从它在块中被声明开始,到块执行结束
隐式初始化 初始化成该类的默认值 没有隐式初始化。如果变量在使用之前没有被赋值,编译器将报错
存储区域 由于实例字段是类的成员,所有的字段都存储在堆里,无论它是指类型还是引用类型 值类型:存储在栈里
引用类型:引用存储在栈里,数据存放在堆里
2.var 关键字
我们知道var可以自动推断变量的类型,如上代码,我们可以发现在声明开始部分提供类型名时,编译器能从初始化语句的右边推断出来它的类型,所以,在声明的开始部分包括显示的类型名多余的。
为了避免这种多余,可以在声明的开始部分的显示类型名的位置使用新的关键字var。上面代码可以改成:
static void main()
{
var a=10;
var mc=new MyClass();
}
3.值参数
使用值参数,通过将实参的值复制到形参的方式把数据传递给方法。方法被调用时,系统做如下操作:
在栈中为形参分配空间
将实参的值复制给形参
4.引用参数(ref)
不会为形参在栈上分配内存
实际情况是,形参的参数名将作为实参变量的别名,指向相同的内存位置
实参必须是变量,在用作实参前必须被赋值。如果是引用类型变量,可以赋值一个引用或者null
5.输出参数(out)
在方法内部,输出参数在能够使用之前必须被赋值。这就意味着参数的初始值是不起作用的,所以没有必要在方法调用前为参数赋值。
在方法返回之前,方法内部任何路径都必须为所有输出参数进行一次赋值。
如下代码:
public void Add(out int outValue) { int var1=outValue+2; }上述代码输出参数在方法赋值之前被读取将会报错。
6.属性(set,get)
属性是一个函数成员
它不为数据的存储分配内存
它可以执行代码
set访问器拥有一个单独的隐式的值参,名称为Value,与属性的类型相同且拥有一个返回类型void
get访问器没有参数,拥有一个与属性类型相同的返回类型。
7.对象初始化语句
我们都知道创建一个对象是由new 后面跟着一个类构造函数及其参数列表组成。
对象初始化语句扩展了创建语法,在表达式的尾部放置了一组成员初始化的语句。允许我们创建新的对象实例时,设置字段和属性的值。
该语法有两种形式:一种形式是包括构造函数的参数列表,另一种是不包括。注意第一种形式可以省略圆括号。
new TypeName {FieldOrProp=InitExpr,FieldOrProp=InitExpr,…}
new TypeName {FieldOrProp=InitExpr,FieldOrProp=InitExpr,…}
示例如下:
public static void main()
{
Point p1=new Point();
Point p2=new Point{X=4,Y=5,Z=6};
Point p3=new Point(9){X=7,Y=8};
}
public class Point
{
public int X=1;
public int Y=2;
public int Z=3;
public Point(int z) { Z=z; }
}
8.this关键字
this关键字在类中使用,是对当前实例的引用。它只能用在下列类成员的代码中
实例构造函数
实例方法
属性和索引器的实例访问权
由于静态成员不是实例的一部分,所以不能在任何静态函数成员的代码中使用this关键字。更适当的说,this用于下列目的:
用于区分类的成员和本地变量或参数
做为调用方法的实参

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

How to write a simple student performance report generator using Java? Student Performance Report Generator is a tool that helps teachers or educators quickly generate student performance reports. This article will introduce how to use Java to write a simple student performance report generator. First, we need to define the student object and student grade object. The student object contains basic information such as the student's name and student number, while the student score object contains information such as the student's subject scores and average grade. The following is the definition of a simple student object: public

How to add local music to Soda Music? You can add your favorite local music to Soda Music APP, but most friends don’t know how to add local music. Next is the graphic tutorial on how to add local music to Soda Music brought by the editor. , interested users come and take a look! Tutorial on using soda music. How to add local music to soda music. 1. First open the soda music APP and click on the [Music] function area at the bottom of the main page; 2. Then enter the play page and click the [three dots] icon in the lower right corner; 3. Finally Expand the function bar below and select the [Download] button to add it to local music.
