Related / related objects / classes in C # such as LINQ

Maybe this is a newbie question, but can someone explain to me how the linked / linked classes are created (I don't know their real names)? An example could be LINQ TO XML .

When I have the code below:

 XDocument doc = XDocument.Load("..."); XElement element = doc.Element("root"); element.SetAttribute("NewAttribute", "BlahBlah"); doc.Save("..."); 

I only change the element variable (I do not need to update it in the doc because it refers). How to create such classes?

[edit]
I tried @animaonline code and it works

 A a = new A(); B b = aB(0); b.Name = "asd"; Console.WriteLine(a.Bs[0].Name); // output "asd" 

But tell me, what is the difference with the code above and below?

 List<string> list = new List<string>(); list.Add("test1"); list.Add("test2"); var test = list.FirstOrDefault(); test = "asdasda"; Console.WriteLine(list[0]); // output "test1" - why not "asdasda" if the above example works??? 
+6
source share
3 answers

doc.Element is a method, it returns a link to the first (in order) child element with the specified XName.

Consider this example:

 public class A { public A() { this.Bs = new List<B>(); this.Bs.Add(new B { Name = "a", Value = "aaa" }); this.Bs.Add(new B { Name = "b", Value = "bbb" }); this.Bs.Add(new B { Name = "c", Value = "ccc" }); } public List<B> Bs { get; set; } public BB(int index) { if (this.Bs != null && this.Bs[index] != null) return this.Bs[index]; return null; } } public class B { public string Name { get; set; } public string Value { get; set; } } 

Application:

 A a = new A(); var refToA = aB(0); refToA.Value = "Some New Value"; foreach (var bs in a.Bs) System.Console.WriteLine(bs.Value); 

Explanation:

As you can see, the B () method returns a link to a list item in class A, updating it will also change the value in the A.bs list, since it is the same object.

+4
source

I think I understand what you are asking. The validity of my answer is completely based on this premise .: D


Class members do not have to be primitive types - they can be other classes.

Simple example

Given the two classes SimpleXDocument and SimpleXElement, note that SimpleXDocument has a member / exposure property of type SimpleXElement:

 public Class SimpleXDocument { private SimpleXElement _element; public SimpleXDocument(){ this._element = null;} public SimpleXDocument(SimpleXElement element){this._element = element;} public SimpleXElement Element { get{return this._element;} set{this._element = value;} } } Public Class SimpleXElement { private String _value; public SimpleXElement(){this._value = String.Empty;} public SimpleXElement(String value){this._value = value;} public String Value { get{return this._value;} set{this._value = value;} } } 

SimpleXDocument refers to its own element of type SimpleXElement. You can do things like:

 SimpleXDocument doc = new SimpleXDocument(); doc.Element = new SimpleXElement("Test 1"); SimpleXElement element = new SimpleXElement(); element.Value = "Test 2"; doc = New SimpleXDocument(element); SimpleXElement anotherElement = new SimpleXElement("another test"); doc.Element = anotherElement; doc.Element.Value = "yet another test"; anotherElement.Value = "final test"; //NB this will update doc.Element.Value too! 
+1
source

As I understand it, you do not need a link to an element, but a copy of the object behind the link.

I think this answer will help you: fooobar.com/questions/2370 / ...

-2
source

Source: https://habr.com/ru/post/950812/


All Articles