I came across this code:
// A
private readonly int value;
public int RawValue => value;
// B
public int CompareTo(Foo other) => value.CompareTo(other.value);
// C
internal int x => unchecked((value & y) + 1);
Could someone please explain what happened here and what are common ways for using those techniques?
EDIT
Link to the code: click
It's a C# 6 syntax of declaring properties (read-only ones) and methods using expression-bodied members (looks similar to lambda expressions).
public int RawValue => value;
is equivalent to
public int RawValue
{
get { return value; }
}
and
public int CompareTo(Foo other) => value.CompareTo(other.value);
is equivalent to
public int CompareTo(Foo other)
{
return value.CompareTo(other.value);
}
Related
I have the below property that calculates the sum of a list of integers.
public int OpponentTotalSetPoints => TeamPoints.Skip(1).Sum(t => t);
My questions is does this property get recaculated every time its called? If so would it be beneficial for the below code to store the value so the summing doesn't happen every time its called?
private int? opponentTotalSetPoints;
public int OpponentTotalSetPoints
{
get
{
if (opponentTotalSetPoints.HasValue)
return opponentTotalSetPoints.Value;
opponentTotalSetPoints = TeamPoints.Skip(1).Sum(t => t);
return opponentTotalSetPoints.Value;
}
}
I would be using something like this
private int? opponentTotalSetPoints;
public int OpponentTotalSetPoints
{
get
{
if (opponentTotalSetPoints==null)
if (TeamPoints.Count >1)
opponentTotalSetPoints = TeamPoints.Skip(1).Sum(t => t);
return opponentTotalSetPoints;
}
}
I have a problem using a self made IEqualityComparer and GetHashCode in a concurrent dictionary.
The class below (simplified with used two properties) works perfect when I implement it like this:
ConcurrentDictionary<TwoUintsKeyInfo,Int64> hashCodePlusIandJDict = new ConcurrentDictionary<TwoUintsKeyInfo, Int64>();
.
public class TwoUintsKeyInfo
{
public uint IdOne { get; set; }
public uint IdTwo { get; set; }
#region Implemetation of the IEqualityComparer
public class EqualityComparerTwoUintsKeyInfo : IEqualityComparer<TwoUintsKeyInfo>
{
System.Reflection.PropertyInfo[] properties;
bool propertyArraySet=false;
public int GetHashCode(TwoUintsKeyInfo obj)
{
unchecked
{
if(!propertyArraySet)
{
properties = obj.GetType().GetProperties().OrderBy(x => x.Name).ToArray();
propertyArraySet = true;
}
decimal hash = 17;
int counter=0;
foreach(System.Reflection.PropertyInfo p in properties)
{
counter++;
var value = p.GetValue(obj);
decimal unique = (decimal)Math.Pow(Math.E, counter);
hash = hash + (value == null ? unique : value.GetHashCode() * unique);
}
return 2147483647M * .001M > hash ? (int)(hash * 1000) : (int)hash;
}
}
public bool Equals(TwoUintsKeyInfo x, TwoUintsKeyInfo y)
{
return GetHashCode(x) == GetHashCode(y);
}
}
#endregion Implemetation of the IEqualityComparer
}
Now I made almost the same class, but instead of the normal IEqualityComparer interface, I made a little change, so I could generate long / int64 hascodes (because when the class hold more and more properties, we encountered multiple values with the same hashcode)
So I wanted to reduce the changes of getting the same hascode. Therefore I wanted to use bigger numbers and if possible multiple by 10000 to get some of the decimals in on the action as well.
therefore I created this interface:
public interface IEqualityComparerInt64<in T>
{
bool Equals(T x, T y);
Int64 GetHashCode(T obj);
}
and altered the property class so it looks like this:
public class TwoUintsKeyInfoInt64
{
public uint IdOne { get; set; }
public uint IdTwo { get; set; }
#region Implemetation of the IEqualityComparer
public class EqualityComparerTwoUintsKeyInfoInt64 : IEqualityComparerInt64<TwoUintsKeyInfoInt64>
{
System.Reflection.PropertyInfo[] properties;
bool propertyArraySet=false;
decimal _upperThreshold,_lowerThreshold;
public EqualityComparerTwoUintsKeyInfoInt64()
{
_upperThreshold = long.MaxValue * .0001M;
_lowerThreshold = -long.MaxValue * .0001M;
}
public long GetHashCode(TwoUintsKeyInfoInt64 obj)
{
unchecked
{
if(!propertyArraySet)
{
properties = obj.GetType().GetProperties().OrderBy(x => x.Name).ToArray();
propertyArraySet = true;
}
decimal hash = 17;
int counter=0;
foreach(System.Reflection.PropertyInfo p in properties)
{
counter++;
var value = p.GetValue(obj);
decimal unique = (decimal)Math.Pow(Math.E, counter);
hash = hash + (value == null ? unique : value.GetHashCode() * unique);
}
return _upperThreshold > hash && _lowerThreshold < hash ? (long)(hash * 10000) : (long)hash;
}
}
public bool Equals(TwoUintsKeyInfoInt64 x, TwoUintsKeyInfoInt64 y)
{
return GetHashCode(x) == GetHashCode(y);
}
}
#endregion Implemetation of the IEqualityComparer
}
GetHashCode worked fine. So far no problem.
But...when I try to add a IEqualityComparer to the concurrentdictionary like this:
ConcurrentDictionary<TwoUintsKeyInfoInt64,Int64> hashCodePlusIandJDict = new ConcurrentDictionary<TwoUintsKeyInfoInt64, Int64>(new TwoUintsKeyInfoInt64.EqualityComparerOneUintAndTwoStringKeyInfo());
I get this error:
Error 3 Argument 1: cannot convert from
'HasCodeTestForUniqueResult.TwoUintsKeyInfoInt64.EqualityComparerOneUintAndTwoStringKeyInfo'
to
'System.Collections.Generic.IEqualityComparer' D:\Users\mldz\Documents\visual
studio
2012\HashCodeTestForUniqueResult\HashCodeTestForUniqueResult\Form1.cs 109 140 HashCodeTestForUniqueResult
I understand that there's a conflict between the int type of the default System.Collections.Generic.IEqualityComparer and my long / int64 result from my own GetHashCode generator. But is there any way to solve this and be able to use long HashCodes?
Kind regards,
Matthijs
P.S. the code above is just to test it and replicate the problem.
According to this you cannot use long hash codes, so the answer to the question is no.
But you can have unique combinations instead of unique values; the solution is to implement a partitioning system, meaning have a dictionary of dictionaries, like:
public class MyClass
{
Dictionary<uint, Dictionary<uint, Int64>> PartDict;
Int64 ReadValue(uint id1, uint id2)
{
return (PartDict[id1])[id2];
}
void AddValue(uint id1, uint id2, Int64 value)
{
Dictionary<uint, Int64> container;
if (!PartDict.TryGetValue(id1, out container))
{
container = new Dictionary<uint, Int64>();
PartDict.Add(id1, container);
}
container.Add(id2, value);
}
}
This way you will have a list of hash codes and each hash code will have again a list of hash codes, the combination being unique. Any reading and writing will be done in 2 steps though (to consider in case you want unique hash for performance).
Hope it helps.
I have a huge array that contains reference type elements, and I want to create a lot of other arrays that essentially just point to specific parts of that one big array.
In other words, I want to create "indexers" or "pointers with lengths".
In C++ it's easy to do so using pointers and for each pointer assign a length, for example create a struct which contains a pointer with a length.
How can I achieve this in C#/.NET?
The whole point is to avoid copying anything, I just want pointers to specific parts in an array that already exists in memory.
Any ideas?
Jon's suggestion of using ArraySegment<T> is likely what you want. If however you are wanting to represent a pointer to the interior of an array, the way you can in C++, here's some code for that. No warranty is expressed or implied, use at your own risk.
This code does not track the "length" of the interior pointer in any way, but it is quite easy to add that feature if you want.
internal struct ArrayPtr<T>
{
public static ArrayPtr<T> Null { get { return default(ArrayPtr<T>); } }
private readonly T[] source;
private readonly int index;
private ArrayPtr(ArrayPtr<T> old, int delta)
{
this.source = old.source;
this.index = old.index + delta;
Debug.Assert(index >= 0);
Debug.Assert(index == 0 || this.source != null && index < this.source.Length);
}
public ArrayPtr(T[] source)
{
this.source = source;
index = 0;
}
public bool IsNull()
{
return this.source == null;
}
public static bool operator <(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index < b.index;
}
public static bool operator >(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index > b.index;
}
public static bool operator <=(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index <= b.index;
}
public static bool operator >=(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index >= b.index;
}
public static int operator -(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index - b.index;
}
public static ArrayPtr<T> operator +(ArrayPtr<T> a, int count)
{
return new ArrayPtr<T>(a, +count);
}
public static ArrayPtr<T> operator -(ArrayPtr<T> a, int count)
{
return new ArrayPtr<T>(a, -count);
}
public static ArrayPtr<T> operator ++(ArrayPtr<T> a)
{
return a + 1;
}
public static ArrayPtr<T> operator --(ArrayPtr<T> a)
{
return a - 1;
}
public static implicit operator ArrayPtr<T>(T[] x)
{
return new ArrayPtr<T>(x);
}
public static bool operator ==(ArrayPtr<T> x, ArrayPtr<T> y)
{
return x.source == y.source && x.index == y.index;
}
public static bool operator !=(ArrayPtr<T> x, ArrayPtr<T> y)
{
return !(x == y);
}
public override bool Equals(object x)
{
if (x == null) return this.source == null;
var ptr = x as ArrayPtr<T>?;
if (!ptr.HasValue) return false;
return this == ptr.Value;
}
public override int GetHashCode()
{
unchecked
{
int hash = this.source == null ? 0 : this.source.GetHashCode();
return hash + this.index;
}
}
public T this[int index]
{
get { return source[index + this.index]; }
set { source[index + this.index] = value; }
}
}
Now we can do stuff like:
double[] arr = new double[10];
var p0 = (ArrayPtr<double>)arr;
var p5 = p0 + 5;
p5[0] = 123.4; // sets arr[5] to 123.4
var p7 = p0 + 7;
int diff = p7 - p5; // 2
It sounds like you're looking for something like ArraySegment<T>. Contrary to my earlier thoughts, it does have an indexer and implement IEnumerable<T> etc - it's just done with explicit interfaces.
Sample code:
using System;
using System.Collections.Generic;
static class Test
{
static void Main()
{
string[] original = { "The", "quick", "brown", "fox", "jumped", "over",
"the", "lazy", "dog" };
IList<string> segment = new ArraySegment<string>(original, 3, 4);
Console.WriteLine(segment[2]); // over
foreach (var word in segment)
{
Console.WriteLine(word); // fox jumped over the
}
}
}
EDIT: As noted in comments, ArraySegment<T> is only really "fully functional" in .NET 4.5. The .NET 4 version doesn't implement any interfaces.
You could use LINQ:
yourArray.Skip(startIndex).Take(numberToTake)
The query is lazily evaluated.
I created a struct
public struct MyCalender : IComparable<MyCalender>
{
public int CompareTo(PersianDate other)
{
return DateTime.Compare(this, other);
}
.
.
.
.
.
}
I new two object of this in a other UserControl, and i want compare they.
I use this code but i get error.
MyCalender value = new MyCalender(2010,11,12);
MyCalender value2 = new MyCalender(2010,11,12);
if (value < value2) ==> geterror
IComparable exposes CompareTo. < and > must be overloaded separately:
class Foo : IComparable<Foo>
{
private static readonly Foo Min = new Foo(Int32.MinValue);
private readonly int value;
public Foo(int value)
{
this.value = value;
}
public int CompareTo(Foo other)
{
return this.value.CompareTo((other ?? Min).value);
}
public static bool operator <(Foo a, Foo b)
{
return (a ?? Min).CompareTo(b) < 0;
}
public static bool operator >(Foo a, Foo b)
{
return (a ?? Min).CompareTo(b) > 0;
}
}
I edited the code so that it does not fail when comparing against null. To keep it brief I used a shortcut that works unless value is Int32.MinValue for a proper Foo. Strictly speaking you'd have to check for null explicitly to get the contract right:
By definition, any object compares greater than (or follows) null, and
two null references compare equal to each other.
Besides, implementing IComparable<T> means that CompareTo(T value) takes a parameter of T. Therefore MyCalendar : IComparable<MyCalender> should implement a method CompareTo(MyCalendar other) rather than PersianDate (or implement IComparable<PersianDate>).
You should either use CompareTo method that you already implemented instead of > in the line you posted or you need to overload > and < operators for your specific class. For instance:
public static bool operator >(MyCalendar c1, MyCalendar c2)
{
return c1.CompareTo(c2) > 0;
}
public static bool operator <(MyCalendar c1, MyCalendar c2)
{
return c1.CompareTo(c2) < 0;
}
But keep in mind that you have to overload both of them.
if comparing just a datetime object,
would something like
DateTime A = DateTime.Now, B = DateTime.Now.AddMinutes(1);
var isqual = A.Date.CompareTo(B.Date);
do the trick?
or something like:
class Calender
{
public DateTime datetime { get; set;}
}
class DateComparer : Calender, IComparable<Calender>
{
public int CompareTo(Calender other)
{
return other.datetime.Date.CompareTo(this.datetime.Date);
}
}
Is there something similar to this C++ template?
template <int A>
class B
{
int f()
{
return A;
}
}
I want to make every instance of B<1>, B<2>, etc (eg tuple) a different type.
The short answer is no.
It doesn't fit the way C# generics, as apposed to C++ templates, work.
The .net generics are not a language feature, they are a runtime feature. The runtime knows how to instantiate generics from special generic bytecode which is rather restricted compared to what C++ templates can describe.
Compare this with C++ templates, which basically instantiate the whole AST of the class using substituted types. It'd be possible to add AST based instantiation to the runtime, but it'd certainly be much more complex than the current generics.
Without features like value-type arrays (which only exist in unsafe code), recursive template instantiation or template specialization using such parameters wouldn't be very useful either.
A workaround to this limitation is to define a class which itself exposes the literal value you are interested in. For example:
public interface ILiteralInteger
{
int Literal { get; }
}
public class LiteralInt10 : ILiteralInteger
{
public int Literal { get { return 10; } }
}
public class MyTemplateClass< L > where L: ILiteralInteger, new( )
{
private static ILiteralInteger MaxRows = new L( );
public void SomeFunc( )
{
// use the literal value as required
if( MaxRows.Literal ) ...
}
}
Usage:
var myObject = new MyTemplateClass< LiteralInt10 >( );
C# does not support non-type generic parameters like C++ does.
C# generics are far simpler and less capable than C++ templates. MSDN has a succinct list of Differences Between C++ Templates and C# Generics.
C# generics are specialized at run-time, whereas C++ templates are processed at compile-time to make an entirely new type. Given this, the runtime simply doesn't have the features to process non-type parameters (it's not just a C# issue).
With C# 10, there's something that comes sort of close:
interface IConstant
{
abstract static int Constant { get; }
}
class B<T> where T : IConstant
{
int GetConstant() => T.GetConstant();
}
class Constant1 : IConstant { static int Constant => 1; }
class Constant2 : IConstant { static int Constant => 2; }
This does not guarantee that Constant is actually constant sadly, and you need to define types for all numbers you want to use. However it does allow you to discriminate B<Constant1> and B<Constant2>
Not nearly an equivalent but something at least
Note that you need to add <EnablePreviewFeatures>true</EnablePreviewFeatures> to your .csproj for this to work, as abstract static Interface methods are still in preview
The following approach may be useful depending on what you're trying to accomplish. The size of the underlying collection is determined by an abstract property.
public abstract class TupleBase
{
protected abstract int NumElems { get; }
protected object[] values;
protected TupleBase(params object[] init)
{
if (init.Length != NumElems)
{
throw new ArgumentException($"Collection must contains {NumElems} elements", nameof(init));
}
values = new object[NumElems];
for (int i = 0; i < init.Length; i++)
{
values[i] = init[i];
}
}
protected object Get(int idx) => values[idx];
protected void Set(int idx, object value) => values[idx] = value;
}
public class Tuple1<T> : TupleBase {
protected override int NumElems => 1;
public Tuple1(T val0) : base(val0) { }
public T Get0() => (T)Get(0);
public void Set0(T value) => Set(0, value);
}
public class Tuple2<T, U> : TupleBase {
protected override int NumElems => 2;
public Tuple2(T val0, U val1) : base(val0, val1) { }
public T Get0() => (T)Get(0);
public U Get1() => (U)Get(1);
public void Set0(T value) => Set(0, value);
public void Set1(U value) => Set(1, value);
}
public class Tuple3<T, U, V> : TupleBase
{
protected override int NumElems => 3;
public Tuple3(T val0, U val1, V val2) : base(val0, val1, val2) { }
public T Get0() => (T)Get(0);
public U Get1() => (U)Get(1);
public V Get2() => (V)Get(2);
public void Set0(T value) => Set(0, value);
public void Set1(U value) => Set(1, value);
public void Set2(V value) => Set(2, value);
}
public class Tuple4<T, U, V, W> : TupleBase
{
protected override int NumElems => 4;
public Tuple4(T val0, U val1, V val2, W val3) : base(val0, val1, val2, val3) { }
public T Get0() => (T)Get(0);
public U Get1() => (U)Get(1);
public V Get2() => (V)Get(2);
public W Get3() => (W)Get(3);
public void Set0(T value) => Set(0, value);
public void Set1(U value) => Set(1, value);
public void Set2(V value) => Set(2, value);
public void Set3(W value) => Set(3, value);
}
public class Tuple5<T, U, V, W, X> : TupleBase
{
protected override int NumElems => 5;
public Tuple5(T val0, U val1, V val2, W val3, X val4) : base(val0, val1, val2, val3, val4) { }
public T Get0() => (T)Get(0);
public U Get1() => (U)Get(1);
public V Get2() => (V)Get(2);
public W Get3() => (W)Get(3);
public X Get4() => (X)Get(4);
public void Set0(T value) => Set(0, value);
public void Set1(U value) => Set(1, value);
public void Set2(V value) => Set(2, value);
public void Set3(W value) => Set(3, value);
public void Set4(X value) => Set(4, value);
}