Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use HashSet

How to use HashSet

Scheduled Pinned Locked Moved C#
csharphelptutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raju_shiva
    wrote on last edited by
    #1

    Hi Experts, I am new to C#,I am trying to use

    HashSet stringSet = new HashSet { "abc", "aa" };

    Bu i am getting error as "A new expression requires () or [] after type" Whethier i should include any header file. Thanks Raj

    G L L D 4 Replies Last reply
    0
    • R raju_shiva

      Hi Experts, I am new to C#,I am trying to use

      HashSet stringSet = new HashSet { "abc", "aa" };

      Bu i am getting error as "A new expression requires () or [] after type" Whethier i should include any header file. Thanks Raj

      G Offline
      G Offline
      Gonzalo Cao
      wrote on last edited by
      #2

      try this: HashSet stringSet = new HashSet { "abc", "aa" } ;

      1 Reply Last reply
      0
      • R raju_shiva

        Hi Experts, I am new to C#,I am trying to use

        HashSet stringSet = new HashSet { "abc", "aa" };

        Bu i am getting error as "A new expression requires () or [] after type" Whethier i should include any header file. Thanks Raj

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        HashSet<string> stringSet = new HashSet<string>() { "abc", "aa" };
        foreach (string s in stringSet) log(s);

        works well on .NET 3.5 or 4.0 FWIW: the constructor parentheses are optional here! :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

        R 1 Reply Last reply
        0
        • R raju_shiva

          Hi Experts, I am new to C#,I am trying to use

          HashSet stringSet = new HashSet { "abc", "aa" };

          Bu i am getting error as "A new expression requires () or [] after type" Whethier i should include any header file. Thanks Raj

          L Offline
          L Offline
          Laxman Auti
          wrote on last edited by
          #4

          raju_shiva wrote:

          Bu i am getting error as "A new expression requires () or [] after type"

          Check answer at your previous question http://www.codeproject.com/Messages/3521515/Re-std-map-in-Csharp.aspx[^]

          Knock out 't' from can't, you can if you think you can. :cool:

          1 Reply Last reply
          0
          • L Luc Pattyn

            HashSet<string> stringSet = new HashSet<string>() { "abc", "aa" };
            foreach (string s in stringSet) log(s);

            works well on .NET 3.5 or 4.0 FWIW: the constructor parentheses are optional here! :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

            R Offline
            R Offline
            raju_shiva
            wrote on last edited by
            #5

            Hi sir, I tried the above but i am getting error as "{ "abc", "aa" };" For this line ,its getting error

            Error 1 ; expected E:\WindowsApplication1\WindowsApplication1\Form1.cs 66 63 WindowsApplication1
            Error 2 ; expected E:\WindowsApplication1\WindowsApplication1\Form1.cs 66 70 WindowsApplication1
            Error 3 Invalid expression term ',' E:\WindowsApplication1\WindowsApplication1\Form1.cs 66 70 WindowsApplication1
            Error 4 ; expected E:\WindowsApplication1\WindowsApplication1\Form1.cs 66 72 WindowsApplication1
            Error 5 ; expected E:\WindowsApplication1\WindowsApplication1\Form1.cs 66 77 WindowsApplication1

            1 Reply Last reply
            0
            • R raju_shiva

              Hi Experts, I am new to C#,I am trying to use

              HashSet stringSet = new HashSet { "abc", "aa" };

              Bu i am getting error as "A new expression requires () or [] after type" Whethier i should include any header file. Thanks Raj

              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #6

              The collection initializer syntax "new X { elements }" is new in C# 3.0. The error message looks like you're using a C# 2.0 compiler (VS 2005?). You could rewrite this snippet to C# 2.0 by using explicit calls to Add(), but that still won't help you as the HashSet class is new in .NET 3.5 as well.

              HashSet<string> stringSet = new HashSet<string>();
              stringSet.Add("abc");
              stringSet.Add("aa");

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups