none
Convert Dictionary Net 4.0 to Net 2.0 ? RRS feed

  • Question

  • I want convert Dictionary Net 4.0 to Net 2.0.

    Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>() {
          { "A", o => MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) },
          { "B", o => MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) }
            };


    Friday, October 20, 2017 7:07 AM

Answers

  • I don't know that I follow what you're trying to say. LINQ is not about writing. LINQ is a query only interface. You cannot change data via LINQ, only manipulate the view of the data. LINQ isn't available in .NET 2.0 so you cannot use it there. You can easily tell if you're using LINQ or not by looking at the using statements. If there is a System.Linq reference then you are including LINQ. It wouldn't compile in .NET 2.0.

    The code you posted isn't LINQ. You don't need a namespace for it as it is a compiler only syntax. The aforementioned code gets converted to the following by the compiler. It is completely irrelevant what version of the framework you target as no runtime components are needed (other than generics).

    /*Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>() {
          { "A", o => MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) },
          { "B", o => MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) }
            };
    */
    Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>();
    
    ItemHandlers.Add("A", o => ...);
    ItemHandlers.Add("B", o => ...);
    
    
    

    Of course the lambda expressions are also compiler sugar and do not require runtime support. The compiler will auto-generate private functions (or perhaps a nested class, depending) to support them.

    The following would be a LINQ query.

    //LINQ syntax
    var handler = (from h in ItemHandlers
                   where h.Key == "A"
                   select h.Value).FirstOrDefault();
    
    //Extension method equivalent
    var handler2 = ItemHandlers.FirstOrDefault(h => h.Key == "A");

    To convert that to non-LINQ you'd end up having to do this.

    Action<IWin32Window> action= null;
    foreach (var handler in ItemHandlers)
    {
       if (handler.Key == "A")
       {
          action = handler.Value;
          break;
       };
    };
    
    Of course the above is using 'var' as well which is another compiler syntax.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by lamtriendong Monday, October 30, 2017 6:48 AM
    Tuesday, October 24, 2017 1:35 PM
    Moderator

  • Hi lamtriendong,

    I try the following way to run your code under net2.0 . The following code for your reference.

    1: Visual Studio 2017

    2: Net Framework 2.0

         Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>();
    
            private void Form3_Load(object sender, EventArgs e)
            {
                //      Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>() {
                //{ "A", o => MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) },
                //{ "B", o => MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) }
    
                ItemHandlers.Add("A", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
                ItemHandlers.Add("B", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //KeyValuePair<T,K>
                foreach (KeyValuePair<string, Action<IWin32Window>> kv in ItemHandlers)
                {
                    if(kv.Key=="B")
                    {
                        Console.WriteLine(kv.Key + kv.Value);
                        Action<IWin32Window> ac = (Action<IWin32Window>)kv.Value;
                        ac.Invoke(this);
                        break;
                    }
                }
            }
    

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Fei HuModerator Sunday, October 29, 2017 6:05 AM
    • Marked as answer by lamtriendong Monday, October 30, 2017 6:47 AM
    Thursday, October 26, 2017 7:08 AM
    Moderator

All replies

  • Dictionary hasn't changed between .NET 2 and 4. What problem are you having? The code you posted is using the dictionary initializer syntax but that is compiler sugar.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, October 20, 2017 2:23 PM
    Moderator
  • I use VS2005, because in the Dictionary there used linq while net2.0 had no use of linq. This is the code that runs in net4.0, you see the attached file http://www.mediafire.com/file/dc47qzis72owdkt/ReducingLinqDictionary.rar
    Monday, October 23, 2017 2:35 AM

  • Hi lamtriendong,

    >>I want convert Dictionary Net 4.0 to Net 2.0.

    As far as I know, if you want use Net 2.0, you can change your application target framework to ,NET 2.0.  That way, your Dictionary is based on NET 2.0.

    Add: your code I tested under 2.0, is not compiled wrong. I can run this code.

    If we have any misunderstanding, you can include all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages or you can upload your demo to OneDrive(Including your test material). We can download it and debugging. This will help us quickly analyze your problem.
    Share OneDrive files and folders:
    https://support.office.com/en-us/article/Share-OneDrive-files-and-folders-9fcc2f7d-de0c-4cec-93b0-a82024800c07

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, October 23, 2017 2:59 AM
    Moderator
  • I'm using net 4.0 for my application but I'm learning the problem of removing linq in this case. I send you back for example: http://www.mediafire.com/file/dc47qzis72owdkt/ReducingLinqDictionary.rar
    Monday, October 23, 2017 3:27 AM
  • "I use VS2005, because in the Dictionary there used linq while net2.0 had no use of linq"

    So you want to port the code written with .NET 4 back to code written for .NET 2.0? That doesn't make a lot of sense to me as the version of .NET you're using has nothing to do with whether you're using LINQ or not. Why are you needing to go back to .NET 2 specifically?

    As for the code, you don't need to change anything. Firstly this code isn't using LINQ at all. Secondly the code is compiler sugar over creating a dictionary and then adding items to it. It'll compile on any version of .NET and has nothing to do with the targeted framework. You can confirm this by changing the project's target framework to .NET v2 and it should still compile. However you are using generics so you won't be able to go back prior to .NET v2.

    Sorry, I should clarify. VS 2005 is no longer a supported product and therefore you should not be using it. VS 2005 does not support the above syntax. But if you're using VS 2015 or higher then you can use the above syntax as it'll be using a newer version of the compiler that supports the syntax. The target framework can be .NET 2+ as it is compiler generated code only.


    Michael Taylor http://www.michaeltaylorp3.net


    • Edited by CoolDadTxModerator Monday, October 23, 2017 5:14 AM Updated comments about VS2005
    Monday, October 23, 2017 5:08 AM
    Moderator
  • I do not think like you, I think Dictionary uses two ways of writing using linq and writing does not use linq, of course net4.0 is better than net2.0, maybe this is hard work, can not reducing linq, I would like to put up a look at how to write remove using linq, linq also depends on linq article to understand linq to understand also linq cryptic posts.
    Tuesday, October 24, 2017 6:51 AM
  • I don't know that I follow what you're trying to say. LINQ is not about writing. LINQ is a query only interface. You cannot change data via LINQ, only manipulate the view of the data. LINQ isn't available in .NET 2.0 so you cannot use it there. You can easily tell if you're using LINQ or not by looking at the using statements. If there is a System.Linq reference then you are including LINQ. It wouldn't compile in .NET 2.0.

    The code you posted isn't LINQ. You don't need a namespace for it as it is a compiler only syntax. The aforementioned code gets converted to the following by the compiler. It is completely irrelevant what version of the framework you target as no runtime components are needed (other than generics).

    /*Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>() {
          { "A", o => MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) },
          { "B", o => MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) }
            };
    */
    Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>();
    
    ItemHandlers.Add("A", o => ...);
    ItemHandlers.Add("B", o => ...);
    
    
    

    Of course the lambda expressions are also compiler sugar and do not require runtime support. The compiler will auto-generate private functions (or perhaps a nested class, depending) to support them.

    The following would be a LINQ query.

    //LINQ syntax
    var handler = (from h in ItemHandlers
                   where h.Key == "A"
                   select h.Value).FirstOrDefault();
    
    //Extension method equivalent
    var handler2 = ItemHandlers.FirstOrDefault(h => h.Key == "A");

    To convert that to non-LINQ you'd end up having to do this.

    Action<IWin32Window> action= null;
    foreach (var handler in ItemHandlers)
    {
       if (handler.Key == "A")
       {
          action = handler.Value;
          break;
       };
    };
    
    Of course the above is using 'var' as well which is another compiler syntax.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by lamtriendong Monday, October 30, 2017 6:48 AM
    Tuesday, October 24, 2017 1:35 PM
    Moderator
  • LINQ I know to be as little as declarative namespaces, ... for an example that contains LINQ code to run. You write: LINQ is not written. LINQ is a query-only interface. I understand you speak but my English is not good. I do not know much about LINQ and I'm afraid to use them so I need someone who knows LINQ to solve my problem, do you have documentation about LINQ writing from basic to advanced and read to understand most ? Because I do not know many LINQ should read online reading understand which is also ambiguous. Read your article written 2 concepts LINQ and lambda expressions, I have not yet distinguished the two concepts LINQ and lambda expressions the same and different ? My code gives you the LINQ syntax and the Extension method equivalent, I also understand that you have to convert to non-LINQ. the above is using 'var' as well which is another compiler syntax, i understand you say foreach (var handler in ItemHandlers) => foreach (Action <iWin32Window> handler in ItemHandlers) still error, you see the attached image file , Can you help me know use LINQ code and use code Non Linq ?

    Wednesday, October 25, 2017 3:19 AM
  • There is really nothing to undo to not use LINQ. If you stick with foreach and method calls you won't be using LINQ.

    The error you're getting appears to be because you're putting the code directly in a class. You cannot do that. The only thing that can be directly nested in a class is declarations (field, event, method, constructor, property). All other statements must be inside a method (or property) body. Put all the code you're trying to compile inside a method (probably Main if you're creating a console application. 

    //This code should be auto-generated by a new console application
    public class Program
    {
       public static void Main ()
       {
          //Put your statements here
       }
    }
    


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 25, 2017 5:29 AM
    Moderator
  • Something like this could work:

                public delegate void ItemHandler(IWin32Window o);

    Dictionary<string, ItemHandler> ItemHandlers = new Dictionary<string, ItemHandler>() { { "A", new ItemHandler(o => MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) ) }, { "B", new ItemHandler(o => MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) ) } };

    EDIT: Oh, I forgot Action<T> already exists on .NET v2.0, so there's no need to  change anything at all.

    And btw, nothing stops you to write:

                Dictionary<string,  Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>()
                {
                    { "A", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }) },
                    { "B", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }) }
                };

    because anonymous function is defined in C# Specification v2 section 19.2 already. No need to use lambda expression at all (As noted by CoolDadTx, your problem is not want to use "lambda expression", not really about LINQ).






    Wednesday, October 25, 2017 6:18 AM
    Answerer
  • Hi cheong00!
    I followed your instructions to get an error, see the attached image file http://www.mediafire.com/view/wm3zpg9r7dmu1c6/RemoveLinq3.jpg
    Thursday, October 26, 2017 3:11 AM
  • Hi CoolDadTx!
    I do not think I need to use the keyword public, static, main, I fixed the following, but still error, see the attachment.  http://www.mediafire.com/view/legp9lpfjmejfu2/RemoveLinq2.jpg
    Thursday, October 26, 2017 3:12 AM
  • Oh, Collection Initializer is new feature introduced in C# 3.0, so you have to change it to traditional Collection<T>.Add() when you want it in strict .NET v2.0 syntax.

                Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>();
                ItemHandlers.Add("A", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
                ItemHandlers.Add("B", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
              

    Thursday, October 26, 2017 3:29 AM
    Answerer
  • I just found that Visual Studio 2015 (in fact csc.exe too) supports restricting language version.

    Invoked by "Project Properties" -> "Build" -> "Advanced..." of lower right corner, the dialog contains "Language Version" settings. Set it to "ISO-2" for "C# 2.0 Strict" rules of syntax.


    Thursday, October 26, 2017 4:43 AM
    Answerer

  • Hi lamtriendong,

    I try the following way to run your code under net2.0 . The following code for your reference.

    1: Visual Studio 2017

    2: Net Framework 2.0

         Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>();
    
            private void Form3_Load(object sender, EventArgs e)
            {
                //      Dictionary<string, Action<IWin32Window>> ItemHandlers = new Dictionary<string, Action<IWin32Window>>() {
                //{ "A", o => MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) },
                //{ "B", o => MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information) }
    
                ItemHandlers.Add("A", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command A is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
                ItemHandlers.Add("B", new Action<IWin32Window>(delegate (IWin32Window o) { MessageBox.Show(o, "The command B is executed", "Dx Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //KeyValuePair<T,K>
                foreach (KeyValuePair<string, Action<IWin32Window>> kv in ItemHandlers)
                {
                    if(kv.Key=="B")
                    {
                        Console.WriteLine(kv.Key + kv.Value);
                        Action<IWin32Window> ac = (Action<IWin32Window>)kv.Value;
                        ac.Invoke(this);
                        break;
                    }
                }
            }
    

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Fei HuModerator Sunday, October 29, 2017 6:05 AM
    • Marked as answer by lamtriendong Monday, October 30, 2017 6:47 AM
    Thursday, October 26, 2017 7:08 AM
    Moderator
  • @lamtriendong, you're getting that error because you're not using the correct type in the foreach. I used var which is a valid type in .NET 2.0 as it is compiler sugar, again. The actual type of the items you're enumerating is KeyValuePair<string, Action<IWin32Window>>.

    foreach (KeyValuePair<string, Action<IWin32Window>> item in ItemHandlers)
    

    But I'm still really confused as to why you're going through this exercise at all. Can you explain why you need to do this? Switching to .NET 2.0 doesn't require any changes to your original code whatsoever. It is all valid just by retargeting the project to .NET 2.0. Why are you going through the exercise of reverting all the existing code to an older version of the C# language? Hopefully this isn't going into any code you intend to release as it is a really bad idea. Not using the newer language constructs (even with an older framework) is just a bad approach and will make it harder to maintain over time.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, October 26, 2017 1:42 PM
    Moderator
  • Hi everybody !
    First of all, I thank you so much for answering my question. I learned programming myself so I did not understand how computers work, I was learning LINQ and no LINQ, my LINQ level of understanding was poor so I asked you to answer me, you do not criticize me, Do you have documentation that talks about LINQ going from basic to advanced readability most ?
    Monday, October 30, 2017 6:47 AM

  • Hi  lamtriendong,

    >>, Do you have documentation that talks about LINQ going from basic to advanced readability most ?

    You can learn the documentation at Microsoft and deepen your LINQ study.

    Getting Started with LINQ in C#:
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-with-linq

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 30, 2017 7:19 AM
    Moderator