Nameof c что это
Перейти к содержимому

Nameof c что это

  • автор:

# nameof Operator

The nameof operator allows you to get the name of a variable, type or member in string form without hard-coding it as a literal.

The operation is evaluated at compile-time, which means that you can rename a referenced identifier, using an IDE’s rename feature, and the name string will update with it.

# Raising PropertyChanged event

Snippet

Console Output

# Basic usage: Printing a variable name

The nameof operator allows you to get the name of a variable, type or member in string form without hard-coding it as a literal. The operation is evaluated at compile-time, which means that you can rename, using an IDE’s rename feature, a referenced identifier and the name string will update with it.

because the name of the variable is "myString". Refactoring the variable name would change the string.

If called on a reference type, the nameof operator returns the name of the current reference, not the name or type name of the underlying object. For example:

# Argument Checking and Guard Clauses

Using the nameof feature makes it easier to refactor method parameters.

# Strongly typed MVC action links

Instead of the usual loosely typed:

You can now make action links strongly typed:

Now if you want to refactor your code and rename the UserController.LogIn method to UserController.SignIn , you don’t need to worry about searching for all string occurrences. The compiler will do the job.

# Handling PropertyChanged events

Snippet

Console Output

https://amdy.su/wp-admin/options-general.php?page=ad-inserter.php#tab-8

Title changed to Everything is on fire and broken Status changed to ShowStopper

C# | nameof Operator

Colton

By using the nameof operator, we can easily get the name of a class, method, or variable. It simply returns a string.

It’s commonly used when we want to reuse the name of a property. There are numeric cases where we would want to have the name of the property. The is a very useful way to keep our code bug free as much as…

Nameof c что это

Оператор nameof возвращает имя класса, имя метода, имя переменной.

Оператор nameof вычисляется во время компиляции, и не влияет на время выполнения программы.

namespace ConsoleApplication1
<
class Book
<
public void ShowBook( int price)
<
Console .WriteLine( "Hello!" );
>
>

class Program
<
static void Main( string [] args)
<
// имя класса
string str1 = nameof(Book);
// str1 = "Book"
Console .WriteLine(str1);

// имя метода
string str2 = nameof(Book.ShowBook);
// str2 = "ShowBook"
Console .WriteLine(str2);

// имя переменной
int myYear;
string str3 = nameof(myYear);
// str3 = "myYear"
Console .WriteLine(str3);
>
>
>

Name already in use

docs / docs / csharp / language-reference / operators / nameof.md

  • Go to file T
  • Go to line L
  • Copy path
  • Copy permalink
  • Open with Desktop
  • View raw
  • Copy raw contents Copy raw contents

Copy raw contents

Copy raw contents

nameof expression (C# reference)

A nameof expression produces the name of a variable, type, or member as the string constant. A nameof expression is evaluated at compile time and has no effect at run time. When the operand is a type or a namespace, the produced name isn’t fully qualified. The following example shows the use of a nameof expression:

You can use a nameof expression to make the argument-checking code more maintainable:

Beginning with C# 11, you can use a nameof expression with a method parameter inside an attribute on a method or its parameter. The following code shows how to do that for an attribute on a method, a local function, and the parameter of a lambda expression:

. code language=»csharp» source=»snippets/shared/NameOfOperator.cs» .

A nameof expression with a parameter is useful when you use the nullable analysis attributes or the CallerArgumentExpression attribute.

When the operand is a verbatim identifier, the @ character isn’t the part of a name, as the following example shows:

C# language specification

For more information, see the Nameof expressions section of the C# language specification, and the C# 11 — Extended nameof scope feature specification.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *