0

I am relatively new in C#, and I have a 2 classes. The first class has a public struct and the second one is trying to declare the struct. I am not good enough in english, but this is the code so that you may understand :

First Class :

class PE
{
#region NT Headers

    [StructLayout(LayoutKind.Explicit)]
    public struct IMAGE_NT_HEADERS32
    {
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public char[] Signature;

        [FieldOffset(4)]
        public IMAGE_FILE_HEADER FileHeader;

        [FieldOffset(24)]
        public IMAGE_OPTIONAL_HEADER32 OptionalHeader;
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct IMAGE_NT_HEADERS64
    {
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public char[] Signature;

        [FieldOffset(4)]
        public IMAGE_FILE_HEADER FileHeader;

        [FieldOffset(24)]
        public IMAGE_OPTIONAL_HEADER64 OptionalHeader;
    }
#endregion
}

Second Class :

class Main
{
    struct NT_Header
    {
        Namespace.PE.IMAGE_NT_HEADERS32 INTSIGN = 
            new Namespace.PE.IMAGE_NT_HEADERS32(); //HERE IS THE ERROR!
    }
}

It always raises the error... Thanks for helps!

4
  • Just to save you a bunch of time: Reading the Portable Executable (PE) Header in C# Commented Dec 22, 2012 at 2:53
  • Certainly, though, the compiler gives you a line number pointing you right to where the error is? Why have you omitted this, making it harder for us to help you? Commented Dec 22, 2012 at 2:53
  • yeah, but i just want to declare it in the second class, because i need it.. Commented Dec 22, 2012 at 2:54
  • it has an error at the main class, declaring the INTSIGN @Jontathon Reinhart Commented Dec 22, 2012 at 2:55

2 Answers 2

3

You can't assign a default value to a struct field, like you're doing in NT_Header. It's not legal C#. This would compile:

class Main
{
struct NT_Header
    {
        public Namespace.PE.IMAGE_NT_HEADERS32 INTSIGN;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@newbie no, you're just a newbie :)
Why is it not allowed in C# ?
@user3241228: Because the language specification says it's not. If I had to guess why, it's because the default value of a struct type is always the same as the one produced by the default constructor, which is a memory-zeroed value. Imagine constructing a large array of these structs. There is never a chance to invoke the field initializers.
-2

C# Compiler does not allow the inline initialization of value type instance fields. However, if you need to initialize a field, you need to mark it as "Static".

2 Comments

If it's static, then it's no longer an instance field, and will not serve the purpose of what the OP was doing.
I was trying to say C# Compiler allows value type to use only its static fields for inline initialization not the instance fields. Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.