English 中文(简体)
2. 启动财产的良好做法?
原标题:Good practices for initialising properties?
  • 时间:2010-04-22 17:15:06
  •  标签:
  • c#

I have a class property that is a list of strings, List. Sometimes this property is null or if it has been set but the list is empty then count is 0. However elsewhere in my code I need to check whether this property is set, so currently my code check whether it s null and count is 0 which seems messy.

if(objectA.folders is null)
{
    if(objectA.folders.count == 0)
    {
      // do something
    }
}

Any recommendation on how this should be handled? Maybe I should always initialise the property so that it s never null?

最佳回答

当我把财产列为财产时,我通常会想像以下一点(这并非一种可怕的安全法典):

public class SomeObject
{
    private List<string> _myList = null;

    public List<string> MyList
    {
        get
        {
            if(_myList == null)
                _myList = new List<string>();
            return _myList;
        }
    }
}

那么,你的法律就永远不必检查,因为如果使用,财产就会开始。 那么,你只得检查数字。

问题回答

现在是你的法典 越权放弃Null Pointer的例外情况,你就会对Null进行检查,如果它无效,你就会再次试图进入不存在的物体。

如果您提出申请,则收集资料的无效内容与收集资料的收集工作无关,那么,是的,我要说,你应该总是开始收集,这样就可以从剩余的法典中删除无效的检查。

这种做法只有在财产设定者不允许在开户后将其改为无效时才具有意义。

你有三种选择(需要根据您的项目决定):

  1. Create a method to check for NullOrNoElements. Pro: Allows both null and no entries. Con: You have to call it everywhere you want to use the property.
  2. Preinitialize with a list. Pro: Thread-save and very easy. Con: will use memory even when not used (depending on how many instances you have this may be a problem)
  3. Lazy initialize Pro: Does only use memory when really used. Con: NOT thread save.
private List<string> lp = null;
public List<string> ListProp 
{ 
    get 
    { 
        if(lp == null) 
            lp = new List<string>(); 
        return lp; 
    } 
}

你们总是可以开始财产,这样它就有一个空洞的清单。 然后,你只能检查计票财产。

List<String> Folder = Enumerable.Empty<String>();

我曾经为ICollection物体写了一种延伸方法,如果这些物体是无效或空洞的,就可以加以检查。

public static Boolean IsNullOrEmpty<T>(this ICollection<T> collection)
{
    return collection == null ? true : collection.Count() == 0;
}

public static Boolean IsPopulated<T>(this ICollection<T> collection)
{
    return collection != null ? collection.Count() > 0 : false;
}

你们可以在单一的国际红十字与红新月联会中这样做。

if(objectA.folders is null || objectA.folders.count == 0)

或者,你可以在检查你这一状况并收回结果的班子中创造毛坯财产

public bool objectA.FolderIsNullOrEmpty
{
    get { return objectA.folders is null || objectA.folders.count == 0;}
}

如果对你的申请不作改动,我会重新拟定名单,以开始。

你们可以通过在建筑商中初步确定目标来处理这一问题。 这通常是在做这类事情的情况下。 虽然我看不出你现行法典有任何错误。 尚未发现任何点,只是废物记忆。

这是一个好的问题。 我要补充一种方法,反对你可以使用的eg。

public virtual FoldersNullOrEmpty()
{
   return (folders == null || folders.count == 0)
}

我几乎总是开始编制名单,甚至确保,如果由任何编造者揭露,名单就会被定为无效。 这使得使用这些工具更加容易。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签