
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Declare a const array in C#
In C#, use readonly to declare a const array.
public static readonly string[] a = { "Car", "Motorbike", "Cab" };
In readonly, you can set the value at runtime as well unlike const.
Another alternative of achieving what we saw above −
public ReadOnlyCollection<string> a { get { return new List<string> { "Car", "Motorbike", "Cab" }.AsReadOnly();}}
.NET framework 4.5 brings an improvement to what we saw −
public ReadOnlyCollection<string> a { get; } = new ReadOnlyCollection<string>( new string[] { "Car", "Motorbike", "Cab" } );
- Related Questions & Answers
- Difference between const int*, const int * const, and int const * in C/C++?
- Difference between const int*, const int * const, and int const * in C
- Difference between const char* p, char * const p, and const char * const p in C
- How to declare a two-dimensional array in C#
- How to declare an array in C#?
- Const Qualifier in C
- Const cast in C++
- Const member functions in C++
- How do I declare a 2d array in C++ using new
- How to modify a const variable in C?
- What is the difference between const int*, const int * const, and int const *?
- How to declare an empty string array in C#?
- How do I declare a two-dimensional array in C++ using new?
- Declare a C/C++ function returning pointer to array of integer function pointers
- How to declare a static String array in Java
Advertisements