全局变量与结构 [英] Global variable vs. Struct

查看:84
本文介绍了全局变量与结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有阅读本文的人您好.我知道变量和结构之间的区别.我真正想知道的是我所举的例子的优缺点.


使用全局变量:

const string _maintTypeCode_Change =``001'';
const string _maintTypeCode_Addition =``021'';
const string _maintTypeCode_Termination =''024'';
const string _maintTypeCode_Reinstatement =``025'';



使用Struc:

struct MaintenanceTypeCode
{
public const string Change =``001'';
public const string Addition =``021'';
public const string Termination =''024'';
公共常量字符串恢复=``025'';
}

Hello to all that read this. I know the difference between a variable and a struc. What I reall want to know are the pros and cons with the examples I have given.


Using Global variables:

const string _maintTypeCode_Change = ''001'';
const string _maintTypeCode_Addition = ''021'';
const string _maintTypeCode_Termination = ''024'';
const string _maintTypeCode_Reinstatement = ''025'';

Or

Using Struc:

struct MaintenanceTypeCode
{
public const string Change = ''001'';
public const string Addition = ''021'';
public const string Termination = ''024'';
public const string Reinstatement = ''025'';
}

推荐答案

结构是一个变量.它只是您定义的类型.最终结果是相同的.全局变量通常不是一个好主意,但有时是不可避免的.
A struct IS a variable. It''s just of a type that you define. The end result is the same. Globals are usually a bad idea, but are unavoidable at times.


C#不支持全局变量.您将不得不将它们扔到一个结构或一个类中.但是,假设全局变量"实际上在类中,并且结构实际上已实例化并分配给类的属性,则使用结构的好处是可以对变量进行逻辑分组并轻松地传递它们.试想一下,如果.Net Framework中的所有变量,常量,函数等等都是全局的,而不是很好地嵌套在名称空间,类和结构中.您最终会遇到一些可怕且无法浏览的内容(例如VB6).

实际上,如果您使用结构,则结构将按值传递,除非您在各处都使用"ref"关键字.这意味着如果使用任何可变(可变)成员并更改值,则将原始结构作为参数(不带"ref")或定义为类的属性传递时不会更新.例如,

C# does not support global variables. You''d have to toss those into a struct or a class. However, assuming your "global variables" are actually in a class and your struct is actually instantiated and assigned to a property on a class, the advantage to using a struct would be that you can group your variables logically and pass them around easily. Imagine if all the variables, constants, functions, and so on in the .Net Framework were all global rather than nicely nested in namespaces, classes, and structs. You''d end up with something horrible and impossible to navigate through (e.g., VB6).

[edited] Actually, if you use a struct, a struct is passed by value unless you use the "ref" keyword everywhere. What this means is that if you use any mutable (changeable) members and change the value, the original structure is not updated when passed as a parameter (without "ref") or defined as a property of a class. For example,

internal static class Preferences
{
  // Nested struct.
  public struct Globals
  {
    public string Option1 = "foo";
    public string Option2 = "bar";
  }

  private static Globals globals;
  public static Globals Globals { get { return globals; } }
}



如果以后再按以下方式更改代码:Preferences.Globals.Option1 = "baz"-全局结构未更改.

最好将它们保留为班级成员.如果只使用常量,则只需将它们定义为类的常量即可.如果需要可变选项,请使用单例类.有关良好的单例模式的更多信息,请参见代码项目或 MSDN [



If later you change the code like so: Preferences.Globals.Option1 = "baz" - the Globals structure is not changed.

Just best to keep these as members of a class. If constants are all you use, just define them as constants of a class. If you need mutable options, use a singleton class. More information about good singleton patterns can be found here on Code Project or on MSDN[^].

But there''s also an entire set of APIs for managing settings like this in the .NET Framework that Visual Studio exposes through the project properties / settings tab. It has the added benefit of being able to merge different user contexts if so desired as well as designer support.


SpeedBump26写道:
SpeedBump26 wrote:

想知道它们的优缺点


使用结构而不是单个变量的一些优点;
1)更容易传递给一组方法(只需将指针/ref传递给该结构)
2)相关变量的逻辑分组


但是从您的代码看来,每个变量对于特定类型来说都是一个谨慎的值.如果是这样,请考虑使用枚举.如果没有,请忽略此!


some pros of using a struct instead of individual variables;
1) easier to pass to methods as a set (just pass a pointer/ref to the struct)
2) logical grouping of related variables


But from your code it looks like each variable is a discreet value for a certain type. If so, consider using an enumeration. If not, ignore this!


这篇关于全局变量与结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