TheDeveloperBlog.com

Home | Contact Us

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

C# Region

This C# example program uses the #region directive. This directive helps organize code blocks.

Region. The #region directive partitions your code.

With #region, we can organize code into blocks that can be expanded or collapsed visually. We must indicate the start of the region and the end of the region.

Example. In this example, we demonstrate some common usages of the #region directive. Please pay attention to the #region and #endregion directives. These are balanced throughout the source file.

You can optionally specify an identifier for the region, such as with #region FIELDS. When you compile this program, the #region directives do not affect the intermediate language output.

C# program that uses region directives

using System;

#region
class Example
{
}
#endregion

class Program
{
    #region FIELDS
    int _field1;
    int _field2;
    #endregion

    static void Main()
    {
	#region BODY
	Console.WriteLine("Hello world!");
	Console.WriteLine("How are you today?");
	#endregion
    }
}

Output

Hello world!
How are you today?

Looking at the example screenshot. In the code example, we see the complete program. In the screenshot, we see the code in Visual Studio environment with the #region areas collapsed.

Tip: To collapse a region, click the plus-minus sign on the left margin of Visual Studio next to your code.

Visual Studio

 

Summary. The #region directive operates at the level of the text. It does not require any particular scope to be balanced in the file. Instead, it strictly blocks off regions of the source text. It does not affect the final compiled metadata.

Note: Region is not ideal. But it is better to use #region than not to organize your code.

Also: Consider instead using more classes and scopes. This enables compile-time detection of problems.

Compile-Time Error


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