Why does Resharper tell me there is a possible NullReferenceException? - c#

I switched the Value Analysis Mode in Rider to Pessimistic to highlight every "Possible NullReferenceException" and in the example below, I have a warning on the "Languages[0]" part and I don't understand why since I initialize my collection right after declaring it.
It should not be null then.
I just tested on an empty project and I have the same warning.
using System.Collections.Generic;
namespace ClassLibrary1
{
public class Class1
{
public static string Current => Languages[0];
public static readonly List<string> Languages = new List<string>
{
"en"
};
}
}
Is it a mistake made by ReSharper or did I missed something ?
Thanks.

I think static properties are evaluated in order of appearance. Try to flip them in order to have first "Languages" and then "Current".

Try this:
public static class Program
{
public static readonly string Foo = Current;
public static string Current => Languages[0];
public static readonly List<string> Languages = new List<string>
{
"en"
};
public static void Main()
{
Console.WriteLine(Foo);
}
}
SharpLab
Static members are initialized in the order they're declared. Foo is initialized before Languages is assigned, and so you see a NullReferenceException.
I guess that Resharper's being very pessimistic here, and only considering Current in isolation, regardless of whether there's actually another static member that might access it before Languages is initialised.
You could also have something diabolical like this, where the construction of Languages causes something to access Program.Current:
public static class Program
{
public static string Current => Languages[0].Value;
public static readonly List<Language> Languages = new List<Language>() { new Language() };
public static void Main()
{
Console.WriteLine(Current);
}
}
public class Language
{
public string Value { get; } = Program.Current;
}
(It's a silly example, but it shows that it's perhaps harder for Resharper to prove that nothing accesses Program.Current before Program's type initializer has finished running, than you might expect).

Related

Static List Showing as Null [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a static class called Constants.
public static class Constants
{
public static string sampleString= "";
public static List<int> sampleList= new List<int> {1,2,3};
}
If I call my static list outside:
Constants.sampleList it gives me a null exception but Constants.sampleString can be called with no problem.
Am I missing something here?
Not sure if this is the same case as ours but we have a class that access the key from the config
string sampleString = WebConfigurationManager.AppSettings["SampleString"]
Sample string was deleted from our web.config due to some merging issues. Error for null pointer happens when you access any of the variables in the class bellow the variable.
Adding the key in the config fixed the issue.
When I run this code:
void Main()
{
Console.WriteLine(Constants.sampleList.Contains(1));
}
public static class Constants
{
public static string sampleString= "";
public static List<int> sampleList= new List<int> {1,2,3};
}
I get True on the Console. You need to provide the code which demonstrates the issue you're facing.
If you add readonly keyword to your method. The method can only be instantiated inside constructor or at the property declaration phase.
public static readonly List<int> sampleList= new List<int> {1,2,3};
If you ever try to reinitialize or make new assignment to sampleList, C# compiler will give you compiler error.
Yet better to use Property
public static readonly List<int> SampleList {get; set;} = new List<int> {1,2,3};
There are no miracles, and if sampleList is null (and you have exception thrown) then you asign null to it somewhere.
Try not to expose public fields which makes Constants vulnerable:
public static class Constants
{
public static string sampleString = "";
public static List<int> sampleList = new List<int> {1,2,3};
}
...
Constants.sampleList = null; // We can easly assign null
...
Constants.sampleList.Add(123); // <- And have an unxpected exception
But either turn them into properties:
public static class Constants
{
private static string s_SampleString = "";
private static List<int> s_SampleList = new List<int> {1,2,3};
public static string sampleString {
get {return s_SampleString;}
set {s_SampleString = value ?? "";}
}
public static List<int> sampleList {
get {return s_SampleList;}
// I doubt you actually want set here
set {s_SampleList = value ?? new List<int>();}
}
}
Or, at least, mark the field(s) as readonly (you can assign them once only):
public static class Constants
{
private static string s_SampleString = "";
public static string sampleString {
get {return s_SampleString;}
set {s_SampleString = value ?? "";}
}
// Manipulate with me, but not reassign
public static readonly List<int> sampleList = new List<int> {1,2,3};
}
In either case you still can manipulate with the list:
Constants.sampleList.Add(4);
Constants.sampleList.RemoveAt(0);
But you are protected from assigning null to the list: either empty list will be assigned (code with property) or you'll have compile time error (code with readonly)
In my case, I was defining a private static readonly Dictionary<byte, string> inside a struct to hold predefined constants. This normally would work fine, however I also had defined a MinValue within my struct to represent the minimum value of a piece of data. When done this way the static dictionary was uninitialized unless defined above the static MinValue property. I'm probably asking a little too much of the compiler and should restructure it instead.
It was hard to diagnose in a large struct as I didn't expect that behavior with C#. Example to reproduce:
public struct MyStruct
{
public string Str;
public static readonly MyStruct MinValue = new MyStruct(0);
public MyStruct(byte val)
{
Str = _predefinedValues[val]; // null reference exception
}
private static readonly Dictionary<byte, string> _predefinedValues = new Dictionary<byte, string>()
{
{0x00, "test 1"},
{0x01, "test 2"},
{0x02, "test 3"},
};
}
The solution here is twofold:
to not call any constructors on the struct and explicitly set each field without accessing _predefinedValues list.
Alternatively, moving the list up above the MinValue field declaration actually does fix it as well (strange right?)
I suspect something weird happens on the stack when trying to allocate this and it's probably a weird thing to do.

Partial implemention through code of C#6's nameof functionality in earlier versions of C#?

"nameof" is an amazing idea which would be good to replicate in previous versions even if partially.
I am particularly interested in public property names like:
public class MyClass
{
public SomeType Myproperty {get;set;}
}
static Main()
{
MyClass myClass = new MyClass();
Console.WriteLine(Utilities.NameOf(myClass.MyProperty)); //Writes "MyProperty".
Console.ReadKey();
}
Is there a way to do this (maybe through reflection etc.)? If we could do this, it would also prepare our code for when we upgrade to C#6 in the future by simply replacing Utilities.NameOf with nameof.
Thanks to Alexei Levenkov for pointing to the right direction,
Schotime's for his answer in this question and
agent-j's for his answer in this question
there is a simple and elegant solution that has two versions:
public static class Util
{
public static string NameOf<TProperty>(Expression<Func<TProperty>> e)
{
return (e.Body as MemberExpression).Member.Name;
}
public static string NameOf<TClass, TProperty>(Expression<Func<TClass, TProperty>> e)
{
return (e.Body as MemberExpression).Member.Name;
}
}
and can be used so:
public class MyClass
{
public SomeProperty MyProperty { get; set; }
}
static void Main(string[] args)
{
MyClass myClass = new MyClass();
string case1 = Util.NameOf(() => myClass.MyProperty); //Case1 when an instance is available 250x
string case2 = Util.NameOf(() => (null as MyClass).MyProperty); //Case2 when no instance is available 175x
string case3 = Util.NameOf((MyClass c) => c.MyProperty); //Case3 when no instance is available 330x
string caseTest = "MyProperty"; //Test case 1x
}
At the end of each case, the number shows how much slower each call is relative to a literal string assignment.
This was done by assigning the return values of each call to an array of strings and comparing that to the literal string assignment of the same value "MyProperty".
Case2 seems to be the winner.

C# Get property value without creating instance?

Is it possible to get value without creating an instance ?
I have this class:
public class MyClass
{
public string Name{ get{ return "David"; } }
public MyClass()
{
}
}
Now I need get the value "David", without creating instance of MyClass.
Real answer: no. It's an instance property, so you can only call it on an instance. You should either create an instance, or make the property static as shown in other answers.
See MSDN for more information about the difference between static and instance members.
Tongue-in-cheek but still correct answer:
Is it possible to get value without creating an instance ?
Yes, but only via some really horrible code which creates some IL passing in null as this (which you don't use in your property), using a DynamicMethod. Sample code:
// Jon Skeet explicitly disclaims any association with this horrible code.
// THIS CODE IS FOR FUN ONLY. USING IT WILL INCUR WAILING AND GNASHING OF TEETH.
using System;
using System.Reflection.Emit;
public class MyClass
{
public string Name { get{ return "David"; } }
}
class Test
{
static void Main()
{
var method = typeof(MyClass).GetProperty("Name").GetGetMethod();
var dynamicMethod = new DynamicMethod("Ugly", typeof(string),
Type.EmptyTypes);
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Ldnull);
generator.Emit(OpCodes.Call, method);
generator.Emit(OpCodes.Ret);
var ugly = (Func<string>) dynamicMethod.CreateDelegate(
typeof(Func<string>));
Console.WriteLine(ugly());
}
}
Please don't do this. Ever. It's ghastly. It should be trampled on, cut up into little bits, set on fire, then cut up again. Fun though, isn't it? ;)
This works because it's using call instead of callvirt. Normally the C# compiler would use a callvirt call even if it's not calling a virtual member because that gets null reference checking "for free" (as far as the IL stream is concerned). A non-virtual call like this doesn't check for nullity first, it just invokes the member. If you checked this within the property call, you'd find it's null.
EDIT: As noted by Chris Sinclair, you can do it more simply using an open delegate instance:
var method = typeof(MyClass).GetProperty("Name").GetGetMethod();
var openDelegate = (Func<MyClass, string>) Delegate.CreateDelegate
(typeof(Func<MyClass, string>), method);
Console.WriteLine(openDelegate(null));
(But again, please don't!)
You can make that property static
public static string Name{ get{ return "David"; } }
Usage:
MyClass.Name;
You requirements do seem strange, but I think you're looking for some kind of metadata. You can use an attribute to achieve this:
public class NameAttribute : Attribute {
public string Name { get; private set; }
public NameAttribute(string name) {
Name = name;
}
}
[Name("George")]
public class Dad {
public string Name {
get {
return NameGetter.For(this.GetType());
}
}
}
[Name("Frank")]
public class Son : Dad {
}
public static class NameGetter {
public static string For<T>() {
return For(typeof(T));
}
public static string For(Type type) {
// add error checking ...
return ((NameAttribute)type.GetCustomAttributes(typeof(NameAttribute), false)[0]).Name;
}
}
Now this code can get names with and without instances:
Console.WriteLine(new Dad().Name);
Console.WriteLine(new Son().Name);
Console.WriteLine(NameGetter.For<Dad>());
Console.WriteLine(NameGetter.For<Son>());
You can make your property static, as pointed out by many others.
public static string Name{ get{ return "David"; } }
Be aware that this means your instances of MyClass will no longer have their own Name property, since static members belong to the class, not the individual object instances of it.
Edit:
In a note, you mentioned that you want to override the Name property in subclasses. At the same time, you want to be able to access it at the class level (access it without creating an instance of your class).
For the static properties, you would simply create a new Name property in each class. Since they are static, you're always (almost always, yay reflection) going to access them using a specific class, so you'd be specifying which version of Name you want to get. If you want to try and hack polymorphism in there and get the name from any given subclass of MyClass, you could do so using reflection, but I wouldn't recommend doing so.
Using the example from your comment:
public class Dad
{
public static string Name { get { return "George"; }
}
public class Son : Dad
{
public static string Name { get{ return "Frank"; }
}
public static void Test()
{
Console.WriteLine(Dad.Name); // prints "George"
Console.WriteLine(Son.Name); // prints "Frank"
Dad actuallyASon = new Son();
PropertyInfo nameProp = actuallyASon.GetType().GetProperty("Name");
Console.WriteLine(nameProp.GetValue(actuallyASon, null)); // prints "Frank"
}
As a side note, since you are declaring a property that has only a getter and it is returning a constant value, I recommend possibly using a const or static readonly variable instead.
public const string Name = "David";
public static readonly string Name = "David";
Usage for both would be the same:
string name = MyClass.Name;
The main benefit (and drawback) of const is that all references to it are actually replaced by its value when the code is compiled. That means it will be a little faster, but if you ever change its value, you will need to recompile ALL code that references it.
Whenever you write C# code, always check if your method and property getter/setter code does anything at all with other instance members of the class. If they don't, be sure to apply the static keyword. Certainly the case here, it trivially solves your problem.
The reason I really post to this question is that there's a bit of language bias at work in some of the answers. The C# rule that you can't call an instance method on a null object is a specific C# language rule. It is without a doubt a very wise one, it really helps to troubleshoot NullReferenceExceptions, they are raised at the call site instead of somewhere inside of a method where it gets very hard to diagnose that the this reference is null.
But this is certainly not a requirement to the CLR, nor of every language that run on the CLR. In fact, even C# doesn't enforce it consistently, you can readily bypass it in an extension method:
public static class Extensions {
public static bool IsNullOrEmpty(this string obj) {
return obj != null && obj.Length > 0;
}
}
...
string s = null;
bool empty = s.IsNullOrEmpty(); // Fine
And using your property from a language that doesn't have the same rule works fine as well. Like C++/CLI:
#include "stdafx.h"
using namespace System;
using namespace ClassLibrary1; // Add reference
int main(array<System::String ^> ^args)
{
MyClass^ obj = nullptr;
String^ name = obj->Name; // Fine
Console::WriteLine(name);
return 0;
}
Create a static property:
public class MyClass
{
public static string Name { get { return "David"; } }
public MyClass()
{
}
}
Get it like so:
string name1 = MyClass.Name;
That is not possible. As Name is an instance property, you can only get its value if you have an instance.
Also, note that you are not talking about a parameter, but about a property.
Create a static class or a static property, and you don't have to explicitly instantiate it.

C# - How to access a static class variable given only the class type?

This is my first time posting on Stack Overflow, so hopefully I did everything right and you guys can help.
I'm wondering if in C# there's a way to access a static variable belonging to a class, when given only the type of the class. For example:
public class Foo
{
public static int bar = 0;
}
public class Main
{
public void myFunc(Type givenType)
{
int tempInt = ??? // Get the value of the variable "bar" from "Foo"
Debug.WriteLine("Bar is currently :" + tempInt);
}
}
// I didn't run this code through a compiler, but its simple enough
// that hopefully you should get the idea...
It's hard to describe the context of needing to know this, but I'm making a game in XNA and I'm trying to use reference counting to reduce the complexity of the design. I have objects in the game and power-ups that can apply an effect them (that stays on the objects). Power-ups can die but their effects can still linger on the objects, and I need to keep track of if any effects from a power-up are still lingering on objects (thus, reference counting). I plan to make a "PowerUpEffect" class (for each type of power-up) with a static integer saving the number of objects still affected by it, but the design of the rest of the game doesn't work well with passing the PowerUpEffect all the way down to the object for it to call a method of the PowerUpEffect class.
I'm hoping to pass only the PowerUpEffect's type (using something like "typeOf()") and use that type to reference static variables belonging to those types, but I have no idea how to do it or if it's even possible.
I'd be glad to even find work-arounds that don't answer this questions directly but solve the problem in a simple and elegant design. =)
Help! (and thanks!)
If you only have the Type handle, you can do this:
var prop = givenType.GetProperty("bar");
var value = prop.GetValue(null);
I would use a Dictionary instead, which are probably the most concise way of mapping one set of values to another. If you are associating int values with Types, then do something like:
public static readonly Dictionary<Type, int> sTypeValues =
new Dictionary<Type, int>
{
{ typeof(Type1), 5 },
{ typeof(Type2), 10 },
{ typeof(Type3), 2 },
{ typeof(Type4), 3 },
{ typeof(Type5), -7 }
};
your function then becomes:
public void myFunc(Type givenType)
{
int tempInt = sTypeValues[givenType];
Debug.WriteLine("Bar is currently :" + tempInt);
}
int tempInt = (int) givenType.GetField("bar").GetValue(null);
Okay, so you have a collection of powerups, and you want to have an integer associated with each of those powerups. Rather than having a lot of classes, each with a static integer, you can have a single static collection which holds onto all of the powerups and their associated integer values.
public static class MyPowerupInfo
{
public static Dictionary<PowerUp, int> PowerUps {get; private set;}
static MyPowerupInfo
{
PowerUps = new Dictionary<PowerUp, int>();
PowerUps.Add(*some power up object goes here*, 0);
//TODO add other power ups
}
}
Then to use it you can do something like:
int powerupCount = MyPowerupInfo.PowerUps[wickedAwesomePowerup];
or:
public static void IncrementPowerup(Powerup powerup)
{
MyPowerupInfo.PowerUps[powerup] = MyPowerupInfo.PowerUps[powerup]+1;
}
If am getting you correc, this might give you some idea:
using System;
using System.Reflection;
public class RStatic
{
private static int SomeNumber {get; set;}
public static object SomeReference {get; set;}
static RStatic()
{
SomeReference = new object();
Console.WriteLine(SomeReference.GetHashCode());
}
}
public class Program
{
public static void Main()
{
var rs = new RStatic();
var pi = rs.GetType().GetProperty("SomeReference", BindingFlags.Static | BindingFlags.Public); // i have used GetProperty in my case
Console.WriteLine(pi.GetValue(rs, null).GetHashCode());
}
}
Are you assuming if the name of the field you're trying to access (for example, for the class "foo", the field "bar") is a different field based on the Type parameter?
If the name of the field is known based on a finite number of allowable types, you should be able to determine it with a switch statement. For example:
public class Foo
{
public static int bar = 0;
}
public class Baz
{
public static int bing = 0;
}
public class Main
{
public void myFunc(Type givenType)
{
switch (givenType.ToString())
{
case "Foo":
Debug.WriteLine("Bar is currently :" + Foo.bar);
break;
case "Baz":
Debug.WriteLine("Bing is currently :" + Baz.bing);
break;
}
}
}

Define "dynamic" enum

I'm working on this project and I have a large number of "services" to execute. I need to have this service codes so I can verifiy and make decisions according to selected services by it's code.
I'm tring to define this enum and get its values from the web.config so I can change them easily.
public enum ServiceCodes
{
Transfer= Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"]),
ChangePlate= Convert.ToInt32(ConfigurationManager.AppSettings["servChangePlate"])
}
But I get this error: The expression must be constant. (because of the Conversion)
What can I do? (Or you can suggest me totally different approaches.)
Just go ahead and define a static class like this:
public static class ServiceCodes
{
readonly static int Transfer = Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"])
//...
}
The documentation states that enum values are constant. An alternative approach is to declare a class with static readonly members.
If you still need the type safety provided by an enum, you could use a slightly complex approach:
public class ServiceCodes {
public static readonly ServiceCodes Transfer = new ServiceCodes(Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"]));
public static readonly ServiceCodes ChangePlate = new ServiceCodes(Convert.ToInt32(ConfigurationManager.AppSettings["servChangePlate"]));
internal int Code {get; private set;}
private ServiceCodes(int code) {
Code = code;
}
}
Then, a method like:
public void SomeAction(ServiceCodes serviceCode) {
//....
}
could be called like this:
SomeAction(ServiceCodes.Transfer);
But, given the complexity (compared with the gain), I would go with the first approach.

Categories

Resources