Replace the use of "var" when creating a variable in C # 2.0

0

Good my query is this:

var items = new Normaslist();

    try
    {
        conn.Open();

        //using (var dr = cmd.ExecuteReader())
        using (SqlDataReader dr = cmd.ExecuteReader())
        {

in the code shown I define a variable items as a class Normaslist ()

[Serializable()]
public class Normaslist
{

public Normaslist()
{
    //Lista = new List<Normalegal>();
}

public List<Normalegal> Lista = new List<Normalegal>();//{ get { return this.p_lista; } set { this.p_lista= value; }}
private List<Normalegal> p_lista;

public string TotalRowsn { get { return this.p_TotalRowsn; } set { this.p_TotalRowsn = value; } }
private string p_TotalRowsn;

public string TotalPagesn { get { return this.p_TotalPagesn; } set { this.p_TotalPagesn = value; } }
private string p_TotalPagesn;


}

I have tried replacing the definition of items with

    Normaslist items = new Normaslist();

Also with

    Normaslist items = Activator.CreateInstance<Normaslist>();

Both work normally when I compile them in c # 4.0 but when compiling it with c # 2.0 it does not work correctly. It does not show compile errors either. I would like to know if this is the correct way to define the variable items without using var in c # 2.0

    
asked by ASP.NEET 02.03.2018 в 19:47
source

1 answer

1

It can be from version 3 of C #:

  

As of Visual C # 3.0, variables that are declared in the method scope may have an implicit " var "

type.

link

using (var dr = cmd.ExecuteReader())           //Implícita
using (SqlDataReader dr = cmd.ExecuteReader()) //Explícita
    
answered by 02.03.2018 в 20:17