class parent
public string a;
public string b;
// child fields
public string c;
public string d;
public parent(string a, string b)
{
this.a = a;
this.b = b;
}
class child1 : parent
{
public child1(string a, string b, string c) :base(string a, string b)
}
class child2 : parent
{
public child2(string a, string b, string d) :base(string a, string b)
}
IList<parent> parent_list = new List<parent>();
parent_list.Add(new child1("243", "ewfwe", "fewf"));
parent_list.Add(new child2("456", "fewf", "efew"));
parent_list.Add(new child1("123", "efe", "ewfew"));
parent_list.Add(new child2("768", "fewf", "ewf"));
var foo = 123
I want to delete all data for an object which has 123 as their field a.
I cant seem to find the correct way to execute this. is it possible?
Yes, you can apply this code.
parent_list = parent_list.Where(item => item.a != "123").ToList();
More details on Where
Just count backwards through the list, removing items as you go:
for(var i = parent_list.Count - 1; i >= 0; i--)
if(parent_list[i].a == "123")
parent_list.Remove(i);
Related
I have the following code:
struct test {
public int a;
public int b;
public test(int a) {
this(a, null);
}
public test(int a, int b) {
this.a = a;
this.b = b;
}
}
Where I would like to have two different constructors for the test struct, one where I only need to pass in a and another where I can pass in both a and b.
This code does not work, as it fails with a few errors:
For the public test(int a) { line:
Field 'test.a' must be fully assigned before control is returned to the caller
Field 'test.b' must be fully assigned before control is returned to the caller
And for the this(a, null); line:
Method name expected.
The 'this' object cannot be used before all of its fields have been assigned
struct test {
public int a;
public int b;
public test(int a) : this(a, 0) { }
public test(int a, int b = 0) {
this.a = a;
this.b = b;
}
}
You can't assign null to int. Also your variable names are ambiguous. You can use an optional parameter to achieve what you're looking for. Or chaining constructors.
Try this
struct Test
{
public readonly int a;
public readonly int b;
public Test(int a) : this()
{
this.a = a;
this.b = 0;
}
public Test(int a, int b) : this()
{
this.a = a;
this.b = b;
}
}
an alternative is to use an optional parameter with one constructor
public Test(int a, int b = 0) : this()
{
this.a = a;
this.b = b;
}
or instead of 0 to use default(int), or just default
public Test(int a) : this()
{
this.a = a;
this.b = default(int);
}
The : this() call is added by Microsoft when the create constructor tool is used so I have added it here. I think it is just to remind the reader of the code that space in the stack is allocated first and then the fields are assigned.
I also added the readonly keyword because mutable structures are evil and to emphasize the requirements the fields need to be all defined before the constructor ends.
Or you can always use one constructor from another constructor
public Test(int a) : this(a, 0)
{ }
I have a class Parent and another class Child which inherits Parent.
I have an operator overloading method inside Parent and I would like to make it also work on Child. However I am not sure how to do this.
public class Parent
{
public int age;
public static Parent operator + (Parent a, Parent b)
{
Parent c = new Parent();
c.age = a.age + b.age;
return c;
}
}
public class Child : Parent
{
//other fields...
}
The only way I can think of is to copy the exact same method and logic to child. However I believe it's not a good way since the code is redundant: (especially when the code is very long)
public class Child : Parent
{
public static Child operator + (Child a, Child b)
{
Child c = new Child();
c.age = a.age + b.age;
return c;
}
}
I tried to do casting but it fails at runtime:
public class Child : Parent
{
public static Child operator + (Child a, Child b)
{
return (Child)((Parent)a + (Parent)b);
}
}
Is there a better method to achieve this? Thank you very much.
Ultimately you have to create the Child object, but you could move the logic into a protected method.
public class Parent
{
public int age;
public static Parent operator + (Parent a, Parent b)
{
Parent c = new Parent();
AddImplementation(a, b, c);
return c;
}
protected static void AddImplementation(Parent a, Parent b, Parent sum)
{
sum.age = a.age + b.age;
}
}
public class Child : Parent
{
public static Child operator + (Child a, Child b)
{
Child c = new Child();
AddImplementation(a, b, c);
return c;
}
}
Or another option would be to move the logic into protected constructors that the operator calls
public class Parent
{
public int age;
public static Parent operator +(Parent a, Parent b)
{
return new Parent(a, b);
}
protected Parent(Parent a, Parent b)
{
this.age = a.age + b.age;
}
}
public class Child : Parent
{
public static Child operator +(Child a, Child b)
{
return new Child(a, b);
}
protected Child(Child a, Child b) : base(a,b)
{
// anything you need to do for adding children on top of the parent code.
}
}
I'm new in .net and C# and I'm trying to create an instance of MyStruct, without known the Type before.
so my class receive 3 type in the constructor and I need to create an Instance of MyStruct with this type.
I looked on internet and saw the last part but I can't compile this.
namespace IQUnionTag
{
public class IQUnionTag
{
private struct MyStruct<A, B, C>
{
public A value1;
public B value2;
public C value3;
}
private object MyStructure;
private Type a;
private Type b;
private Type c;
public IQUnionTag(Type a, Type b, Type c)
{
this.a = a;
this.b = b;
this.c = c;
int d = 2;
var d1 = typeof (MyStruct<>); // Doesn't compile
Type[] typeArgs = { a, b, c };
var makeme = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(makeme);
Console.WriteLine(o);
}
}
}
I just want something like
Mystructure = new MyStruct<a,b,c> // this doesn't compile too
typeof(MyStruct<>) make error compile like
Erreur Using the generic type 'IQUnionTag.IQUnionTag.MyStruct<A,B,C>' requires 3 type arguments
i certainly missed something, can you help me to create my instance?
It is not clear what is your purpose but you can do:
public class IQUnionTag
{
private struct MyStruct<A, B, C>
{
public A value1;
public B value2;
public C value3;
}
private object MyStructure;
private Type a;
private Type b;
private Type c;
public IQUnionTag(Type a, Type b, Type c)
{
this.a = a;
this.b = b;
this.c = c;
int d = 2;
var d1 = typeof(MyStruct<,,>); // this is the way to get type of MyStruct
Type[] typeArgs = { a, b, c };
var makeme = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(makeme);
Console.WriteLine(o);
}
}
how can i transform a method (that performs a+b and returns the result) from add(a,b) to a.add(b)?
i read this somewhere and i can't remember what is the technique called...
does it depends on the language?
is this possible in javascript?
In .NET it is called extension methods.
public static NumberExtensions
{
public static int Add(this int a, int b)
{
return a + b;
}
}
UPDATE:
In javascript you could do this:
Number.prototype.add = function(b) {
return this + b;
};
var a = 1;
var b = 2;
var c = a.add(b);
On c# it is named extensions methods:
public static class IntExt
{
public static int Add(this int a, int b)
{
return a + b;
}
}
...
int c = a.Add(b);
say for example you want to do this on integers in C#. You need to define extension methods like this:
public static class IntExtMethods
{
public static int add(this int a, int b)
{
return a+b;
}
}
In C# you can use an Extension Method. In C++, you need to create a member which belongs to the A class which performs the add for you. C does not have objects, so what you're looking for is not possible in C.
If you want to create your own JavaScript class:
function Num(v) {
this.val = v;
}
Num.prototype = {
add: function (n) {
return new Num(this.val + n.val);
}
};
var a = new Num(1);
var b = new Num(2);
var c = a.add(b); // returns new Num(3);
Taking your question literally, I assume you mean transforming this
var add = function(a, b) {
return a + b;
}
to this:
a.add = function(b) {
return this + b;
}
This however only adds that method to a, not to any other object with the same constructor. See Darin Dimitrov's answer for an example of that. Extending the native Number constructor's prototype is not something many would recommend though...
I have a class with 2 constructors:
public class Lens
{
public Lens(string parameter1)
{
//blabla
}
public Lens(string parameter1, string parameter2)
{
// want to call constructor with 1 param here..
}
}
I want to call the first constructor from the 2nd one. Is this possible in C#?
Append :this(required params) at the end of the constructor to do 'constructor chaining'
public Test( bool a, int b, string c )
: this( a, b )
{
this.m_C = c;
}
public Test( bool a, int b, float d )
: this( a, b )
{
this.m_D = d;
}
private Test( bool a, int b )
{
this.m_A = a;
this.m_B = b;
}
Source Courtesy of csharp411.com
Yes, you'd use the following
public class Lens
{
public Lens(string parameter1)
{
//blabla
}
public Lens(string parameter1, string parameter2) : this(parameter1)
{
}
}
The order of constructor evaluation must also be taken into consideration when chaining constructors:
To borrow from Gishu's answer, a bit (to keep code somewhat similar):
public Test(bool a, int b, string c)
: this(a, b)
{
this.C = c;
}
private Test(bool a, int b)
{
this.A = a;
this.B = b;
}
If we change the evalution performed in the private constructor, slightly, we will see why constructor ordering is important:
private Test(bool a, int b)
{
// ... remember that this is called by the public constructor
// with `this(...`
if (hasValue(this.C))
{
// ...
}
this.A = a;
this.B = b;
}
Above, I have added a bogus function call that determines whether property C has a value. At first glance, it might seem that C would have a value -- it is set in the calling constructor; however, it is important to remember that constructors are functions.
this(a, b) is called - and must "return" - before the public constructor's body is performed. Stated differently, the last constructor called is the first constructor evaluated. In this case, private is evaluated before public (just to use the visibility as the identifier).