Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Just learning, have a question about objects

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
I am learning c# coming from a vbscript, Perl, PHP and flex background. I have a questions about objects.

In PHP if I want to create an object I would do this:
Code:
$t = new stdClass;
$t->name = "Tom";
In Flex I would do this
Code:
var t:Object = new Object();
t.name = "Tom";
Does c# have anything like this for creating objects?

Thanks,
timgerr


-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
Yes, in fact for reference types, you need to use <new> keyword to create instances.

class Foo { public int ID; }

Foo f = new Foo();
f.ID = 0;

 
C# is strongly typed.

That just means that you must specify the type of object when you declare it.

so:

<type> <varname> = new <type>
Person person = new Person();
 
Actually in framework 3.5 you can use var and also named parameters - but I won't go into it as you're learning it may confuse things. But if you want more info on these just ask.

 
var" is syntax sugar in 3.0 to reduce ceremony.

var p = new Person();
instead of
Person p = new Person();

using property initializers you can also do this
var p = new Person
{
firstname ="John",
lastname = "Doe"
};


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
ah I get it, so isit just a "shorthanded" way of writing it the old way of Person p = new Person(); ?
var p = new Person {
firstname ="John",
lastname = "Doe"
};
Is that the same as Person p = new Person('john', 'doe'); ?

Is there any benificial way of doing it this way of using var?

Cheers for your time :)

Regards,

Martin

Computing Design And Services:
 
no new Person("a", "b"); is different than new Person { firstname ="John", lastname = "Doe" };

the first uses ctor injection
class Person
{
public Person(string firstname, string lastname)
{
...
}
}
while the second uses setter injection
class Person
{
public string firstname {get; set; }
public string lastname {get; set; }
}

while you could pass null values for ctor injection, it's not recommended. the idea is that ctor dependencies are required, the object should error if a dependency is null.
setter dependencies are optional and usually have a defined default implementation.

ctor injection is how you would create immutable objects (objects that do not change state). System.DateTime is a classic example of an immutable object and ctor dependency injection. when working with datetime you can never change the value of the current object, it always returns a new datetime object.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top