Is it possible to define a local struct, within a method, in C#?

拜拜、爱过 提交于 2019-11-28 22:23:46

I believe it's not permitted to define named types within a method. As to why, I'll have to speculate. If a type is not going to be used outside, then its existence probably cannot be justified.

You can however define anonymous type variables within a method. It will somewhat resembles structures. A compromise.

public void SomeMethod ()
{
    var anonymousTypeVar = new { x = 5, y = 10 };
}

It is a little late but this is my solution for lists - using anonymous vars as the structs inside of methods:

var list = new[] { new { sn = "a1", sd = "b1" } }.ToList(); // declaring structure
list.Clear();                                               // clearing dummy element
list.Add(new { sn="a", sd="b"});                            // adding real element
foreach (var leaf in list) if (leaf.sn == "a") break;       // using it

Anonymous elements (sn and sd) are somehow read only.

You could do something like this using anonymous types. MSDN examples below:

var v = new { Amount = 108, Message = "Hello" };

or

var productQuery = 
    from prod in products
    select new { prod.Color, prod.Price };

foreach (var v in productQuery)
{
    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}

No, this is not possible. If you are using .net 4.0, you could use Tuple<T1, ..., Tn> to replicate such a thing.

I don't see the reason why you would need such a struct - just use variables with speaking names and this shouldn't be any problem at all. In combination with explicit declaration using the class names there is very little space for ambiguity.

You can define an anonymous type within your method and use it. The anonymous type will be readonly, so it gets you the immutability that is desired of structs. It will not explicitly be a struct, but it will be fully defined and contained within your method.

var myLocalType = new 
    {
        SomeValue = "Foo",
        SomeId = 14
    };

it's not a struct, but mayme a var can help you out here?

var person = new {Name= "John", City = "London"};

it's strong typed so it will be compile time checked

You can create a dynamic type in c# 4.0 to accomplish this task, but its not exactly what you are looking for.

However I believe that the maximum of defining variables as close to where they are used is meant to mean where a variable is introduced into program flow not where the type is declared. I believe that most types have some ability to be reused creating in method types limits you ability to create reusable blocks of code that operates on common data.

No, this is not possible. What is the practical application? How often is it useful to use a structure only within a single method. Microsoft probably saw no compelling reason to implement this feature. In the cases where it could be useful we have anonymous types and anonymous methods.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!