TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Variable Initializer for Class Field

This C# program shows variable initializer syntax. This syntax lets you simplify programs.

Variable initializers simplify programs.

This syntax enables you to directly assign a field at the class level. This results in the C# compiler generating appropriate code at the beginning of all constructor method bodies.

Constructor

Example. In this code example we demonstrate the execution of the variable initializer syntax form. When you have a field in a class, you can assign that field directly inside the class declaration.

This feature exists to allow shorter source text for class declarations in the C# language. The resulting code is equivalent to having the variable initialized at the top of all constructor method bodies.

Tip: You can assign a field in a class directly at the class level. This results in shorter source text but no execution change.

C# program that uses variable initializer in class

using System;

class Test
{
    string _value = Program.GetPi();
    public Test()
    {
	// Simple constructor statements.
	Console.WriteLine("Test constructor: " + _value);
    }
}

class Program
{
    public static string GetPi()
    {
	// This method returns a string representation of PI.
	Console.WriteLine("GetPi initialized");
	return Math.PI.ToString();
    }

    static void Main()
    {
	// Create an instance of the Test class.
	Test test = new Test();
    }
}

Output

GetPi initialized
Test constructor: 3.14159265358979

The program introduces the Test class, which uses a variable initialization syntax form, and the Program class which provides the Main entry point. First, the Test class is constructed and allocated upon the managed heap.

Then: The constructor calls GetPi and stores its result. This method call was inserted at the top of the constructor in the compiled code.

Note: Clear and concise syntax in the source text is important from a maintainability perspective.

Order. Let's address the order of execution of the statements and examine the compiled intermediate language. If we inspect the .ctor() member, we see that the initialization of the field _value was inserted at the top of the constructor body.

IL Disassembler TutorialIntermediate Language

Summary. We saw a variable initializer. This is a syntax form that allows you to assign a field to the result of an expression or method invocation. The C# compiler generates code at the top of all relevant constructors.

Thus: This syntax will have no unique effect on execution and is purely syntactic sugar in the language.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf