Type of A field is its Class Name. What is this called in C#? - c#-3.0

I new to C#, Sometime I see C# code like this
public class ClassName
{
ClassName field;
}
It mean the field type the same as Class name. WHat this mean and called in C# ?

It is a reference to an instance of the type itself. E.g. An element in a linked list could reference the next element or if the type was a WebPage, it could have a reference to another WebPage.

Related

Binding a CollectionView item to a member variable, not a property

I am using .net MAUI's <CollectionView> with a nested <CollectionView.ItemTemplate> to bind to a collection of objects. I understand how to define the item template so that it contains Label objects that bind to properties of the objects in the collection: using (for example) Text="{Binding Surname}". This works correctly.
I'd also like to bind to a collection of objects that expose readonly member variables, not properties, but binding by name does not result in a value being displayed. No runtime errors occur.
Is there a way to bind collection view items to member variables?
(Using VS 2022 Preview 7.4.0 with .net 6)
Is there a way to bind collection view items to member variables?
No, it only works with public properties. This is simply the way it's implemented. The XAML binding engine will go through the object that serves as a binding context and will look for public properties only.
A work-around is to add a property that returns the member variable.
Given MyType MyVariable;, add:
public MyType MyProperty => MyVariable;
That is shorthand for public MyType MyProperty { get { return MyVariable; } }.

MongoDB c# sharp Property vs Field vs Member vs Element

Looking into Custom Serialization, what's the difference between
A "Property" BsonClassMap.MapProperty
A "Field" BsonClassMap.MapField
A "Member" BsonClassMap.MapMember
This answer should cover it:
What is the difference between a Field and a Property in C#?
C# fields are meant to be hidden. Properties expose fields.
From http://api.mongodb.org/csharp/1.0/html/18aadb76-2494-c732-9768-bc9f41597801.htm
MapProperty
Creates a member map for a property and adds it to the class map.
MapField
Creates a member map for a field and adds it to the class map.
MapMember
Creates a member map for a member and adds it to the class map.

Get the class structure instance of a GObject type

How do I get a class object of a certain class in GObject / Gtk? For example, if my class is GtkSpinButton, I want to get the instance of GtkSpinButtonClass that represents the class. It is the parameter "class" in
gtk_spin_button_class_init (GtkSpinButtonClass *class)
and it is the object where virtual functions are stored. When I have an instance of GtkSpinButton, I can call
GtkSpinButtonClass *class = GTK_SPIN_BUTTON_GET_CLASS (instance)
however I don't have an instance around. GTK_TYPE_SPIN_BUTTON gives me the type id, a number, and not the class object. Any idea how to get the actual instance?
You want to use g_type_class_ref
GtkSpinButtonClass *klass = g_type_class_ref(GTK_TYPE_SPIN_BUTTON);
and when you're done with it
g_type_class_unref(klass);

After raising IEnumerable.OrderBy(), the source list was not sorted,why?

1.First I defined an extension method for the IEnumerable.Add() like the code below
public static IEnumerable<T> Add<T, TKey>(this IEnumerable<T> enumerable, T value, Func<T, TKey> orderBy)
{
if (enumerable == null)
return null;
if (enumerable is IList<T>)
{
var list = enumerable as IList<T>;
if (!enumerable.Contains(value))
{
list.Add(value);
enumerable = enumerable.OrderBy(orderBy);
}
}
}
2.Then,I raised the extension method like this to sort the itemlist according to the "Date" property when a new item was added to the list:
itemList.Add(item, o => o.Date);
3.After all,it appears that the "itemList" was not sorted.
4.I followed the extension method and found that "enumerable" was a new instance after "enumerable = enumerable.OrderBy(orderBy)" and it was sorted,but the "list" was not.
5.Then I tried to cast the sorted enumerable to list like "list=enumerable.ToList()",both of them("enumerable" and "list") were sorted.
6.After that ,when the call stack went back to the "itemList.Add(item, o => o.Date);",the "itemList" was not sorted at all!!!
Anyone can give me some advices?Thanks a looooooooooooooooooooooooooooooooot!!
I believe your problem is that the reference to enumerable is being passed by value rather than by reference. See Jon Skeet's article about passing parameters by value or reference for more information about what that means. In short, C# passes a copy of the parameter's reference so assigning a new value to parameter does not change the reference of the object that was passed in. To pass a parameter by reference you specify the ref keyword, but I don't think that will work with an extension method. If you're dead set on making this work I would suggest inserting the items into your List in sorted order, probably requiring that T implement IComparable.
Update:
First off, see the Skeet's article it's really quite informative and I will probably only be half as clear as he is. Second, when you pass an object as a parameter to a method you are passing a copy of the reference. This means you can still access members of the object but, the same way that a value type is passed by copy, if you modify the reference (ie assign it a new value) you wont modify the original reference. Specifying ref means that you are passing a reference to the reference and changing the reference (assigning a value to it) will affect the original object.
Neither OrderBy or ToList will affect the source list. When you did this: list=enumerable.ToList() you changed your variable to point to a whole new list instance.
It appears to me that this method does too much. I would keep adding and sorting as separate operations. The fact that this extends IEnumerable but silently does nothing if the target is not an IList is a code smell.

How to specify a List<MyCustomType> as a "Return Type" for a UML Interface Property

In my Visio 2007 UML document I am unable to figure out how I can add an operation to an Interface that returns a generic List<MyCustomType> type.
For example:
Say I have a class named "MyClass" and an Interface named "IFace". IFace has a signature of a method that returns a Generic List of MyClass.
For clarity, here's an example of the C# code:
namespace StackO
{
public interface IFace
{
List<MyClass> SomeMethod(string data);
}
public class MyClass
{
}
}
Here's a screenshot of where I'm stuck:
It seems as though the only way to specify a List<MyClass> as my Return Type is to create another user-defined datatype that is explicitly written as List<MyClass>. If this is the case, so be it. However, I'm posting this in hopes that there is a better/proper way to do this.
How can I define the Return Type of an Operation of a Visio Interface to be a Generic List of a User-Defined Datatype?
In the Class diagram properties > Go to operations > select the return type you are interested in changing and click properties.
In the next dialog you will have option for setting prefix List< and suffix >.
This way you can specify the return type as List<>.
I see this option in Visio 2010. But I am not sure if this option is available in Visio 2007.
There is no such a thing as T1<T2> in UML class diagrams.
If you want to specify that the method returns several values, the correct notation is:
SomeMethod(data: String) : MyClass [*]
This notation is much more powerful than the one used by C#. List<MyClass> SomeMethod(string data) gives no information about the contract of the method. With UML, you know that in:
SomeMethod(data: String) : MyClass [*]
SomethingElse() : String [1..*]
LastExample(number: UnlimitedNatural) : Integer [0..1]
SomeMethod returns a sequence containing zero or more elements. SomethingElse returns a sequence of one or several elements: this sequence is never empty. Finally, LastExample returns an optional value. This could be expressed in C# as int? LastExample(uint number) — see, no IEnumerable here.
Also note that:
SomeMethod(data: String) : MyClass [0..*]
shouldn't be used, since [*] means the same thing and is shorter. As for:
SomeMethod(data: String) : MyClass [0..n]
is incorrect, despite being used a lot on the internet.