nullable var using implicit typing in c#? - c#

Is there anyway to have the var be of a nullable type?
This implicitly types i as an int, but what if I want a nullable int?
var i = 0;
Why not support this:
var? i = 0;

var is typed implicitly by the expression or constant on the right hand side of the assignment. var in and of itself is not a type so Nullable<var> is not possible.

Why support it? If that's what you mean, you should say var i = (int?)0; instead.
(Well, you should probably just say int? i = 0, but for more complicated types, etc.)

The problem deals with nullable types.
For instance, you cannot create a nullable string, which in turn prevents you from creating a nullable var, since var could be a string.

My answer is kind of along these lines. "var" is implicitly typed. It figures out what type it is by the value supplied on the right-hand side of the assignment. If you tell it that it's nullable it has no idea what type to be. Remember, once it's assigned, that's the type it's going to be forever.

Try this - is this what you're talking about?
static void Main(string[] args)
{
for (int i=0; i < 10; i++)
{
var j = testMethod();
if (j == null)
{
System.Diagnostics.Debug.WriteLine("j is null");
}
else
{
System.Diagnostics.Debug.WriteLine(j.GetType().ToString());
}
}
}
static int? testMethod()
{
int rem;
Math.DivRem(Convert.ToInt32(DateTime.Now.Millisecond), 2, out rem);
if (rem > 0)
{
return rem;
}
else
{
return null;
}
}

VAR is an implicit type determined by the compiler at the time of compilation. It is assigned upon first use. IE when you set var I = 1, var is an int because that is what the number 1 is. If you instead have var I = SomeFunction() where some function returns a nullable int than I will be set as int?. Now with that said var should not be used in a case where you want to control what the variable type is.
So bottom line, as per your example using var is wrong and should be explicitly set to int? from the start.
Darroll

I wouldn't even use var in this case. If it's a complex type or a simple one such as an int I would use type "object".
Example:
//Define all global class variables
List<int> m_lInts = New List<int>();
List<string> m_lStrs = New List<string>();
public static object FindMyObject(string sSearchCritera)
{
//Initialize the variable
object result = null;
if (result == null)
{
//Search the ints list
foreach(int i in m_lInts)
{
if (i.ToString() == sSearchCritera)
{
//Give it a value
result = (int)i;
}
}
}
if (result == null)
{
//Search the ints list
foreach(string s in m_lStrs)
{
if (s == sSearchCritera)
{
//Give it a value
result = (string)s;
}
}
}
//Return the results
return result;
}

Related

How to store multiple datatype values into single list without defining class?

