Questions tagged [c#-4.0]

C# 4.0 introduced features like dynamic objects, COM interoperability improvements, generic co- and contra-variance, optional and named parameters. Use this tag if your question specifically pertains to C# 4.0 specific features. The C# tag should be used if your question is not specific to C# 4.0 features.

C# 4.0 is the fourth version of the programming language. It introduced features like dynamic objects, COM interoperability improvements, and optional and named parameters.

Resources

25854 questions
724
votes
10 answers

Interop type cannot be embedded

I am creating a web application on the .NET 4.0 framework (beta2) in C#. When I try to use a assembly called "ActiveHomeScriptLib", I get the following error: Interop type 'ActiveHomeScriptLib.ActiveHomeClass' cannot be embedded. Use the…
Jan
  • 9,858
  • 7
  • 26
  • 33
640
votes
10 answers

What are the true benefits of ExpandoObject?

The ExpandoObject class being added to .NET 4 allows you to arbitrarily set properties onto an object at runtime. Are there any advantages to this over using a Dictionary, or really even a Hashtable? As far as I can tell, this is…
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
435
votes
8 answers

What is the difference between task and thread?

In C# 4.0, we have Task in the System.Threading.Tasks namespace. What is the true difference between Thread and Task. I did some sample program(help taken from MSDN) for my own sake of learning with Parallel.Invoke Parallel.For Parallel.ForEach…
user372724
414
votes
7 answers

Why are C# 4 optional parameters defined on interface not enforced on implementing class?

I noticed that with the optional parameters in C# 4 if you specify an optional parameter on an interface you don't have to make that parameter optional on any implementing class: public interface MyInterface { void TestMethod(bool flag =…
theburningmonk
  • 15,701
  • 14
  • 61
  • 104
363
votes
6 answers

.NET NewtonSoft JSON deserialize map to a different property name

I have following JSON string which is received from an external party. { "team":[ { "v1":"", "attributes":{ "eighty_min_score":"", "home_or_away":"home", "score":"22", …
RasikaSam
  • 5,363
  • 6
  • 28
  • 36
319
votes
19 answers

Better naming in Tuple classes than "Item1", "Item2"

Is there a way to use a Tuple class, but supply the names of the items in it? For example: public Tuple GetOrderRelatedIds() That returns the ids for OrderGroupId, OrderTypeId, OrderSubTypeId and OrderRequirementId. It would be…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
294
votes
5 answers

Dynamically adding properties to an ExpandoObject

I would like to dynamically add properties to a ExpandoObject at runtime. So for example to add a string property call NewProp I would like to write something like var x = new ExpandoObject(); x.AddProperty("NewProp", System.String); Is this easily…
Craig
  • 36,306
  • 34
  • 114
  • 197
280
votes
4 answers

Parallel.ForEach vs Task.Factory.StartNew

What is the difference between the below code snippets? Won't both be using threadpool threads? For instance if I want to call a function for each item in a collection, Parallel.ForEach(items, item => DoSomething(item)); vs foreach(var item…
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
276
votes
10 answers

What is the 'dynamic' type in C# 4.0 used for?

C# 4.0 introduced a new type called 'dynamic'. It all sounds good, but what would a programmer use it for? Is there a situation where it can save the day?
Fahad
  • 2,903
  • 3
  • 18
  • 13
247
votes
11 answers

C# 4.0 optional out/ref arguments

Does C# 4.0 allow optional out or ref arguments?
Joe Daley
  • 45,356
  • 15
  • 65
  • 64
224
votes
12 answers

HttpListener Access Denied

I am writing an HTTP server in C#. When I try to execute the function HttpListener.Start() I get an HttpListenerException saying "Access Denied". When I run the app in admin mode in windows 7 it works fine. Can I make it run without admin mode?…
Randall Flagg
  • 4,834
  • 9
  • 33
  • 45
214
votes
8 answers

C# Create New T()

You can see what I'm trying (but failing) to do with the following code: protected T GetObject() { return new T(); } Any help would be greatly appreciated. EDIT: The context was as follows. I was playing around with a custom controller class…
Hanshan
  • 3,656
  • 5
  • 29
  • 36
204
votes
5 answers

How can I default a parameter to Guid.Empty in C#?

I wish to say: public void Problem(Guid optional = Guid.Empty) { } But the compiler complains that Guid.Empty is not a compile time constant. As I don’t wish to change the API I can’t use: Nullable
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
203
votes
3 answers

Is HttpClient safe to use concurrently?

In all the examples I can find of usages of HttpClient, it is used for one off calls. But what if I have a persistent client situation, where several requests can be made concurrently? Basically, is it safe to call client.PostAsync on 2 threads at…
Alex K
  • 10,835
  • 8
  • 29
  • 34
198
votes
10 answers

Covariance and contravariance real world example

I'm having a little trouble understanding how I would use covariance and contravariance in the real world. So far, the only examples I've seen have been the same old array example. object[] objectArray = new string[] { "string 1", "string 2" }; It…
Razor
  • 17,271
  • 25
  • 91
  • 138
1
2 3
99 100