Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Array Initialization

As someone with a background in C, C++ and assembly language, one thing that has always bothered me about C# is that I cannot do something like this:

struct OperatorType
{
    string Operator;
    TokenType Type;
}

protected static OperatorType[] Operators = {
    { "{", TokenType.OpenBrace }
};

I'd like to declare this so that it doesn't require allocation and initialization at run time but C# won't allow it.

Yes, I understand I can initialize with new OperatoryType() { Operator = "{", Type = TokenType.OpenBrace }. But doesn't that involve a run-time allocation and initialization of memory? I know it's not that much overhead but I don't understand why it is necessary here.

Can anyone explain why this bit of additional overhead is required by C#, or possibly a way to do this without the run-time allocation?

like image 471
Jonathan Wood Avatar asked Apr 25 '11 17:04

Jonathan Wood


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

Allocations, whether in C# or C++, always require allocation at runtime. It's more a matter of when the allocations occur.

If you allocate in a static constructor in C#, you'll allocate sometime before the first usage of the type. That should be safe, and not really have any extra overhead when compared to your C++ version.

Also, one thing to realize - runtime allocations in C# tend to be much cheaper than C++. This is one huge advantage of the garbage collector. It's very likely this is a classic case of premature optimization. I would recommend not worrying about this unless you've found a real, measured performance issue.

like image 102
Reed Copsey Avatar answered Oct 04 '22 07:10

Reed Copsey


The problem with having static data simply mapped from a binary file is that it requires the format of all the data to be frozen at compile time. Since the runtime determines the layout of structures (including strings and arrays), the compiler can't know what the layout will be. Even if the compiler emitted the layout of the current runtime, it could be broken by future runtimes. This means that only structs with explicit layout that were defined in that assembly could be statically mapped from a file, which is quite frankly not too useful.

The C# Language Specification 4.0, section 7.6.10.4 says:

Except in an unsafe context (§18.1), the layout of arrays is unspecified.

In 18.5.8:

The order in which members are packed into a struct is unspecified.

The layout of string actually changed between .NET 3.5 and 4.0 (they removed a field); it went from

[NonSerialized]
private int m_arrayLength;
[NonSerialized]
private char m_firstChar;
[NonSerialized]
private int m_stringLength;

to

[NonSerialized, ForceTokenStabilization]
private char m_firstChar;
[NonSerialized]
private int m_stringLength;

This isn't a problem in C or C++ because the compiler determines the layout of structs. Of course this also means that you have to recompile everything that uses a struct/class in order to change its layout.

like image 38
Gabe Avatar answered Oct 04 '22 05:10

Gabe