Trending September 2023 # Learn How Nameof Operator Work In C# With Sample Code # Suggested October 2023 # Top 10 Popular | Dacvumuahe.com

Trending September 2023 # Learn How Nameof Operator Work In C# With Sample Code # Suggested October 2023 # Top 10 Popular

You are reading the article Learn How Nameof Operator Work In C# With Sample Code updated in September 2023 on the website Dacvumuahe.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn How Nameof Operator Work In C# With Sample Code

Introduction to C# nameof

To avoid the hardcoded string in the code, we make use of an operator called nameof operator in C# which accepts the elements of the code names and returns the same element’s string literal and a class name, all the members of the class like variables, methods, constants can be passed as parameters to the nameof operator and a string literal is returned and this string literal returned from using nameof operator is a special kind of string literal because the given name is checked by the compiler to see if something exists by that name and if it is referred by the visual studio.

Start Your Free Software Development Course

The syntax of the nameof operator in C# is as follows:

nameof(Expression) Working of nameof operator in C#

The nameof operator in C# is often overlooked but it is one of the very useful operators in C#.

It is an operator without which we can perform coding but to point out certain errors during the compile-time, the nameof operator is very essential.

Names of different code artifacts are returned by the nameof operator in C#.

Consider the below program to demonstrate the use of nameof operator in C#:

Code:

using System; namespace Name { class Check { static void Main(string[] args) { Console.WriteLine(nameof(Name)); Console.WriteLine(nameof(Check)); Console.WriteLine(nameof(Check.Main)); Console.WriteLine(nameof(Main)); Console.WriteLine(nameof(args)); } } }

Output:

In the above program, a namespace called name is defined. Then a class called check is defined. Then the main method is called. Then the nameof operator is used to identify the name of the namespace, name of the program, the main method, and its arguments. The point of using the nameof operator here is that if any of the items of the code names are changed, then we must change all the nameof operators used in the code otherwise failure of build occurs. If we are using the nameof operator, then the compiler will point out the bugs otherwise these will be found when the program is used in production.

Examples of C# nameof

Here are the following examples mention below

Example #1

Program to demonstrate the nameof operator.

Code:

using System; namespace Features { class Example { int[] array = new int[5]; public static void Main(string[] args) { Example ex1 = new Example(); try { ex1.showop(ex1.array); } catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine("The name of the method is: "+nameof(ex1.showop)); } } int showop(int[] x) { x[5] = 12; return x[5]; } } }

Output:

In the above program, a namespace called Features is defined. Then a class called Example is defined. Then an integer array called array is defined. Then the main method is called. Then an instance of the class Example is created. Then try and catch block is defined. Then the showop method is called using the instance of the Example class. Then the method that throws the exception is displayed. The point of using the nameof operator here is that if any of the items of the code names are changed, then we must change all the nameof operators used in the code otherwise failure of build occurs. If we are using the nameof operator, then the compiler will point out the bugs otherwise these will be found when the program is used in production.

Example #2

Program to demonstrate the use of the nameof operator.

Code:

using System; public class Check { private static DateTime Today = DateTime.Now; public string Name { get; set; } public static void Main(string[] args) { var local_Time = DateTime.Now.ToLocalTime(); Console.WriteLine(nameof(local_Time)); Console.WriteLine(nameof(args)); Console.WriteLine(nameof(System.IO)); Console.WriteLine(nameof(Main)); Console.WriteLine(nameof(Check)); Console.WriteLine(nameof(Check.Today)); Console.WriteLine(nameof(Check.Name)); } }

Output:

Example #3

Program to demonstrate the nameof operator.

Code:

using System; class Check { static void Taste(int argu) { Console.WriteLine(nameof(argu)); Console.WriteLine(argu); var plane = "Shobha"; Console.WriteLine(nameof(plane)); } static void Main() { Taste(200); } }

Output:

In the above program, a class called check is defined. Then a method called taste is defined within the class in which the nameof operator is used. Then a variable is defined on which the nameof operator is used again. Then the main method is called from which the defined method Taste is called along with passing the parameter. The point of using the nameof operator here is that if any of the items of the code names are changed, then we must change all the nameof operators used in the code otherwise failure of build occurs. If we are using the nameof operator, then the compiler will point out the bugs otherwise these will be found when the program is used in production.

Conclusion

In this tutorial, we understand the concept of nameof operator in C# through definition, the syntax of nameof operator in C#, working of nameof operator in C# through examples, and their outputs.

Recommended Articles

This is a guide to C# nameof. Here we discuss the working of nameof operator in C# and examples for better understanding. You may also look at the following articles to learn more –

You're reading Learn How Nameof Operator Work In C# With Sample Code

Update the detailed information about Learn How Nameof Operator Work In C# With Sample Code on the Dacvumuahe.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!