{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/12245","repository_url":"https://api.github.com/repos/dotnet/roslyn","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/12245/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/12245/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/12245/events","html_url":"https://github.com/dotnet/roslyn/issues/12245","id":162637681,"number":12245,"title":"[Proposal] Warn before using static field initialiser that contains other static field from same class ","user":{"login":"neurospeech","id":2217983,"avatar_url":"https://avatars.githubusercontent.com/u/2217983?v=3","gravatar_id":"","url":"https://api.github.com/users/neurospeech","html_url":"https://github.com/neurospeech","followers_url":"https://api.github.com/users/neurospeech/followers","following_url":"https://api.github.com/users/neurospeech/following{/other_user}","gists_url":"https://api.github.com/users/neurospeech/gists{/gist_id}","starred_url":"https://api.github.com/users/neurospeech/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neurospeech/subscriptions","organizations_url":"https://api.github.com/users/neurospeech/orgs","repos_url":"https://api.github.com/users/neurospeech/repos","events_url":"https://api.github.com/users/neurospeech/events{/privacy}","received_events_url":"https://api.github.com/users/neurospeech/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-Compilers","name":"Area-Compilers","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-06-28T09:09:23Z","updated_at":"2016-07-02T04:03:27Z","closed_at":null,"body":"**Version Used**: \r\nC# 6.0\r\n**Steps to Reproduce**:\r\n\r\n```\r\n// this is not static class\r\npublic class MyClass{\r\n\r\n     // I guess this is defined before FieldA, so FieldA will be null\r\n     static SomeClassB FieldB = new SomeClassB(FieldA);\r\n\r\n     static SomeClass FieldA = new SomeClass();\r\n\r\n}\r\n```\r\n\r\n**Expected Behavior**:\r\nI should get a warning, as there are many properties/fields, and order of them could not be in clear way, (if they are also defined in partial classes), this often results in null values, hard to debug.\r\n\r\n**Actual Behavior**:\r\nNo warning is given, and FieldA is always null while initializing FieldB. \r\n\r\n\r\n**Solution**\r\n\r\n1. Warning/Error indicating that static property/field cannot be used as member initalizer for other member as order of initialisation is unknown\r\n2. Automatically change order of initalization if possible\r\n\r\nCorrect way to initialize FieldB should be\r\n```\r\n// this is not static class\r\npublic class MyClass{\r\n\r\n     static MyClass(){\r\n         FieldB = new SomeClassB(FieldA);\r\n     }\r\n\r\n     static SomeClassB FieldB;\r\n\r\n     static SomeClass FieldA = new SomeClass();\r\n\r\n}\r\n```\r\n\r\n\r\n\r\n**Benefits**\r\nUsually in case of WPF/Xamarin, static Dependency/Bindable properties require all sort of static field intialization and it is kind of nightmare to debug as it does not result in an known error. I accidentally found my error.\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/230082121","html_url":"https://github.com/dotnet/roslyn/issues/12245#issuecomment-230082121","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/12245","id":230082121,"user":{"login":"gafter","id":3804346,"avatar_url":"https://avatars.githubusercontent.com/u/3804346?v=3","gravatar_id":"","url":"https://api.github.com/users/gafter","html_url":"https://github.com/gafter","followers_url":"https://api.github.com/users/gafter/followers","following_url":"https://api.github.com/users/gafter/following{/other_user}","gists_url":"https://api.github.com/users/gafter/gists{/gist_id}","starred_url":"https://api.github.com/users/gafter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gafter/subscriptions","organizations_url":"https://api.github.com/users/gafter/orgs","repos_url":"https://api.github.com/users/gafter/repos","events_url":"https://api.github.com/users/gafter/events{/privacy}","received_events_url":"https://api.github.com/users/gafter/received_events","type":"User","site_admin":false},"created_at":"2016-07-02T04:03:27Z","updated_at":"2016-07-02T04:03:27Z","body":"Perhaps it would be good if we warn about using a _later-initialized_ static field in the same class?"}}
{"actor":{"display_login":"gafter"}}