Apoorv
Apoorv

Reputation: 2043

C# alternative to VB code

I have this VB code

 Public Function InitMeridDataType() As MeridDataType
        Dim OutData As MeridDataType
        With OutData
            .NumCurves = 0
            Erase .CurveData
        End With
        Return OutData
    End Function

I converted this code into C# using Telerik Online Tool

The C# code is

 public static MeridDataType InitMeridDataType()
        {
            MeridDataType OutData = default(MeridDataType);
            var _with1 = OutData;
            _with1.NumCurves = 0;
            _with1.CurveData = null;
            return OutData;
        }

MeridDataType is defined in VB as

Public Structure MeridDataType
        Dim NumCurves As Short
        Dim CurveData() As CurveDataType
    End Structure

I feel like there is something seriously wrong with the C# code. Shouldn't it be returning _with1 ?

A version of which I have written is

public static MeridDataType InitMeridDataType()
            {
                MeridDataType OutData = default(MeridDataType);
                //var _with1 = OutData;
                OutData .NumCurves = 0;
                OutData .CurveData = null;
                return OutData;
            }

any leads ?

Upvotes: 1

Views: 96

Answers (3)

I feel like there is something seriously wrong with the C# code.

I agree. MeridDataType is defined as a value type, so attempting to modify OutData through a copy _with1 will not have the intended effect: An unmodified OutData will be returned.

If all you need to do is set some properties on a data object, this is about as simple as it gets in C#:

public static MeridDataType InitMeridDataType()
{
   return new MeridDataType()  // the parentheses are actually optional
   {
       NumCurves = 0,
       CurveData = null
   };
}

(By the way: Explicitly setting NumCurves and CurveData to "null values" is not actually necessary. This is already done implicitly through the default constructor, which initializes the underlying memory to "all zeroes", so resetting these properties has no additional effect! Nevertheless, this may be a good idea because it makes explicit what your values should be initialized to.)


P.S.: new T() or default(T)?

TL;DR: Use new T(…) for creating T instances, not default(T)… even for structs.

Getting a fresh instance of some type T is properly done via new T(). Unlike default(T), this states your intent clearly, and has the benefit of always doing what you want:

  • For value types (structs) only, C#'s default(T) is equivalent to the new T() default constructor: both give you a instance of the value type that has its underlying memory initialized to "all zeroes". But it's a sneaky way of creating a fresh instance, and not very transparent. Better reserve the use of default(T) for situations when you don't know the actual type T, e.g. inside a generic method.

  • For reference types (e.g. class, interface), default(T) will return a null reference, so subsequently accessing T will produce an exception.

Upvotes: 3

kurakura88
kurakura88

Reputation: 2305

Reference to default: https://msdn.microsoft.com/en-us/library/xwth0h0d.aspx. If MeridDataType is a struct, then your code should work fine. If it is a class, then it would be better to instantiate with default constructor.

public static MeridDataType InitMeridDataType()
{
   MeridDataType OutData = new MeridDataType();
   OutData.NumCurves = 0;
   OutData.CurveData = null;
   return OutData;
}

Upvotes: 0

Neil
Neil

Reputation: 11929

OutData and _with1 are the same object, so it really doesn't match which you return.

Upvotes: -1

Related Questions