473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you have a Constant Array in C#

hello,

how do you declare a multi-dimentional constant array in C#
does this make sense?

Thanks
Nov 15 '05 #1
5 17236
Can you be a little more specific. If you want a multi dimensional
array, you can do this:

// Create a multi dimensional array.
int[,] pintArray = new int[2, 2];

This is different than a jagged array, which is like this:

int[][] pintArray = {new int[2], new int[2]};

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"News VS.NET ( MS ILM )" <sq**********@h otmail.com> wrote in message
news:eU******** ******@TK2MSFTN GP09.phx.gbl...
hello,

how do you declare a multi-dimentional constant array in C#
does this make sense?

Thanks

Nov 15 '05 #2
how do you declare a multi-dimentional constant array in C#
does this make sense?


No, arrays can't be constant. You can store an array reference in a
static read-only field, but that will only prevent the reference from
being modified, not the content for the array being referred to.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3
From what I can tell, the only thing that can be constant in C# is instance
variables. Supposedly C# is safer than C++. Not that I believe that
farther than you can throw a house-sized boulder.

--
----------
Will Pittenger
E-Mail: mailto:wi****** ******@verizon. net
All mail filtered by Qurb (www.qurb.com)
"News VS.NET ( MS ILM )" <sq**********@h otmail.com> wrote in message
news:eU******** ******@TK2MSFTN GP09.phx.gbl...
hello,

how do you declare a multi-dimentional constant array in C#
does this make sense?

Thanks

Nov 15 '05 #4
Will,
From what I can tell, the only thing that can be constant in C# is instance
variables.


On the contrary, consts are implicitly static.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #5
Hi NoName,

Yes. In effect.

Create a static class which contains your array.
Give the class read-only indexer properties.

What do you want these constant arrays for ?

Regards,
Fergus
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5149
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For example, given: $data = serialize(array('4.50','0.00','0.00'));
6
6452
by: sruojlim | last post by:
Hi everyone I'm posting again this question as my system was down at time of the first posting...well something in the newsgroups was down. I'd like to know how i should declare a constant as an array with two dimension. I've tried the following but seems the syntax is somehow wrong. Can someone help me in doing so or any clue would be fully appreciate. Here is my bugged code:
9
6507
by: xtra | last post by:
Hi Folk Is it possible to declare a constant array e.g. dim i() as string i(1) = ... i(2) = ... seems to work but the following does not const i(1) = ....
27
2063
by: Peter Ammon | last post by:
My code obfuscator gave me this: char buff; to which gcc retorted: "ISO C90 forbids variable-size array 'buff'" and checking the standard, it appears that commas are indeed forbidden from being in a constant expression.
2
1815
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference specifies that 'the only valid values of a constant declarator constant expressions'. A constant expression is defined as 'an expression that can be fully evaluated at compile-time ... Therefore, the only possible values for constants of reference...
1
3065
by: Andrzej 'Foxy' D. | last post by:
Hi! I cannot figure out, why is the following code incorrect: const int array = { 1 }; const int var = 1; template<int I> struct Blah {};
5
7883
by: fred | last post by:
Hi, Can someone explain me why gcc-4.0 gives me the 'Initializer element is not constant' error with this code ? Everything seems to be constant here... #include <stdio.h> typedef struct { int a; int b;} t; int main(int argc, char** argv) {
25
5720
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with the constant 0x00010203040506070809 where array == 0x00, array == 0x01, etc. How do I do that. Obviously I can't use memcmp() with the 0x00010203040506070809 since the compiler will use that constant as an address and give me a segfault. Other...
13
27441
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
3
4066
by: longbrmb | last post by:
I'm new to C++/CLI and my main background is Java. I'm trying to create an array of constants as a static member of a class. An example is shown below: public ref class TestClass { public: static const array<constant int>^ stuff = {1,2,3,4,5}; }; This gives me compiler error C4538 which basically says that you can't
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.