Make sure C # variable initialization

Consider this code:

public string Variable1 { get; set;} public int Variable2 { get; set;} public void Function() { // Has been Variable1 Initialized? } 

Inside the function, I want to know if the value Variable1 and Variable2 were sent before the function call

, even if DEFAULT values ​​were sent, this is normal (null for the string and 0 for int)

+7
variables c # instance-variables
source share
4 answers

Consider using a simple wrapper:

 public struct AssignableProperty<T> { private T _value; public T Value { get { return _value; } set { WasAssigned = true; _value = value; } } public bool WasAssigned { get; private set; } public static implicit operator AssignableProperty<T>(T data) { return new AssignableProperty<T>() { Value = data }; } public static bool operator ==(AssignableProperty<T> initial, T data) { return initial.Value.Equals(data); } public static bool operator !=(AssignableProperty<T> initial, T data) { return !initial.Value.Equals(data); } public override string ToString() { return Value.ToString(); } } 

Then your class will look like this:

 public class Test { public AssignableProperty<string> Variable1 { get; set; } public AssignableProperty<int> Variable2 { get; set; } public void Function() { if(Variable1.WasAssigned&&Variable2.WasAssigned) //do stuff } } 

You can go ahead and add a throw Exception or getter contract, so if someone tries to access an uninitialized value, he will throw an exception or show you a warning

+8
source share

Some basics for the default in C #:

When an instance of a class (or structure) is created, all fields are initialized with the appropriate default value.

For reference types, this will be null . For value types, it will be equivalent to 0 . This easily explains, since memory management ensures that the new allocated memory is initialized to 0x0 bytes.

Auto properties hide the generated field, but there is one. Therefore, the same rules apply.

Now, to answer your question , the best way to make sure the values ​​are initialized is to make a constructor with one parameter for each property / property and hide the default constructor without parameters:

 public Yourtype(String param1, Int32 param2) { this.Variable1 = param1; this.Variable2 = param2; } private Yourtype() { } 

Other alternatives are described in the @Sean and @Alex answers if you need to initialize / check only a subset of properties / fields. But this hides some overhead (one bool for each property / field and some indirectness).

+2
source share

For reference types you need to add a flag:

 string m_Variable1; bool m_IsVariable1Set; public string Variable1 { get{return m_Variable1;} set{m_IsVariable1Set = true; m_Variable1 = value;} } 

For value types, you can use a value with a null value

 int? m_Variable2; int Variable2 { get{return m_Variable2.GetValueOrDefault();} set{m_Variable2 = value;} } 

What you can check to determine if it was installed using m_Variable2.HasValue .

0
source share

Well, you can just do a check on both variables to see if they have any value assigned to them in your function.

 public void Function() { if (String.IsNullOrEmpty(Variable1) && Variable2 ==0 ) { // Variables are not assigned } } 
-2
source share

All Articles