Java Equivalent to #Region in C#

Java equivalent to #region in C#

There's no such standard equivalent. Some IDEs - Intellij, for instance, or Eclipse - can fold depending on the code types involved (constructors, imports etc.), but there's nothing quite like #region.

C-sharp's #region & #endregion in Java?

No, there is nothing equivalent.

Code folding is an IDE feature, not a language feature.

Eclipse has code folding for classes and members when Java files are loaded in it. It may be extended (for example) to add code folding on carefully crafted comment lines.

C# equivalent of Javas Guavas Maps.uniqueIndex

In case of C# where we have IEnumerable<V> instead of Iterable<V> and Dictionary insetead of Map. That's why we can put in C#

using System.Linq;

...

Dictionary<K, V> result = values.ToDictionary(v => keyFunction(v));

where values impelements IEnumerable<V> and keyFunction is Func<V, K>. In your case C# code can be something like this

using System.Linq;

...

// I've turned Java getPassportId method into PassportId property
// "Dictionary<String, Human>" declaration can be changed into just "var"
Dictionary<String, Human> idToHuman = humans.ToDictionary(human => human.PassportId);

Java IntStream, Range and mapToDouble and reduce function equivalent in C#

Code using streams in Java usually translates well into LINQ in .NET.

map or mapToXXX works like Select, reduce is Aggregate, but here Sum is more convenient. IntStream.range is Enumerable.Range. Everything else should have a "obvious" equivalent.

public static double[] LogSumExp(IList<double> a, IList<double> b) {
double amax = a.Max();
double sum = Enumerable.Range(0, a.Count)
.Select(i => Math.Exp(a[i] - amax) * (i < b.Count ? b[i] : 1.0))
.Sum();
double sign = Math.Sign(sum);
sum *= sign;
double abs = Math.Log(sum) + amax;
double[] ret = {abs, sign};
return ret;
}

If you are using C# 7+, you should really be returning a tuple instead:

public static (double abs, double sign) LogSumExp(IList<double> a, IList<double> b) {
...
return (abs, sign);
}

C# equivalent of Java Class? extends Base?

You could use a generic constraint.

public void someMethod<T>() where T : SomeType {
Type myType = typeof(T);

// do something with myType
}

Here's an implementation of your desired use case.

class EventManager {
public Dictionary<Type, ICollection<EventHandler>>
HandlerMap = new Dictionary<Type, ICollection<EventHandler>>();

public void RegisterEventHandler<TEvent>(EventHandler eh)
where TEvent: SomeEventType
{
Type eventType = typeof(TEvent);

if (!HandlerMap.ContainsKey(eventType)) {
HandlerMap[eventType] = new List<EventHandler>();
}

HandlerMap[eventType].Add(eh);
}
}

You would call it like this.

var em = new EventManager();
EventHandler eh = null; // or whatever
em.RegisterEventHandler<SpecificEventType>(eh);

Problem with C# equivalent of Java Unsigned right bit-shift operator

In your Java code, you're using the >>> operator on a 64-bit integer - in your C# code, you're using it on a 32-bit integer.

The reason you're getting the rather large value for s (instead of just 0, as you might expect if you shifted a 32-bit integer right by more than 32 bits) is that when you use >> 39 on a 32-bit integer, it actually performs a >> 7, which is odd but is in line with the specification:

When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other words, the shift count is computed from count & 0x1F.

I would suggest casting (once) to a ulong, and doing all the shift operations on that:

ulong uhand = (ulong) hand;
int c = (int)hand & 0x1FFF;
int d = (int)(uhand >> 13) & 0x1FFF;
int h = (int)(uhand >> 26) & 0x1FFF;
int s = (int)(uhand >> 39);

That gives you the results you expect.

VS/C# Equivalent of Java/Eclipse resource folder?

After a little research, I had found a solution for this problem. There are in fact two possible solutions to this issue.


.NET Core Solution

The first involves editing the .csproj file of your C# project. This solution is only available in .Net Core.

You can add this code snippet to your file and change the {PATH_TO_RESOUCE_FOLDER_HERE} folder to your desired folder.

<ItemGroup>
<EmbeddedResource Include="{PATH_TO_RESOUCE_FOLDER_HERE}\**" />
</ItemGroup>

Now any item placed in that folder will be considered an embedded resource Assembly.GetManifestResourceStream(string name) method.


Regular .NET Solution

The second method involves using a .resx file to encapsulate all of your resources

In Visual Studio 2019, you can create a .resx file by right clicking on the location in your project where you wish to add the file to, and navigating to Add > New Item (you may also press Ctrl+Shift+A). You can now navigate to the item that quotes "Resources File" and select it. You can now use this GUI to insert your resources (for a deeper explanation, click on this or this link. For use cases, see this MSDN).

The "Resources File" option

Note that this solution will also work in .NET Core.


I hope this answer helped you as much as it did me! :)



Related Topics



Leave a reply



Submit