In debugging step through, Visual Studio 2013 shows BitConverter.IsLittleEndian is:
false: When I hover mouse on BitConverter and see the value of BitConverter.IsLittleEndian and
true: When I put it in a variable like var x = BitConverter.IsLittleEndian;
I assume BitConverter.IsLittleEndian should be already evaluated because I have called GetBytes on BitConverter so it's static constructor should be called at this point, right? What am I missing?
My code is this (I wanted to generate sequential Guid; rest is bytes of a long counter - at this version):
static Guid Id(long ticks, byte[] rest)
{
var ticksBytes = BitConverter.GetBytes(ticks).PolishEndian();
// var x = BitConverter.IsLittleEndian; // <- TESTED HERE
int a = BitConverter.ToInt32(new byte[] { ticksBytes[4], ticksBytes[5], ticksBytes[6], ticksBytes[7] }.PolishEndian(), 0);
short b = BitConverter.ToInt16(new byte[] { ticksBytes[2], ticksBytes[3] }.PolishEndian(), 0);
short c = BitConverter.ToInt16(new byte[] { ticksBytes[0], ticksBytes[1] }.PolishEndian(), 0);
return new Guid(a, b, c, rest.PolishEndian(true).ToArray());
}
static byte[] PolishEndian(this byte[] ba, bool reverse = false)
{
var flag = reverse ? BitConverter.IsLittleEndian : !BitConverter.IsLittleEndian;
if (flag) return ba.Reverse().ToArray();
return ba;
}
Note that in this case IsLittleEndian is actually a field and not a property. That has an effect on how the EE is able to process the value.
I tried this out locally and this is the behavior I saw
First i stepped until the cursor hit the var ticksBytes line. At that point I observed that IsLittleEndian == false. This is actually expected at this point. The EE does not always need to force a static constructor to run in order to read fields. Hence it is just reading the value as is and because no other code for BitConverter has run the value is false
Immediately after stepping over that line I observe that IsLittleEndian == true. The CLR ran the static constructor in order to execute the GetBytes method and hence that set the field. The EE was then reading the set field.
Note that you can recreate this example with your own code. For example
static class Test {
static readonly bool example;
static Test() {
example = true;
}
internal static void Go() {
// example == true
}
}
class Program {
static void Main() {
// Test.example == false;
Test.Go();
}
}
Earlier I mentioned that the EE didn't always need to execute a static constructor in order to read fields. One case where it often needs to is when reading static fields off of a generic type. The storage for a static field of a generic type isn't created essentially until the CLR instantiates an instance of the type. Hence in order to read a field off of a generic type which hasn't yet been used the EE will create an instance under the cover in order to force the CLR to read it. For example
static class Test<T>
{
static readonly bool example = false;
static Test()
{
example = true;
}
}
If you add this to your program and then evaluate the following in the watch window
Test<int>.example
you will find that the value is true clearly indicating the cctor ran
Related
I have been trying for more time than i would like to admit to get this to work. I'm pretty new at programming so this might be a very simple question. (I thought that when i started at least)
I really need to know how i can increase an existing value in increments using a method.
class Program
{
static void Main(string[] args)
{
int TestValue = 0;
Console.WriteLine(TestValue);
TestingMethod(TestValue);
Console.WriteLine(TestValue);
TestingMethod(TestValue);
Console.WriteLine(TestValue);
}
static int TestingMethod(int Start)
{
Start++;
return Start;
}
}
I want the program to print out 1, 2 and 3 using some sort of method. (preferably basic since i want to understand what i'm actually doing.) I also want to use multiple values with this method so simply putting in numbers won't work for me here.
I've already tried the things i know but since i don't have much knowledge of programming i may have used them incorrectly so they don't really matter i think.
Your method is correct but you need to write:
Console.WriteLine(TestValue);
TestValue = TestingMethod(TestValue);
Console.WriteLine(TestValue);
TestValue = TestingMethod(TestValue);
Console.WriteLine(TestValue);
This works like using the string Replace method for example, where you need to assign to a string the result of the method, the same variable or another.
An alternative is to pass the parameter by reference, so it will be modified, else it is passed by value because int is a value type:
static void Main(string[] args)
{
int TestValue = 0;
Console.WriteLine(TestValue);
TestingMethod(ref TestValue);
Console.WriteLine(TestValue);
TestingMethod(ref TestValue);
Console.WriteLine(TestValue);
}
static void TestingMethod(ref int Start)
{
Start++;
}
Passing Reference-Type Parameters (C# Programming Guide)
Since “int Start” is not a reference, every time you call TestingMethod(TestValue) you are sending 0 to the method, you are also not doing anything to the return value, so the program always prints 0.
As the previous answer said, using ref at the signature is a good idea, another way of doing this could be by creating your own counter class that can be passed as a reference.
public class MethodCounter
{
private int counterValue = 0;
public MethodCounter(int initialCount = 0)
{
counterValue = initialCount;
}
public void IncreaseCount()
{
counterValue++;
}
public int GetCount()
{
return counterValue;
}
}
Making your program class look like this
class Program {
static void Main(string[] args) {
MethodCounter counter = new MethodCounter();
Console.WriteLine(counter.GetCount());
TestingMethod(counter);
Console.WriteLine(counter.GetCount());
TestingMethod(counter);
Console.WriteLine(counter.GetCount());
}
static void TestingMethod(MethodCounter counter)
{
counter.IncreaseCount();
}
}
When passing data into methods, there are two ways that it can happen
Pass-By-Reference
Pass-By-Value
Because int is a primivite data type, your data is being sent using pass-by-value by defualt. Here's a general reference for primitives https://en.wikipedia.org/wiki/Primitive_data_type
When passing primitive type variables to methods, they are generally sent using Pass-By-Value. This means that the method receives a copy of your variable - and not the actual variable itself. Since the operations performed are being done on a different copy of your variable, your original copy isnt acutally being changed.
When passing Non-Primitive variables to a method (like StringBuilder, or HttpClient), they are generally sent using Pass-By-Reference. This means that you are passing the reference to your variable, aka the location in memory where it is stored. when the method receives the reference, it can find the exact copy of the variable you were working with, and perform operations on it
As per your requirement, your code should look like this, you have not assigned the returned value to the TestValue.
public static void Main(string[] args)
{
int TestValue = 0;
TestValue = TestingMethod(TestValue);
Console.WriteLine(TestValue);
TestValue = TestingMethod(TestValue);
Console.WriteLine(TestValue);
TestValue = TestingMethod(TestValue);
Console.WriteLine(TestValue);
}
static int TestingMethod(int Start)
{
return Start+=1;
}
I'm having some trouble with computed data from a static class in a C# Xunit test being computed twice.
The actual production code this would be used for is much more complicated, but the code that follows is enough to exhibit the issue I am seeing.
In the code below I have a randomly generated, lazily loaded int seeded off of the current time.
All I am testing here is that this property is equal to itself. I insert the property's value into the test via a MemberData function.
Since the property ought to only be initialized once, I'd expect that this test should always pass. I would expect that the static field would be initialized when the RandomIntMemberData function is run and never again.
However, this test consistently fails. The value inserted into the test, and the value tested against are always different.
Further if I debug, I only see the initialization code being hit once. That is, the value being tested. I never see the initialization of the value being tested against.
Am I misunderstanding something, or is Xunit doing some weird behind the scenes magic to setup it's input data, then initializing the value again when the test is actually run?
Minimal Code to Reproduce Bug
public static class TestRandoIntStaticClass
{
private static readonly Lazy<int> LazyRandomInt = new Lazy<int>(() =>
{
// lazily initialize a random interger seeded off of the current time
// according to readings, this should happen only once
return new Random((int) DateTime.Now.Ticks).Next();
});
// according to readings, this should be a thread safe operation
public static int RandomInt => LazyRandomInt.Value;
}
The Test
public class TestClass
{
public static IEnumerable<object[]> RandomIntMemberData()
{
var randomInt = new List<object[]>
{
new object[] {TestRandoIntStaticClass.RandomInt},
};
return randomInt as IEnumerable<object[]>;
}
[Theory]
[MemberData(nameof(RandomIntMemberData))]
public void RandoTest(int rando)
{
// these two ought to be equal if TestRandoIntStaticClass.RandomInt is only initialized once
Assert.True(rando == TestRandoIntStaticClass.RandomInt,
$"{nameof(rando)} = {rando} but {nameof(TestRandoIntStaticClass.RandomInt)} = {TestRandoIntStaticClass.RandomInt}");
}
}
At the time of tests discovery, Visual Studio Xunit console runner creates AppDomain with test data for all attributes like MemberData, ClassData, DataAttribute so all data are just saved in memory after build (that is also why XUnit require classes to be serializable).
We can verify this by adding a simple logger to your methods:
namespace XUnitTestProject1
{
public class TestClass
{
public static IEnumerable<object[]> RandomIntMemberData()
{
var randomInt = new List<object[]>
{
new object[]
{TestRandoIntStaticClass.RandomInt},
};
return randomInt;
}
[Theory]
[MemberData(nameof(RandomIntMemberData))]
public void RandoTest(int rando)
{
// these two ought to be equal if TestRandoIntStaticClass.RandomInt is only initialized once
Assert.True(rando == TestRandoIntStaticClass.RandomInt, $"{nameof(rando)} = {rando} but {nameof(TestRandoIntStaticClass.RandomInt)} = {TestRandoIntStaticClass.RandomInt}");
}
}
public static class TestRandoIntStaticClass
{
private static readonly Lazy<int> LazyRandomInt = new Lazy<int>(() =>
{ // lazily initialize a random interger seeded off of the current time
// according to readings, this should happen only once
var randomValue = new Random((int) DateTime.Now.Ticks).Next();
File.AppendAllText(#"D:\var\log.txt", $"Call TestRandoIntStaticClass {randomValue}; ThreadId {Thread.CurrentThread.ManagedThreadId} " + Environment.NewLine);
return randomValue;
});
public static int RandomInt => LazyRandomInt.Value; // according to readings, this should be a thread safe operation
}
}
As a result we see in logs:
> Call TestRandoIntStaticClass 1846311153; ThreadId 11
> Call TestRandoIntStaticClass 1007825738; ThreadId 14
And in test execution result
rando = 1846311153 but RandomInt = 1007825738
Expected: True
Actual: False
at
However, if you use dotnet test will be successful because 'data generation' and test run will be launched on one process
I'm just returning back to C# after an extended period of C++ and Qt. I'm currently stumped by what I would have thought to be a very simple problem.
I have a struct:
struct FontGlyph {
public uint codepoint;
public byte[] bmp;
...
}
And an array of such structs:
FontGlyph[] glyphs = new FontGlyph[100];
Now, I have a couple of functions which set up and modify the fields in the structs:
static void ConvertFont(Font fnt) {
...
if (fnt.IsSpaceCharacter) {
glyphs[idx].codepoint = 0x00FF;
glyphs[idx].bmp = null;
}
else {
RenderFontGlyph(glyphs[idx]);
// glyphs[idx].bmp is NOT ok here - it is null!
}
...
}
static void RenderFontGlyph(FontGlyph glyph) {
...
glyph.bmp = new byte[256];
// bmp is fine here, and can be populated as a normal array
...
}
This isn't a particularly great snippet of code, however, in the RenderFontGlyph function, I can see that the bmp array is allocated correctly yet when the RenderFontGlyph function returns, upon inspection of the glyphs[idx] variable, bmp is back to null.
I appreciate I'm probably doing something n00bish but its been a while. Am I a victim of garbage collection or am I being stupid? It had occurred to me that the struct was being passed into the RenderFontGlyph function by-value rather than by-ref but this also makes no difference!
It had occurred to me that the struct was being passed into the RenderFontGlyph function by-value rather than by-ref but this also makes no difference!
Well yes, it does. You're creating a copy of the struct, and passing that into RenderFontGlyph. Any changes made to that copy don't affect anything else.
If you pass it by reference instead, it will make a difference, because you'll be modifying the original storage location in the array:
RenderFontGlyph(ref glyphs[idx]);
...
static void RenderFontGlyph(ref FontGlyph glyph)
Or you could keep using a value parameter, and make RenderFontGlyph return the modified value which you'd need to store back in the array, as per Leonardo's answer.
I certainly wouldn't go so far as to say that you're being stupid, but it's really, really important that you understand the semantics of reference types and value types, particularly if you're creating mutable value types. (And worse, a mutable value type containing a reference to a mutable reference type - the array in this case. You can mutate the array without mutating the struct... this could all become very confusing if you're not careful.)
Unless you have a really good reason to create mutable value types, I'd strongly advise against it - just like I'd also advise against exposing public fields. You should almost certainly be modelling FontGlyph as a class - it doesn't feel like a natural value type to me. If you do want to model it as a value type, then rather than passing in a FontGlyph at all, why not just pass in the code point you want to render, and make the method return the glyph?
glyphs[0] = RenderGlyph(codePoint);
As you're claiming that pass-by-reference isn't working for you, here's a complete example demonstrating that it does work. You should compare this with your code to see what you're doing wrong:
using System;
struct FontGlyph
{
public uint codepoint;
public byte[] bmp;
}
class Program
{
static void Main()
{
FontGlyph[] glyphs = new FontGlyph[100];
RenderFontGlyph(ref glyphs[0]);
Console.WriteLine(glyphs[0].bmp.Length); // 10
}
static void RenderFontGlyph(ref FontGlyph glyph)
{
glyph.bmp = new byte[10];
}
}
How about:
static void ConvertFont(Font fnt) {
...
if (fnt.IsSpaceCharacter) {
glyphs[idx].codepoint = 0x00FF;
glyphs[idx].bmp = null;
}
else {
glyphs[idx] = RenderFontGlyph(glyphs[idx]);
// glyphs[idx].bmp is NOT ok here - it is null!
}
...
}
static FontGlyph RenderFontGlyph(FontGlyph glyph) {
...
glyph.bmp = new byte[256];
// bmp is fine here, and can be populated as a normal array
...
return glyph;
}
or use ref like this: static void RenderFontGlyph(ref FontGlyph glyph) and then call it like this: RenderFontGlyph(ref glyphs[idx])
I have the following code, the idea is simple if the object is in cache get it, if not then retrieve it from the data source and save it into cache, I am using resharper and I got this warning but cant understand why
public static ModulosPorUsuario GetModulesForUser(string identityname)
{
// It needs to be cached for every user because every user can have different modules enabled.
var cachekeyname = "ApplicationModulesPerUser|" + identityname;
var cache = CacheConnectionHelper.Connection.GetDatabase();
ModulosPorUsuario modulosUsuario;
//get object from cache
string modulosUsuariosString = cache.StringGet(cachekeyname);
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (modulosUsuariosString != null)
{
//conver string to our object
modulosUsuario = JsonConvert.DeserializeObject<ModulosPorUsuario>(modulosUsuariosString);
return modulosUsuario;
}
// ReSharper disable once HeuristicUnreachableCode
modulosUsuario = DbApp.ModulosPorUsuario.Where(p => p.Email == identityname).FirstOrDefault();
//convert object to json string
modulosUsuariosString = JsonConvert.SerializeObject(modulosUsuario);
//save string in cache
cache.StringSet(cachekeyname, modulosUsuariosString, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
return modulosUsuario;
}
There's quite a lot going on here, but the bottom line, this is a ReSharper bug - the value can certainly be null, and I have a much smaller example proving it.
First, let's figure out what's going on in your code. I had to dig a little bit into the StackExchange.Redis library that you're using. Your cache object is, in fact, an IDatabase, which is implemented by the RedisDatabase class. The StringGet method that you're using returns a RedisValue, which is a struct. This, by itself, would make perfect sense why ReSharper tells you it can never be null - value types can't!
However, you're putting the result into a string variable! This works because the RedisValue struct defines a bunch of implicit operators to convert the value into the requested type. In case of a string, notice that if the blob is empty, an empty string is returned:
RedisValue.cs
/// <summary>
/// Converts the value to a String
/// </summary>
public static implicit operator string(RedisValue value)
{
var valueBlob = value.valueBlob;
if (valueBlob == IntegerSentinel)
return Format.ToString(value.valueInt64);
if (valueBlob == null) return null;
if (valueBlob.Length == 0) return "";
try
{
return Encoding.UTF8.GetString(valueBlob);
}
catch
{
return BitConverter.ToString(valueBlob);
}
}
But from this code it's obvious that the string can be null as well.
This makes ReSharper incorrect to flag that line, and it can be reproduced with a smaller example:
static void Main(string[] args)
{
string value = MyStruct.GetValue();
if (value == null) // <- ReSharper complains here, but the value is null!
{
return;
}
}
public struct MyStruct
{
public static MyStruct GetValue() => new MyStruct();
public static implicit operator string(MyStruct s)
{
return null;
}
}
I reported this issue to JetBrains, they will fix it.
In the meantime, you might want to keep that comment, disabling ReSharper warning.
I'm trying to use this great project but since i need to scan many images the process takes a lot of time so i was thinking about multi-threading it.
However, since the class that makes the actual processing of the images uses Static methods and is manipulating Objects by ref i'm not really sure how to do it right. the method that I call from my main Thread is:
public static void ScanPage(ref System.Collections.ArrayList CodesRead, Bitmap bmp, int numscans, ScanDirection direction, BarcodeType types)
{
//added only the signature, actual class has over 1000 rows
//inside this function there are calls to other
//static functions that makes some image processing
}
My question is if it's safe to use use this function like this:
List<string> filePaths = new List<string>();
Parallel.For(0, filePaths.Count, a =>
{
ArrayList al = new ArrayList();
BarcodeImaging.ScanPage(ref al, ...);
});
I've spent hours debugging it and most of the time the results i got were correct but i did encounter several errors which i now can't seem to reproduce.
EDIT
I pasted the code of the class to here: http://pastebin.com/UeE6qBHx
I'm pretty sure it is thread safe.
There are two fields, which are configuration fields and are not modified inside the class.
So basically this class has no state and all calculation has no side effects
(Unless I don't see something very obscure).
Ref modifier is not needed here, because the reference is not modified.
There's no way of telling unless you know if it stores values in local variables or in a field (in the static class, not the method).
All local variables will be fine and instanced per call, but the fields will not.
A very bad example:
public static class TestClass
{
public static double Data;
public static string StringData = "";
// Can, and will quite often, return wrong values.
// for example returning the result of f(8) instead of f(5)
// if Data is changed before StringData is calculated.
public static string ChangeStaticVariables(int x)
{
Data = Math.Sqrt(x) + Math.Sqrt(x);
StringData = Data.ToString("0.000");
return StringData;
}
// Won't return the wrong values, as the variables
// can't be changed by other threads.
public static string NonStaticVariables(int x)
{
var tData = Math.Sqrt(x) + Math.Sqrt(x);
return Data.ToString("0.000");
}
}