Using braces without defining function, structure, etc.

When analyzing the source code of Orchard.cms, I found an interesting code when the brackets in C # were not used to define any internal object. This seems to have been done for some use of context. Could you clarify what the purpose of using parentheses like this?

Here is a sample code:

builder.RegisterType<DefaultOrchardHost>().As<IOrchardHost>().As<IEventHandler>().SingleInstance(); { builder.RegisterType<ShellSettingsManager>().As<IShellSettingsManager>().SingleInstance(); builder.RegisterType<ShellContextFactory>().As<IShellContextFactory>().SingleInstance(); { builder.RegisterType<ShellDescriptorCache>().As<IShellDescriptorCache>().SingleInstance(); builder.RegisterType<CompositionStrategy>().As<ICompositionStrategy>().SingleInstance(); { builder.RegisterType<ShellContainerRegistrations>().As<IShellContainerRegistrations>().SingleInstance(); builder.RegisterType<ExtensionLoaderCoordinator>().As<IExtensionLoaderCoordinator>().SingleInstance(); builder.RegisterType<ExtensionMonitoringCoordinator>().As<IExtensionMonitoringCoordinator>().SingleInstance(); builder.RegisterType<ExtensionManager>().As<IExtensionManager>().SingleInstance(); { builder.RegisterType<ExtensionHarvester>().As<IExtensionHarvester>().SingleInstance(); builder.RegisterType<ModuleFolders>().As<IExtensionFolders>().SingleInstance() .WithParameter(new NamedParameter("paths", new[] { "~/Modules" })); builder.RegisterType<CoreModuleFolders>().As<IExtensionFolders>().SingleInstance() .WithParameter(new NamedParameter("paths", new[] { "~/Core" })); builder.RegisterType<ThemeFolders>().As<IExtensionFolders>().SingleInstance() .WithParameter(new NamedParameter("paths", new[] { "~/Themes" })); builder.RegisterType<CoreExtensionLoader>().As<IExtensionLoader>().SingleInstance(); builder.RegisterType<ReferencedExtensionLoader>().As<IExtensionLoader>().SingleInstance(); builder.RegisterType<PrecompiledExtensionLoader>().As<IExtensionLoader>().SingleInstance(); builder.RegisterType<DynamicExtensionLoader>().As<IExtensionLoader>().SingleInstance(); builder.RegisterType<RawThemeExtensionLoader>().As<IExtensionLoader>().SingleInstance(); } } builder.RegisterType<ShellContainerFactory>().As<IShellContainerFactory>().SingleInstance(); } builder.RegisterType<DefaultProcessingEngine>().As<IProcessingEngine>().SingleInstance(); } 
+4
source share
3 answers

The goal in this case is only so that the developer / person can understand how these types are hierarchically related when reading the code to make it easier to understand the code .

(Using curly braces like this will indent lines in most IDEs like Visual Studio)

+5
source

Can you clarify what purpose brackets like this use?

These are just blocks. They form a scope, as in:

 Method1(1); { int i = 1; // i is local to this {} block Method1(i++); } // Here i does not exist any more 

Note that the block is in no way connected with the previous statement; the absence of empty lines in your example can be misleading.

But in the published code, the variables are not declared. Thus, they are completely superfluous. I assume they are left over from preprocessing the code.

+4
source

This is just an organizational thing in this case. Curly braces create scope so that any variables declared inside the brackets are local to this block of code. So the normal goal for them.

However, in the example you give, nothing is declared in any of them. In this case, Orchard people just do it as an organizational method to make the code more understandable. I assume that if you look at the classes in the example you specified, you will see that they are connected hierarchically, just as they are grouped by curly braces.

+1
source

Source: https://habr.com/ru/post/1493656/


All Articles