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 assign a specific value to all the array elements at the time of initilization?

How to assign a specific value to all the array elements at the time of initilization?

Scheduled Pinned Locked Moved C#
c++data-structurestutorialquestion
2 Posts 2 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.
  • S Offline
    S Offline
    signbit
    wrote on last edited by
    #1

    Hello, I have an array of, say 40, elements, and I want all the elements to contain 255 when the array is initialized, is there a way to do it? (like in C++, we did int array[40] = {255}; )

    - A programmer's national anthem; "AAAAAHHHHH!!!!"

    S 1 Reply Last reply
    0
    • S signbit

      Hello, I have an array of, say 40, elements, and I want all the elements to contain 255 when the array is initialized, is there a way to do it? (like in C++, we did int array[40] = {255}; )

      - A programmer's national anthem; "AAAAAHHHHH!!!!"

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      AFAIK there's no such shortcut, so you have to explicitly assign this value to each element:

      int[] array = new int[13];
      for (int i = 0; i < array.Length; i++)
      array[i] = 255;


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      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