Do variables in static methods become static automatically because they are within static scopes in c#?

后端 未结 7 1040
长情又很酷
长情又很酷 2021-01-17 17:29
public static void DoSomething()
{
int a;
string b;

//..do something
}

In the example above, i have declared two variables. Do they become static

7条回答
  •  感动是毒
    2021-01-17 18:18

    I am positive with your opinion but in the sample code below i'am taking an access violation exception about using protected memory. Because of that maybe it isn't support static local variables but in memory management it can point same address.

    public static byte[] RawSerialize(object anything)
            {
    
                    int rawsize = Marshal.SizeOf(anything);
                    IntPtr buffer = Marshal.AllocHGlobal(rawsize);
                    Marshal.StructureToPtr(anything, buffer, false);
                    byte[] rawdata = new byte[rawsize];
                    Marshal.Copy(buffer, rawdata, 0, rawsize);
                    Marshal.FreeHGlobal(buffer);
                    return rawdata ;
            }
    

提交回复
热议问题