Search code examples
c#.netvariablesvariable-types

Receiving data in specific data type variable or generic variable


I'm calling webservices and catching the provided data. Some of the datatypes are complex and require some gymnastics to get the data I want into a variable with the data's specific type.

  • Example:

        wsModule.setModuleOutput smo = new wsModule.setModuleOutput();
        smo = client2.setModule(smi);
    
        wsModule.resultDetailType[] rdtArray;
        wsModule.resultDetailType rdt = new wsModule.resultDetailType();
        rdtArray = new wsModule.resultDetailType[] { rdt };
    
        rdtArray = smo.modules;
    

Ok, above you can see that to get a module I have to declare an array of resultDetailType, a resultDetailType and insert the latter into the array. Then I give the array the data.

The other approach is way simpler: not declaring any type and get the data into a var type variable.

  • Example:

        wsModule.setModuleOutput smo = new wsModule.setModuleOutput();
        smo = client2.setModule(smi);
    
        var x = smo.modules.FirstOrDefault();
    

My question is, which is better? Which should I use?

[EDIT] Performance and coding time should be considered: there are around 200 webservices to be accessed

Note:

figuring out what is the correct type in which to store the data isn't only an addition of 3 lines of code: it's also some minutes to actually understand and get to it.


Solution

  • If smo.modules is enumerable, then use FirstOrDefault(). No need to create an intermediary variable.

    "Which is better" is a bit vague. Can you provide more info on what you're looking for?