Question

boobyy on Mon, 07 Apr 2014 23:11:36


Can someone help me write the code in old ways, like in C# 2005. 

For example there is a class

 public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

now I am initiating this class in to an array object, how i can rewrite it in longer version.  For me that is more readable.

 Product[] products = new Product[] 
        { 
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
        };

2) Also if instead of writing the above code I want to have these objects in List<product> how would I do that.


i am a novice and a student


Sponsored



Replies

darnold924 on Mon, 07 Apr 2014 23:28:07


Can someone help me write the code in old ways, like in C# 2005.

That is  a good one! :)

 

PaulLinton on Tue, 08 Apr 2014 00:47:42


"I want to have these objects in List<product> "

change

Product[]

to

List<Product>

in both places

"how i can rewrite it in longer version"

You can press enter after each comma and line up the properties names one under the other.  That would make it longer.

Joel Engineer on Tue, 08 Apr 2014 00:57:10


See code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> products = new List<Product> { 
               new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
               new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
               new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
           };

        }
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Category { get; set; }
            public decimal Price { get; set; }
        }
    }
}

KeesDijk on Tue, 08 Apr 2014 20:45:21


Hi,

In my opinion most developers find object initializers and collection initializers more readable than the way we needed to write this code before these existed. I would strongly advise you to get used to it.

If you really want to, below is the longest code I could think of:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Program
    {
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Category { get; set; }
            public decimal Price { get; set; }
        }

        private static void Main(string[] args)
        {
            var products = new List<Product>();

            var product1 = new Product();
            product1.Price = 1;
            product1.Category = "Groceries";
            product1.Name = "Tomato Soup";
            product1.Id = 1;

            var product2 = new Product();
            product2.Price = 3.75M;
            product2.Category = "Toys";
            product2.Name = "Yo-yo";
            product2.Id = 2;

            var product3 = new Product();
            product3.Price = 16.99M;
            product3.Category = "Hardware";
            product3.Name = "Hammer";
            product3.Id = 3;

            products.Add(product1);
            products.Add(product2);
            products.Add(product3);

            Console.ReadLine();
        }
    }
}

A tool like resharper lets you switch between object initializers and simple assignments statements easily.

boobyy on Wed, 09 Apr 2014 05:08:45


Thank you very much KeeDojk and thanks to others as well.

KeeDojk, this is what I was talking about thanks.  So after writing this code I realized the version i typed it does not initiate the  product1 or product2. While when we do it the way you did we need to create an object of Product in order to assign the values to product members.   I do understand the code that i wrote but it is not the exact thing after resharper rewrite the code.  What do you think?

 var products = new List<Product>();

            var product1 = new Product();

Anne Jing on Wed, 09 Apr 2014 06:08:37


Hi,

Var Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. So you cannot use var in C# 2005. You can edit the code like below:

List<Product> products = new List<Product>();
Product product1 = new Product();

And you can use these codes below:

class Program
    {
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Category { get; set; }
            public decimal Price { get; set; }
        }

        static void Main(string[] args)
        {
            List<Product> products = new List<Product>();
            products.Add(new Product { Price = 1, Category = "Groceries", Name = "Tomato Soup", Id = 1 });
            products.Add(new Product { Price = 3.75M, Category = "Toys", Name = "Yo-yo", Id = 2 });
            products.Add(new Product { Price = 16.99M, Category = "Hardware", Name = "Hammer", Id = 3 });

            Console.ReadLine();

        }
    }

Best Wishes!

KeesDijk on Wed, 09 Apr 2014 10:00:33


Hi,

Basically the end result in IL is the same, except in the older version you need to think up names for Product1 and Product2 variables yourself, in the newer version the C# compiler thinks up the names for you.

If you don't want to think up the names, you can look at the answer by Anne Jing