Is there a way to generate a dictionary initializer using the C# CodeDom? Are those supported at all?
I would like to have:
private IDictionary<string, string> map = new Dictionary<string, string>
{
{ "Name", "Value" },
...
};
...
I was too excited when object initializer appeared in C#.
MyClass a = new MyClass();
a.Field1 = Value1;
a.Field2 = Value2;
can be rewritten shorter:
MyClass a = new MyClass { Field1 = Value1, Field2 = Value2 }
Object initializer code is more obvious but when properties number come to dozen and some of the assignment deals with nul...
I am trying to use Object initializers to set the properties of a class and then access them within the constructor of the class. The problem is that the properties do not seem to be set until after the constructor runs. Am I doing something wrong.
Basic Class..
public class TestClass
{
public string FirstName{get; set;}
publ...
Hi All,
I had a quick question. Can I do all of this logic inside the select statement?
var entries = atisDAO.GetPME(xl, null);
response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme => new DetectorDetails {ID = pme.PlaceNum.ToString()}));
if(response.Data.Detectors.Any())
{
response.Data.Detectors.ForEach(d =>{
...
Hi all,
Can I somehow get a reference to the instance I am creating using object initialiser
var x = new TestClass
{
Id = 1,
SomeProperty = SomeMethod(this)
}
"this" should point to the new TestClass instance I'm creating. But it obviously refers the the instance of the class i...
I have used auto properties a lot but I have gone more and more away from that setting up classes with readonly backing fields initialized in the constructor. I remove all setters and only add the back if the property clearly need a setter.
I find this makes my classes more robust and elegant OO wise and I am kicking myself for not doi...
I'm trying to convert some C# code to VB but I’m getting an error. What would be the correct VB syntax?
C#
return new List<string> {"First Name", "Last Name", "First & Last Name", "None"};
VB
Return New List(Of String)() From {"First Name", "Last Name", "First & Last Name", "None"}
And how about would I convert this too?
Dim lis...
I have a LINQ dbml class that I am wrapping in a POCO. I have built overloaded constructors that take the DBML class and init. the wrapper objects properties based on the dbml object passed in.
For example
public class MyPerson{
public MyPerson(DBMLPerson p)
{
this.ID = p.ID;
this.Name = p.Name;
}
}
if I the...
This code compiles successfully, but I think it should fail to compile. Also, when you run it you get a NullReferenceException. The missing code is the "new Bar" in the initialization of the Bar property.
class Bar
{
public string Name { get; set; }
}
class Foo
{
public Bar Bar { get; set; }
}
class Program
{
static void ...
Why isn't it possible to assign events along with properties in object initializers in C#? It seems to be so natural to do so.
var myObject = new MyClass()
{
Property = value,
Event1 = actor,
// or
Event2 += actor
};
Or is there some trick that I don't know of?
...