This question already has answers here:
Parameter in C#
(4 answers)
How do the In and Out attributes work in .NET?
(2 answers)
Closed 10 years ago.
Once I know what this means, there's probably a better way of expressing this question, but I don't know where to start.
Here's the Read method for a StreamReader:
public override int Read([In, Out] char[] buffer, int index, int count) { ... }
That "In, Out" bit - what is it for, what does it do, what's it called?
That "In, Out" bit - what is it for, what does it do, what's it called?
They are parameter attributes.
In this case System.Runtime.InteropServices.InAttribute and System.Runtime.InteropServices.OutAttribute which are used for interop with code outside the .NET runtime.
Related
This question already has answers here:
What are generics in C#? [closed]
(3 answers)
Understanding C# generics much better
(5 answers)
Closed 9 months ago.
In C# Documentations, there was an example under constructed types where they use class Queue , what is TElement and what does it mean /represent?
It's a placeholder for whatever type you wish to specify.
It could be Queue<string> or Queue<int> or Queue<List<int> or Queue<SomeComplicatedClass> - whatever you like.
It's called a "generic type parameter" (the bit between the < and the >).
The docs page has the following example:
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
You can read more about generic type parameters here.
This question already has answers here:
C# memcpy equivalent
(7 answers)
memcpy function in c# [duplicate]
(6 answers)
Closed 8 years ago.
I want to convert the code from C++ to C#, there is one line code regarding with memcpy. Not sure how to convert it.
memcpy(Bucket, nBucket, 4 * L);
The original code is from TopCoderForums. I finished most of them except a few lines.
In that specific example of code (where Bucket and nBucket are int arrays) here is what you can do in c#:
Array.Copy(nBucket, Bucket, 4 * L)
(Note that I think souce and destination should be swapped around)
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why it is not posible to define generic indexers in .NET?
how to write a function to take any object with an index operator
I've never seen any usage like that. But I just wonder if it is possible to make an implementation like bleow. I know that it's not working. But I mean a similar usage if exist.
public T this<T>[T param]
{
get
{
....
}
}
No, generic properties, and indexers (a property), aren't possible.
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.