C # Create object and change value

I am having a problem creating an object and setting / getting values. The purpose of this is to return data from the model to the controller.

Disclaimer: Im new to C #

Can anyone show me the correct way to do this?

I tried these ways, but I keep getting the error:

object o = new { test = "cat" };
o.test = "dog";

Object o = new { test = "cat" };
o.test = "dog";

object o = new Object();
o.test = "dog";

// I also tried
object o = new Object();
o["test"] = "dog";

      

+3


source to share


8 answers


JREAM, your basic premise and understanding of C # objects is probably a little flawed, which is the reason for your confusion.

"In C #'s unified type system, all types, predefined and user-defined reference types, and value types inherit directly or indirectly from Object. You can assign values ​​of any type to variables of the type object." http://msdn.microsoft.com/en-us/library/9kkx3h3c%28v=vs.110%29.aspx

In this case, it is preferable to use a specific type, if possible, rather than an object. In your case, your objects should really be classes, which then in turn create reference types that you can use.



public class O
{
    public string test { get; set; }
}

var newO = new O() { test = "cat" };
newO = "dog";

      

Here we are creating a new class "O". We have one property inside this class. Then we can instantiate the class and access the properties inside it. After it is created, we can access the property as much as we want and reassign new values ​​to it. Hope it helps.

+3


source


It looks like you are trying to create an anonymous type . Anonymous types are great because they provide a quick and convenient way to create an object without having to define a type.

Try the following:

var o = new { test = "cat" };

      



At this point, you will be able to access the properties of the anonymous type, for example:

o.test = "dog";
MessageBox.Show(o.test);  //shows "dog"

      

+2


source


If you are looking for Key,Value pair

, you can easily use Dictionary

;

using System.Collections.Generic;

Dictionary<string, string> kv = new Dictionary<string, string>() {
 {"Key1","Value1"}, {"Key2","Value2"}, {"Key3","Value3"}
};

      

And get like:

string Val1 = kv["Key1"];

      

And add key, values ​​like;

kv.Add("Key4","Value4");

      

+2


source


You need to instantiate a class in C #. Creating objects from blue (as in JavaScript) is not possible in C #.

For example, the class would look like this:

public class MyClass {
  public string test { get; set; }
}

MyClass o = new MyClass { test = "cat" };

      

Update: As of .NET 3.5, you can indeed create objects like this:

var o = new { test = "cat" };
Console.WriteLine(o.test);

      

However, after creating them, you cannot add or remove properties.

+1


source


An object

does not have these properties or fields. To access these fields, make a variable dynamic

.

dynamic o = new { test = "cat" };
Console.WriteLine(o.test );

      

Oh btw o["test"]

doesn't work. o

is not an array-associated JavaScript or C # dictionary. This is an anonymous object.

+1


source


Typically, you create a class and initialize an instance of that class; but everything works anyway.

Beware that even if test

not declared, it will still be created; but at runtime you will have an exception.

dynamic temp = ((dynamic)o).test;

      

+1


source


new { test = "cat" };

      

You are trying to create an anonymous object with a property test

that doesn't seem to be what you want. If you want to initialize custom properties use the following syntax

var customer = new Customer {Name = "Ilya"};

      

which will be translated by the compiler to

var customer = new Customer();
customer.Name = "Ilya";

      

Please note that you must define your own class like

public class Customer 
{
    public string Name { get; set; }
}

      

+1


source


If you want arbitrary string-to-value matching you should use Dictionary

.

Dictionary<string, string> lookup = new Dictionary<string, string>();
lookup.Add("test", "dog");
//add other pairs.

string value = lookup["test"]; //value will be "dog"

      

The code new { test = "cat" };

instantiates a new anonymous type with one property ( test

) and a given value. Anonymous types in C # are immutable, so you won't be able to set this property to anything else after you create it.

The reason why you can't access it properly is because you are storing it in object

. Since it object

does not have any properties, the compiler "loses" the knowledge that the object has this property. You can use var

to ensure that a variable is of the appropriate type (anonymous type) that will allow you to use a property:

var obj = new { test = "cat" };
string value = obj.test;

      

+1


source







All Articles