Inline using how to make use of it [duplicate] - c#

This question already has answers here:
Using statement question
(6 answers)
What are the uses of "using" in C#?
(29 answers)
Closed 4 years ago.
In C# one can type the using verb in line width the code sometimes, like
using (textwriter){ ..... }
I like that writing style and am wondered what is required to allow that for my own Api's.

As long as your objects are IDisposable then you can use it with the using statement.

Related

C# Check if a method exists in TDD [duplicate]

This question already has answers here:
How to check whether an object has certain method/property?
(5 answers)
Reflection GetMethod. select a more specific method
(3 answers)
Closed 11 months ago.
In Java I can check if a method named "addNumbers" exists in a class "Calculate" by doing this:
Class<?> c = Class.forName("com.example.package.Calculate");
Method m = c.getMethod("addNumbers", int.class, int.class);
m.invoke(5,7)
What is the C# equivalent for this example?

C# Generics, what does T<,> mean? [duplicate]

This question already has answers here:
Generics open and closed constructed types
(3 answers)
C# Language: generics, open/closed, bound/unbound, constructed
(2 answers)
Closed 3 years ago.
I have not seen this <,>-syntax before, and its impossible to google.
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(OuterBehavior<,>));
.. I m guessing its some kind of wildcard, but I cant find any documentation
Source (for example)

What are [..] called above enum / class definition? [duplicate]

This question already has answers here:
What does the [Flags] Enum Attribute mean in C#?
(14 answers)
What does square bracket [] mean in the below code?
(2 answers)
what is [] brackets in .net? [duplicate]
(7 answers)
Closed 6 years ago.
Sorry for the silly question, but I came across the following C# code and I'm wondering what the [Flags] portion is and what it does.
[Flags]
public enum UserFlags
{
//...
}
Thank you in advance.
It's a class attribute. There are also method attributes for example.
You can even write your own
They are usually used to define meta information or behaviour about a class or method and can be read using reflection.

How to declare an Optional type in C#? [duplicate]

This question already has answers here:
Optional return in C#.Net
(13 answers)
Closed 8 years ago.
Java 8 has Optional<T> which is nice way to declare optional types as described here.
Is there an equivalent way to that in C# ?
As per this answer You could make a struct that mimics this this type of functionality.

byte[] to string without knowing the coding [duplicate]

This question already has answers here:
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
(41 answers)
Determine a string's encoding in C#
(10 answers)
How to find out the Encoding of a File? C#
(5 answers)
Closed 9 years ago.
I saw this answer, The problem is - so I also need to know what type of encoder to use for getting the correct string - it may be in UTF\UTF8\ANSI.
Here is a example from the immediate window.
Encoding.Unicode.GetString(combinedBuf)
"믯ힿ힩힜힕�"
Encoding.UTF8.GetString(combinedBuf)
"שלום"

Categories