none
Return null instead an object RRS feed

  • Question

  • Hello,

    Why return null instead object of Car?

    using System;
    using System.Linq;
    
    namespace P961006_P2
    {
        class Car
        {
            public int Power { get; set; }
            public string  Name { get; set; }
            public string Color { get; set; }
            public Car(int power ,string name , string color)
            {
                Power = power;
                Name = name;
                Color = color;
            }
            public override string ToString()
            {
                return $"Name is {Name} and Color is {Color}";
            }
        }
        class Program
        {
    
            static Array LinqOverArrayList(Car[] car)
            {
                Array mycars = (from i in car select new { i.Color, i.Name }).ToArray();
                return mycars;
            }
            static void Main(string[] args)
            {
                Car[] car = new Car[]
                {
                    new Car(1300,"P","Red"),
                    new Car(1600,"Peugeot", "Blue")
                };
    
                Array array = LinqOverArrayList(car);
    
                foreach (var item in array)
                {
                    Car str = (item as Car);
                    Console.WriteLine(str.Name);
                }
    
                Console.ReadLine();
            }
        }
    }
    

    Wednesday, December 27, 2017 9:35 PM

Answers

  • This is what will happen with anonymous types, try this

    using System;
    using System.Linq;
    
    namespace ConsoleApp5
    {
        class Car
        {
            public int Power { get; set; }
            public string Name { get; set; }
            public string Color { get; set; }
            public Car(int power, string name, string color)
            {
                Power = power;
                Name = name;
                Color = color;
            }
            public override string ToString()
            {
                return $"Name is {Name} and Color is {Color}";
            }
        }
        class Program
        {
            static Array LinqOverArrayList(Car[] car)
            {
                Array mycars = (from i in car select new { i.Color, i.Name }).ToArray();
                return mycars;
            }
            static void Main(string[] args)
            {
                Car[] car = new Car[]
                {
                    new Car(1300,"P","Red"),
                    new Car(1600,"Peugeot", "Blue")
                };
    
                var array = LinqOverArrayList(car);
    
                foreach (var item in array)
                {
                    dynamic newtype = item;
         
                    Console.WriteLine($"Name: '{newtype.Name}' Color: '{newtype.Color}'");
                }
    
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Arash_89 Wednesday, December 27, 2017 10:07 PM
    Wednesday, December 27, 2017 10:06 PM
    Moderator
  • Thank you, If suppose we have in earlier .Net and dynamic keyword doesn't exist.

    Now What we do?

    Hello,

    First off I would suggest not even going down this road in regards to using dynamic in the case of your code example. When using dynamic there are no compiler checks which makes the code brittle, look a the documentation for in depth information, then read the following StackOverflow post.

    So what would you do when dynamic is not available? Strong type the method to an array of Car.

    Hopefully this answers your current question.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Arash_89 Friday, December 29, 2017 10:56 AM
    Friday, December 29, 2017 10:45 AM
    Moderator

All replies

  • This is what will happen with anonymous types, try this

    using System;
    using System.Linq;
    
    namespace ConsoleApp5
    {
        class Car
        {
            public int Power { get; set; }
            public string Name { get; set; }
            public string Color { get; set; }
            public Car(int power, string name, string color)
            {
                Power = power;
                Name = name;
                Color = color;
            }
            public override string ToString()
            {
                return $"Name is {Name} and Color is {Color}";
            }
        }
        class Program
        {
            static Array LinqOverArrayList(Car[] car)
            {
                Array mycars = (from i in car select new { i.Color, i.Name }).ToArray();
                return mycars;
            }
            static void Main(string[] args)
            {
                Car[] car = new Car[]
                {
                    new Car(1300,"P","Red"),
                    new Car(1600,"Peugeot", "Blue")
                };
    
                var array = LinqOverArrayList(car);
    
                foreach (var item in array)
                {
                    dynamic newtype = item;
         
                    Console.WriteLine($"Name: '{newtype.Name}' Color: '{newtype.Color}'");
                }
    
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Arash_89 Wednesday, December 27, 2017 10:07 PM
    Wednesday, December 27, 2017 10:06 PM
    Moderator
  • Thank you, If suppose we have in earlier .Net and dynamic keyword doesn't exist.

    Now What we do?

    Friday, December 29, 2017 7:39 AM
  • Thank you, If suppose we have in earlier .Net and dynamic keyword doesn't exist.

    Now What we do?

    Hello,

    First off I would suggest not even going down this road in regards to using dynamic in the case of your code example. When using dynamic there are no compiler checks which makes the code brittle, look a the documentation for in depth information, then read the following StackOverflow post.

    So what would you do when dynamic is not available? Strong type the method to an array of Car.

    Hopefully this answers your current question.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Arash_89 Friday, December 29, 2017 10:56 AM
    Friday, December 29, 2017 10:45 AM
    Moderator