locked
Dim and Var RRS feed

  • Question

  • User577498514 posted

    From my research var is alternative of Dim, is it good to use var in all places for replacement and what is dynamic, some of my fellows use dynamic in function parameter 

    Friday, November 22, 2019 3:14 AM

Answers

  • User-1780421697 posted

    They are not the same. Dim in VB simply means that what follows is a variable declaration.

    For example,
    these two are equivalent:

    Dim x As Integer= 1
    int x = 1;

    In C#,var means that a variable declaration’s type is inferred by the compiler based on usage (initialisation)(mostly used in LINQ when doing some data operations like need IQueryable type of response and late binding), The same can be achieved in VB by simply omitting the type of the declaration.

    However, this also requires that Option Strict and Option Infer are activated:

    Dim x = 1 ' Compiler infers type of x = integer
    var x = 1; // same here.

    ------------------------------------------------------------------------------------------------------


    There's this from Scott Hanselman

    For c# var:

    [var is] a new keyword that means, "I want to declare a variable, but I’m too lazy to write out its type."
    One way to look at the power of VB's Dim operator is to say,
    Dim kind of means, "I want to declare a variable but I can't tell you much about how it behaves until much later."
    Dim lets you do actual late-binding while in C# (today) you do late-binding with reflection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 6:54 AM

All replies

  • User288213138 posted

    Hi Chan Leo,

    From my research var is alternative of Dim, is it good to use var in all places for replacement

    They have their own usage. C# supports type inference and the the dim without a corresponding type in VB, its similar to declaring a type as object in C#.

    More information about dim and var you can refer to this link: https://stackoverflow.com/a/5679014

    what is dynamic

    A dynamic type escapes type checking at compile time; instead, it avoids compile time type checkingre and solves type at run time.

    More information about dynamic type you can refer to this link:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic

    Best regards,

    Sam

    Friday, November 22, 2019 7:23 AM
  • User1120430333 posted

    From my research var is alternative of Dim, is it good to use var in all places for replacement and what is dynamic, some of my fellows use dynamic in function parameter 

    dim and var are similar in strong typing objects in the local scope of a method, which are checked at compile time.

    Dynamic in c#  allows the object to be typed at runtime, which is similar to late binding in VB.NET.

    https://www.tutorialsteacher.com/csharp/csharp-dynamic-type

    http://net-informations.com/faq/oops/binding.htm

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-binding/

    Saturday, November 23, 2019 10:05 AM
  • User-1780421697 posted

    They are not the same. Dim in VB simply means that what follows is a variable declaration.

    For example,
    these two are equivalent:

    Dim x As Integer= 1
    int x = 1;

    In C#,var means that a variable declaration’s type is inferred by the compiler based on usage (initialisation)(mostly used in LINQ when doing some data operations like need IQueryable type of response and late binding), The same can be achieved in VB by simply omitting the type of the declaration.

    However, this also requires that Option Strict and Option Infer are activated:

    Dim x = 1 ' Compiler infers type of x = integer
    var x = 1; // same here.

    ------------------------------------------------------------------------------------------------------


    There's this from Scott Hanselman

    For c# var:

    [var is] a new keyword that means, "I want to declare a variable, but I’m too lazy to write out its type."
    One way to look at the power of VB's Dim operator is to say,
    Dim kind of means, "I want to declare a variable but I can't tell you much about how it behaves until much later."
    Dim lets you do actual late-binding while in C# (today) you do late-binding with reflection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 6:54 AM