Common C # keyword detailed tutorial (more comprehensive) _ Basic application

Source: Internet
Author: User
Tags constructor modifier modifiers volatile

Whether you're a novice or a veteran, do you have some key words in C # that don't touch your head? Now I'm going to learn the meaning of these keywords with all of you.

Type

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 a method. Declares a parameterless method that does not return a value in the following form:

Ovid SampleMethod ();

Var

Variables declared in the scope of a method can have implicit type var. An implicitly typed local variable is a strongly typed variable (as if you have declared the type), but the compiler determines the type. There are return values

Reference type

Class

Class is declared using the keyword class

Commissioned

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 a named or anonymous method. Delegates are similar to function pointers in C + +, but delegates are type safe and reliable.

Interface

An interface contains only the signature of a method, property, event, or indexer. The implementation of the member is done in the class or struct that implements the 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) are inherited 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

A string type represents a sequence consisting of 0 or more Unicode characters. A string is an alias for a string in the. NET Framework.

Although string is a reference type, defining the equality operator (= = and!=) is to compare the value of a string object, rather than a reference. This makes testing of string equality more intuitive.

Modifiers

Override

To extend or modify an abstract or virtual implementation of an inherited method, property, indexer, or event, you must use the override modifier.

Virtual

The virtual keyword is used to decorate methods, properties, indexers, or event declarations and to enable them to be overridden in a derived class. 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 more than one thread that executes concurrently. Fields declared as volatile are not limited by compiler optimizations (assumed to be accessed by a single thread). This ensures that the field renders the most recent value at any time.

Unsafe

The unsafe keyword represents an unsafe context that is required by any operation involving pointers.

Static

Use the static modifier to declare static members that belong to the type itself and not to a particular object. The static modifier is available for classes, fields, methods, properties, operators, events, and constructors, but not for indexers, destructors, or types other than classes.

Sealed

When you apply the sealed modifier to a class, this modifier prevents other classes from inheriting from the 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 a field. When a field declaration includes a readonly modifier, the field assignment introduced by the Declaration can only appear as part of the declaration, or it may appear in a constructor of the same class.

extern

The extern modifier is used to declare a method that is implemented externally. The common usage of the extern modifier is to use with the DllImport property when you are using the Interop service to dial unmanaged code. In this case, you must also declare the method 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 the field or local variable is constant and cannot be modified.

Abstract

The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can only be a base class for other classes. A member that is marked as abstract or contained in an abstract class must be implemented by a class derived from the abstract class.

Access modifiers

Internal

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

Private

The Private keyword is a member access modifier. Private access is the minimum level of access allowed. Private members are accessible only in the class and struct in which they are declared

Protected

The protected keyword is a member access modifier. A protected member can be accessed by a derived class instance in the class in which it resides.

Public

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

Abstract can be used in conjunction with classes, methods, properties, indexers, and events.
Identifies a class or method that must be implemented that can be extended but not materialized.

As a conversion operator, which returns null if the conversion fails.

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

Catch defines a block of code that executes block code when a particular type of exception is thrown.
See try and finally.

Checked is both an operator and a statement.
Ensures that when the compiler runs, it checks for overflow when an integer type operation or conversion occurs.

A const identifies a variable value that can be computed at compile time, that is, the value that cannot be modified is assigned.

delegate specifies a declaration as a delegate type. The delegate encapsulates the method as a callable entity,
Can be invoked in a delegate entity.

An enum represents a value type for a named constant cluster.

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

Explicit an operator that defines the user's custom conversion operator
Typically used to convert an builtin type to a user-defined type or reverse operation.
The display conversion operator must be invoked when converting.

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

Finally, a code block is defined, which executes quickly after the program control leaves the try code. See try and catch.

Fixed to assign a pointer to a variable at a pinned memory location when a block of code executes.

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.
Typically used to convert a predefined type to a user-defined type or reverse operation.
The implicit conversion operator must be used at the time of 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 load an operator.

Out identifies the parameter to which a parameter value is affected, but when the method is passed in,
This parameter does not need to be initialized first.

params declares an array of arguments. If used, the last parameter specified must be modified.
Optional parameters are allowed.

ReadOnly The value that identifies a variable cannot be modified after initialization.

Ref identifies a parameter to which a parameter value may be affected.

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

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 inline types.

Throw throws an exception.

The try exception handles one of the components of the code block. Try code blocks include the possibility of
The code that throws the exception. See Catch and finally keywords.

typeof an operator that returns the type of the incoming argument.

Unchecked prevents overflow checking.

Unsafe a code block, method, or class that contains a pointer operation.

Using when used for namespaces, the Using keyword allows access to types in that namespace.
Without specifying its full name. Also used to define the scope of the finalization operation.
Virtual a method modifier that identifies the method that can be mounted.

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

A generic constructor is always public and, if it is a private type, indicates that the class cannot be instantiated, which is typically used for classes that contain only static members.

Declaring an overload of a virtual method in a derived class requires that you add the override keyword in the declaration, and you cannot have new,static or virtual modifiers.

A sealed class uses the sealed modifier in a declaration, which prevents the class from being inherited by other classes.

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.