Apoorv Apoorv - 4 months ago 15
Vb.net Question

Why do I need default while accessing a structure?

I had this VB method which I converted to C#

Public Function GetToricData(ByRef SaveFileName As String) As ToricDataType
Dim filenumber As Short
Dim InString As String = ""
filenumber = FreeFile()
If System.IO.File.Exists(SaveFileName) Then
With GetToricData
FileOpen(filenumber, SaveFileName, OpenMode.Input)
Input(filenumber, InString)
If IsNumeric(InString) Then
.ToricAppDiam = Convert.ToDouble(InString)
End If
Input(filenumber, InString)
If IsNumeric(InString) Then
.NumToricOpticZones = Convert.ToInt16(InString)
End If
FileClose(filenumber)
End With
Else
With GetToricData
.NumToricOpticZones = 20
.ToricAppDiam = 3
End With
SaveToricData(SaveFileName, GetToricData)
End If
End Function


Post conversion to C# using Telerik Online Converter I got this

public static FrontEndStructures.ToricDataType GetToricData(ref string SaveFileName)
{
var title = ((AssemblyTitleAttribute)System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0]).Title;
FrontEndStructures.ToricDataType functionReturnValue = default(FrontEndStructures.ToricDataType);
//FrontEndStructures.ToricDataType functionReturnValue = new FrontEndStructures.ToricDataType();

int filenumber = 0;
string InString = "";
filenumber = FileSystem.FreeFile();
if (System.IO.File.Exists(SaveFileName))
{
var _with38 = functionReturnValue;
FileSystem.FileOpen(filenumber, SaveFileName, OpenMode.Input);
FileSystem.Input(filenumber,ref InString);
if (Information.IsNumeric(InString))
{
_with38.ToricAppDiam = Convert.ToDouble(InString);
}
FileSystem.Input(filenumber,ref InString);
if (Information.IsNumeric(InString))
{
_with38.NumToricOpticZones = Convert.ToInt16(InString);
}
FileSystem.FileClose(filenumber);
}
else
{
var _with39 = functionReturnValue;
_with39.NumToricOpticZones = 20;
_with39.ToricAppDiam = 3;
SaveToricData(ref SaveFileName, ref _with39);
}
return functionReturnValue;
}


ToricDataType is defined as

public struct ToricDataType
{
public double ToricAppDiam;
public short NumToricOpticZones;
}


I want to know that what is the significance of the line


FrontEndStructures.ToricDataType functionReturnValue =
default(FrontEndStructures.ToricDataType);


why did the online tool use Default keyword and why new keyword couldnt be used ?
As the class is a structure type and Structures are value types so where is the memory allocated (Stack or Heap ) and how data members are initialized ?

I am expecting a detail explanation on this and want to know that using new operator would help me achieve the same purpose or not ?

Answer

The C# null is not quite the same as the VB Nothing. In VB, Nothing means the default value for a type. For a reference type that is a null reference and for a value type that is a value with all its fields set to default values. In C#, null means a null reference for reference types but cannot be used to initialise value types. The default operator does the equivalent job of providing a default value for a value type.

Note also that, unlike VB, C# functions have no default return value. In VB you can assign to the implicit local variable that is named after the function and that value will be returned if no explicit Return statement is hit. In C#, you are obliged to explicitly return something, hence the additional local variable. I would really suggest not using that implicit return in VB anyway. Explicit returns make your code clearer.

EDIT: You can use the new keyword to create a new instance and have the same effect. The one scenario where you must use default is when the type you pass it is a generic type parameter, in which case you normally won't know whether it is a reference type or value type.