Expected result :
List<something> obj = new List<something>(); //something is i need.
string[] val = new string[] {"hi","20"}; //here I used static but input data fetch from CSV file. So we don't know when which type of data will come?.
int intval;
for(int i=0;i<val.Length;i++)
{
if(int.TryParse(val[i],out intval)
{
obj.add(intval); //here I face the error "Cannot implicitly convert int to string"
}
else
{
obj.add(val[i]);
}
}
I need to add int value and also string value into the same list. But The condition is Developer don't know when which type of value will come. So here I used TryParse to convert values and store into list.
How to declare a list or any other methods are there?
Note: Don't use Class to declare field and define like List<classname> val = new List<classname>();
Not sure why you want to do this but I think the code below is what you're looking for. I suppose you can later check if each list value typeof is string/int but why store multi value types in one list like this?
string[] val = new string[] { "hi", "20" };
List<object> objs = val.Select(x =>
{
if (int.TryParse(x, out var intval))
return (object)intval;
else
return (object)x;
}).ToList();
If you just need to store all the results that you get to a single list without caring about the type then dont Parse to int type like
for (int i = 0; i < val.Length; i++)
{
//simply add the
obj.add(val[i]);
}
Just use a List<object>. Every type ultimately derives from object:
List<object> obj = new List<object>();
string[] val = new string[] {"hi","20"};
for(int i = 0; i < val.Length; i++)
{
if(int.TryParse(val[i], out var intval)
{
obj.add(intval);
}
else
{
obj.add(val[i]);
}
}

Convert Short? to System.Collection.Generic.List

I have a variable called filter being passed into the SaleList from the view. When it first loads its always NULL which is should be. I seem to be having an issue with the else statement k = filter. I have tried a couple things which no such luck. Any help would be appreciated .
Cannot implicitly convert type short?[]' to System.Collection.Generic.List
I am not sure how convert this
short? filter = sale.SalesStatusID;
List<short> k;
if (filter == null)
{
k = (from s in _db.SalesStatus
select s.SaleStatusID).ToList();
}
else
{
k = filter;
}
var sales = from t in _db.Sales
where k.Contains((short) t.SalesStatusID)
select t;
The variable k is a collection of short and your filter variable is a nullable short. You cannot assign a nullable short(singlet item) to a collection type.
You can create a collection using your filter variable value and assign it.
List<short> k;
if (filter != null)
{
k = new List<short> { filter.Value };
}
else
{
k = new List<short>();
}
Or even better, initialize the empty list and add to it.
List<short> k = new List<short>();
if (filter != null)
{
k.Add(filter.Value);
}
But, Keep in mind that (short) t.SalesStatusID will give you unexpected results. What if you SalesStatusID value is a number which is bigger than short.MaxValue(32767) ? So i suggest use the same type and avoiding the casting.
List<int> k = new List<int>(); //Assuming SalesStatusID's type is int32
if (filter != null)
{
k.Add(filter.Value);
}
else
{
k = _db.SalesStatus.Select(a=>a.SaleStatusID).ToList();
}
// use k now
You're assigning k (which is a List type) to value of filter (which is nullable short type) You need to change your else statement to this
else
{
k.Add(filter.Value);
}

.Net dynamic list conversion

I have to do a comparison of 2 lists. The problem is that I don't know of what type the field inside the list are, they can be int, string, decimal, enums or even other objects.
I will know the type only on runtime. I was thinking of creating a list of object and cast them to object the problem is that let's say I have a List<int> and I'm trying to cast it to object it fails.
Another problem is that I know there is a list only on runtime. so on runtime I need to transform the variable of type object to a list.
How can I cast that object to List and how can I cast it to let's say list of objects?
Update:
I have and object and by reflection I'm getting the the property of it with
var oldProperty = property.GetValue(old);
var newProperty = property.GetValue(new);
Once I have the properties values and I can see it's a list I will need to compare those 2. Let's say oldProperty is of type List
I've tried to do something like:
var myOldList = (List<object>)oldProperty;
If the cast fails with
Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'System.Collections.Generic.List`1[System.Object]'
Here you have a look of the function i;m trying to create. Please don't mind of null objects(is not in the scope)
public void SetDifference(object first, object second)
{
var properties = first.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
var oldValue = property.GetValue(first);
var newValue = property.GetValue(second);
if (Convert.GetTypeCode(newValue) != TypeCode.Object)
{
if (!oldValue.Equals(newValue))
{
result.AddDifference(new PrimitiveComparison()
{
BeforeValue = oldValue.ToString(),
AfterValue = newValue.ToString(),
PropertyName = property.Name
});
}
}
else
{
if (property.PropertyType.Name.Contains("List"))
{
// here fails with the error from above
var oldList = (List<object>)oldValue;
var newList = (List<object>)newValue;
if (oldList.Count != newList.Count)
{
result.AddDifference(new PrimitiveComparison()
{
BeforeValue = oldList.Count.ToString(),
AfterValue = newList.Count.ToString(),
PropertyName = property.Name + "Count"
});
}
// add the list differences
result.AddDifference(SetListDifference(oldList, newList);
}
else
{
var diffrence = SetDifference(oldValue, newValue);
if (!diffrence.areEqual)
{
result.AddDifference(diffrence);
}
}
}
}
}
You can just cast your two values to IList and compare them, for example like this:
static bool AreEqual(IList first, IList second) {
if (first.Count != second.Count)
return false;
for (int i = 0; i < first.Count; i++) {
if (!object.Equals(first[i], second[i]))
return false;
}
return true;
}
once you do conversion of you list than you can check both the list have same element or not by using except method of linq, to find both are equal or not
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);
if(onlyInFirstSet.Count() ==0)
Console.WriteLine("equal");
for primitive type this works fine but for user defined type you need compare implementation.
Check this blog post for comparing list of two different type : Difference between list of user defined types
If you are aware that is going to be IEnumerable type than you can try
List<object> objlst = (value as IEnumerable<object>).Cast<object>
().ToList()
you can try like this
Type t = typeof(obj);
if (t == typeof(List<int>)) {
var lst= (List<int>)obj;
} else if (t == typeof(List<string>)) {
var lst = (List<string>)obj;
} else if (t == typeof(List<decimal>)) {
var lst = (List<decimal>)obj;
}
else if (t == typeof(List<EnumName>)) {
var lst = (List<EnumName>)obj;
}

How do I pass in an Enum as a parameter such that it can be used to perform a cast?

In the stub below how do I pass in (MyEnum) as a parameter such that I can use this procedure with any enum?
public static Enum Proc(this Enum e)
{
Int32 i = (Int32)(MyEnum)e;
...
Here is the solution I have come up with that works:
public static Enum Next(this Enum e, Type eT)
{
Int32 i = (Int32)(Object)e;
return (Enum)Enum.Parse(eT, Enum.GetName(eT, Enum.GetName(eT, ++i) == null? i = 0 : i));
}
This solution isn't ideal because I have to do this to get the next value:
MyEnum e = (MyEnum)e.Next(typeof(MyEnum));
I'd rather just do
MyEnum e = e.Next(typeof(MyEnum));
Or even better:
MyEnum e = e.Next();
Anyone who can provide the simple solution can have the point.
Also the code I've written above runs fine in LinqPad but only compiles in WP7 and then throws an exception when I run it (InvalidProgramException).
Here's a function that will cycle through the values of any enum:
static public Enum Cycle(this Enum e)
{
bool found = false;
Enum first = null;
foreach (Enum i in Enum.GetValues(e.GetType()))
{
if (first == null)
first = i;
if (found)
return i;
found = e.Equals(i);
}
if (found)
return first;
return null;
}
For versions of C# that don't have Enum.GetValues, you'll have to use a method like this which only works for enums whose values start at 0 and increment by 1:
static public Enum Cycle(this Enum e)
{
var i = ((IConvertible)e).ToInt64(null) + 1;
var eT = e.GetType();
var next = Enum.GetName(eT, i);
return (Enum)Enum.Parse(eT, next ?? Enum.GetName(eT, 0), false);
}
Use it like:
var nextEnum = (MyEnum)curEnum.Cycle();
Edit: I updated this to return the next enum in the list, strongly typed, regardless of what the numbering of the enum values may be. I'm able to compile and run this under .NET 4 and haven't tried it on WP7, but I don't think I'm using anything that's missing/disabled in SL/WP7.
public static T Next<T>(this T e) where T : struct
{
var t = typeof(T);
if (!t.IsEnum) throw new ArgumentException("T must be an enumerated type");
if (!Enum.IsDefined(t, e)) throw new ArgumentException();
var intValue = (int)t.GetField(e.ToString()).GetValue(null);
var enumValues = t.GetFields(BindingFlags.Public | BindingFlags.Static).Select(x => x.GetValue(null));
var next = (T?)enumValues.Where(x => (int)x > intValue).Min();
if (next.HasValue)
return next.Value;
else
return (T)enumValues.Min();
}
It can be used as simply as:
var nextE = e.Next();

Adding data to a generic string Array C#

I'm learning about generics in C#, and I'm trying to make a generic array and add some strings to it. I can easily add int values to it, but I can't figure out how to make it work with strings. When I'm trying to use strings I get NullReferenceException.
I have a class called myArray, and it looks like this:
class MyArray<T> : IComparable<T>, IEnumerable<T>, IEnumerator<T>
{
T[] data = new T[10];
int current = -1;
public T[] Data
{
get { return data; }
}
public void Add(T value)
{
for (int i = 0; i < data.Length; i++)
{
if (data[i].Equals(default(T)))
{
data[i] = value;
return;
}
}
T[] tmp = new T[data.Length + 10];
data.CopyTo(tmp, 0);
Add(value);
}
In my mainform I add the data like this:
class Program
{
static void Main(string[] args)
{
MyArray<string> StringArray = new MyArray<string>();
StringArray.Add("ONE");
StringArray.Add("TWO");
}
}
The default of string is null as it is a reference type, not a value type. You are constructing a new array of type T[], which results in an array filled with the default value of T, which in this case is null.
Following that, data[i].Equals(default(T)) throws NRE as you are trying to call null.Equals(...).
Your array is being initialized with null values, so you're getting a NRE at this line:
if (data[i].Equals(default(T)))
if (data[i].Equals(default(T))) is where the issue lies. In a new string (or any other reference type) array,
var array = new String[10];
each element in the array is null by default. So when you say
data[i].Equals(default(T)
and data[i] is null, you're calling a method on a null reference, thus causing the exception to be thrown.
This doesn't happen for value types, as the array is initialized to the default value of whatever the value type is.
This is part of the problem with generics--you can't always treat reference types and value types the same.
Try this instead, to avoid default:
private int _currentIndex = 0;
public void Add(T value)
{
data[_currentIndex] = value;
_currentIndex++;
if(_currentIndex == data.Length)
{
T[] tmp = new T[data.Length + 10];
data.CopyTo(tmp, 0);
data = tmp;
_currentIndex = 0;
}
}

Categories