Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of commonly used C # keywords

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "detailed explanation of commonly used C# keywords". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the "detailed explanation of commonly used C# keywords"!

Types

Void

When used as the return type of a method, the void keyword specifies that the method does not return a value.

Void is not allowed in the parameter list of the method. Declare a method with no parameters and no return value in the following form:

Ovid SampleMethod ()

Var

Variables declared in the method scope can have the implicit type var. The local variable of an implicit type is a strongly typed variable (as if you had already declared the type), but the type is determined by the compiler. There is a return value

Reference type

Class

Classes are declared using the keyword class

Entrust

The declaration of a delegate type is similar to a method signature, with a return value and any number of parameters of any type:

Public delegate void TestDelegate (string message)

Public delegate int TestDelegate (MyType m, long num)

Delegate is a reference type that can be used to encapsulate named or anonymous methods. Delegates are similar to function pointers in C++; however, delegates are type-safe and reliable.

Interface

The interface contains only the signature of the method, property, event, or indexer. Members are implemented in a class or structure that implements an interface

Object

The object type is an alias for Object in the .NET Framework. In C# 's unified type system, all types (predefined types, user-defined types, reference types, and value types) inherit directly or indirectly from Object. You can assign any type of value to a variable of type object. The process of converting a variable of a value type to an object is called boxing. The process of converting a variable of an object type to a value type is called unboxing.

String

The string type represents a sequence of zero or more Unicode characters. String is an alias for String in the .NET Framework.

Although string is a reference type, the equality operators (= = and! =) are defined to compare the values of string objects, not references. This makes the testing of string equality more intuitive.

Modifier

Override

To extend or modify abstract or virtual implementations of inherited methods, properties, indexers, or events, you must use the override modifier.

Virtual

The virtual keyword is used to modify methods, properties, indexers, or event declarations and make them overridden in derived classes. For example, this method can be overridden by any class that inherits it.

Volatile

The volatile keyword indicates that a field can be modified by multiple threads executing simultaneously. Fields declared as volatile are not restricted by compiler optimization (assuming they are accessed by a single thread). This ensures that the field renders the latest value at any time.

Unsafe

The unsafe keyword indicates an insecure context, which is required for any operation involving pointers.

Static

Use the static modifier to declare static members that belong to the type itself and not to a specific object. Static modifiers can be used for classes, fields, methods, properties, operators, events, and constructors, but not for indexers, destructors, or types other than classes.

Sealed

When a sealed modifier is applied to a class, this modifier prevents other classes from inheriting from that class. In the following example, class B inherits from class A, but no class can inherit from class B.

Readonly

The readonly keyword is a modifier that can be used on fields. When a field declaration includes a readonly modifier, the field assignment introduced by the declaration can only appear as part of the declaration or in a constructor of the same class.

Extern

The extern modifier is used to declare methods that are implemented externally. A common use of the extern modifier is with the DllImport property when calling unmanaged code using an Interop service. In this case, the method must also be declared as static

Event

The event keyword is used to declare events in the publisher class.

Const

The const keyword is used to modify the declaration of a field or local variable. It specifies that the value of a field or local variable is constant and cannot be modified.

Abstract

Abstract modifiers can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in the class declaration to indicate that a class can only be the base class of other classes. Members marked as abstract or contained in an abstract class must be implemented through a class derived from the abstract class.

Access modifier

Internal

The internal keyword is an access modifier for types and type members. Internal types or members are accessible only in files of the same assembly

Private

The private keyword is a member access modifier. Private access is the lowest level of access allowed. Private members are accessible only in the classes and structures in which they are declared

Protected

The protected keyword is a member access modifier. Protected members can be accessed by an instance of a derived class in the class they are in.

Public

The public keyword is an access modifier for types and type members. Public access is the highest level of access allowed.

Abstract can be used with classes, methods, properties, indexers, and events

Identifies a class or method that can be extended but cannot be materialized and must be implemented.

As a conversion operator that returns null if the conversion fails.

Base is used to access base class members that are hidden by members of the same name in a derived class or construction.

Catch defines a block of code that executes code within the block when a particular type of exception is thrown.

See try and finally.

Checked is both an operator and a statement.

Make sure that while the compiler is running, check for overflows during integer type operations or conversions.

Const identifies a variable value that can be calculated at compile time, that is, an immutable value once assigned.

Delegate specifies a declaration as a delegate type. Delegate encapsulates the method as a callable entity

Can be called in the delegate entity.

Enum represents the value type of a named constant cluster.

Event allows a member of a class or object to provide notification, which must be a delegate type.

Explicit an operator that defines a user-defined conversion operator

It is commonly used to convert built-in types to user-defined types or reverse operations.

The display conversion operator is called when you must convert again.

Extern identifies a method that will be implemented externally (usually not in the c # language).

Finally defines a block of code that executes after program control leaves the try code quickly. See try and catch.

When fixed executes a block of code, it assigns a pointer to a variable in a fixed memory location.

Foreach is used to traverse the elements of a cluster.

Goto A jump statement that redirects program execution to a label statement.

Implicit an operator that defines a user-defined conversion operator.

It is commonly used to convert predefined types to user-defined types or reverse operations.

The implicit conversion operator must be used during conversion.

Interface specifies a declaration as the interface type, that is, the contract that the implementation class or construct must follow.

Internal an access modifier.

Namespace defines the type and namespace of a logical group.

Operator is used to declare or overload an operator.

Out identifies a parameter whose value is affected, but when passing in a method

This parameter does not need to be initialized first.

Params declares an array of parameters. If used, the last parameter specified must be modified.

Optional parameters are allowed.

Readonly identifies that the value of a variable cannot be modified after initialization.

Ref identifies a parameter whose value may be affected.

Sealed prevents types from being derived and methods and property from being overridden.

Sizeof an operator that returns the length of a value type in byte.

Stackalloc returns a pointer to a block of memory allocated on the heap.

Struct struct is a value type that can declare constants, fields, methods, property,

Indexers, operators, constructors, and embedded types.

Throw throws an exception.

Part of the try exception handling code block. The try code block includes a possible

Code that throws an exception. See the catch and finally keywords.

Typeof an operator that returns the type of the parameter passed in.

Unchecked forbids overflow checking.

Unsafe annotations contain code blocks, methods, or classes for pointer operations.

Using when used in a namespace, the using keyword allows access to types in that namespace

Without specifying its full name. It is also used to define the scope of finalization operations.

Virtual A method modifier that identifies a method that can be overridden.

Volatile identifies an attribute that can be modified by the operating system, some hardware devices, or concurrent threads.

The general constructor is always of type public, and if it is of type private, it indicates that the class cannot be instantiated, which is usually used for classes that contain only static members.

Declaring an overload on a virtual method in a derived class requires the override keyword to be added to the declaration, and there can be no new,static or virtual modifiers.

Sealed classes use the sealed modifier in their declarations, which prevents the class from being inherited by other classes.

At this point, I believe you have a deeper understanding of the "detailed explanation of commonly used C# keywords". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report