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
Filter by
Sorted by
Tagged with
730 votes
10 answers
428k views

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....
Jan's user avatar
  • 9,968
647 votes
10 answers
164k views

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<string, object>, or ...
Reed Copsey's user avatar
442 votes
8 answers
272k views

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 ...
user avatar
416 votes
7 answers
146k views

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 ...
theburningmonk's user avatar
369 votes
6 answers
353k views

.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":"...
RasikaSam's user avatar
  • 5,473
337 votes
20 answers
263k views

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<int, int, int int> GetOrderRelatedIds() That returns the ids for OrderGroupId, ...
Vaccano's user avatar
  • 80.2k
294 votes
5 answers
164k views

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....
Craig's user avatar
  • 36.5k
281 votes
4 answers
148k views

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<Item>(...
stackoverflowuser's user avatar
278 votes
10 answers
185k views

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's user avatar
  • 2,923
251 votes
11 answers
132k views

C# 4.0 optional out/ref arguments

Does C# 4.0 allow optional out or ref arguments?
Joe Daley's user avatar
  • 45.9k
226 votes
12 answers
206k views

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 ...
Randall Flagg's user avatar
216 votes
8 answers
223k views

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 ...
Hanshan's user avatar
  • 3,684
207 votes
5 answers
104k views

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: ...
Ian Ringrose's user avatar
  • 51.5k
204 votes
3 answers
99k views

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 ...
Alex K's user avatar
  • 10.9k
199 votes
11 answers
82k views

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[] ...
Razor's user avatar
  • 17.4k
177 votes
7 answers
155k views

How to get first record in each group using Linq

Considering the following records: Id F1 F2 F3 ------------------------------------------------- 1 Nima 1990 10 2 ...
DooDoo's user avatar
  • 13.1k
171 votes
2 answers
53k views

What is Microsoft.csharp.dll in .NET 4.0

This DLL is added by default in Visual Studio 2010 projects. What is this new assembly used for? It does not seem to contain much after looking at it using Reflector and Google does not seem to have ...
anon's user avatar
  • 1,713
170 votes
6 answers
36k views

Difference between Covariance & Contra-variance

I am having trouble understanding the difference between covariance and contravariance.
jane doe's user avatar
  • 1,743
169 votes
14 answers
172k views

How do I check if a property exists on a dynamic anonymous type in c#?

I have an anonymous type object that I receive as a dynamic from a method I would like to check in a property exists on that object. .... var settings = new { Filename="temp.txt", ...
David MZ's user avatar
  • 3,688
169 votes
4 answers
17k views

How does the C# compiler detect COM types?

EDIT: I've written the results up as a blog post. The C# compiler treats COM types somewhat magically. For instance, this statement looks normal... Word.Application app = new Word.Application(); ......
Jon Skeet's user avatar
  • 1.5m
168 votes
7 answers
236k views

How to encode the plus (+) symbol in a URL

The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) signs in the email body with blank space. It looks like it only happens with the + ...
user523234's user avatar
  • 14.6k
168 votes
4 answers
12k views

Why covariance and contravariance do not support value type

IEnumerable<T> is co-variant but it does not support value type, just only reference type. The below simple code is compiled successfully: IEnumerable<string> strList = new List<string&...
cuongle's user avatar
  • 74.6k
166 votes
7 answers
113k views

How do I get the current line number?

Here is an example of what I want to do: MessageBox.Show("Error line number " + CurrentLineNumber); In the code above the CurrentLineNumber, should be the line number in the source code of this ...
Furkan Gözükara's user avatar
162 votes
8 answers
391k views

Redirect to Action in another controller

I have two controllers, both called AccountController. One of them, lets call it Controller A, is in an Area called Admin and the other, lets call it Controller B, is not in any Area (I guess that ...
Sachin Kainth's user avatar
161 votes
18 answers
385k views

Best way to compare two complex objects

I have two complex objects like Object1 and Object2. They have around 5 levels of child objects. I need the fastest method to say if they are same or not. How could this be done in C# 4.0?
NoWar's user avatar
  • 37k
156 votes
4 answers
238k views

Check if list<t> contains any of another list

I have a list of parameters like this: public class parameter { public string name {get; set;} public string paramtype {get; set;} public string source {get; set;} } IEnumerable<...
gdp's user avatar
  • 8,122
153 votes
2 answers
227k views

Moq, SetupGet, Mocking a property

I'm trying to mock a class, called UserInputEntity, which contains a property called ColumnNames: (it does contain other properties, I've just simplified it for the question) namespace CsvImporter....
Hans Rudel's user avatar
  • 3,523
151 votes
10 answers
71k views

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

Both of these generate an error saying they must be a compile-time constant: void Foo(TimeSpan span = TimeSpan.FromSeconds(2.0)) void Foo(TimeSpan span = new TimeSpan(2000)) First of all, can ...
Mike Pateras's user avatar
  • 14.9k
146 votes
8 answers
96k views

No Main() in WPF?

I am a beginner when it comes to programming but I was sure that one of the universal rules was that a program starts with Main(). I do not see one when I create a WPF project. Is Main() simply named ...
Juice's user avatar
  • 2,860
144 votes
9 answers
124k views

How to remove a single, specific object from a ConcurrentBag<>?

With the new ConcurrentBag<T> in .NET 4, how do you remove a certain, specific object from it when only TryTake() and TryPeek() are available? I'm thinking of using TryTake() and then just ...
Brandon's user avatar
  • 14.1k
139 votes
1 answer
112k views

Start may not be called on a promise-style task. exception is coming

I am creating a simple wpf desktop application. UI have just a button and code in .cs file like. private void Button_Click_2(object sender, RoutedEventArgs e) { FunctionA(); } public void ...
D J's user avatar
  • 6,988
138 votes
8 answers
139k views

How to change the output name of an executable built by Visual Studio

I want to change name of executable file. Like suppose my project name is "SampleDemo" It will create executable file Like 'SampleDemo.exe' but I want to rename it to 'Demo.exe'
Kiran Desai's user avatar
  • 1,811
135 votes
12 answers
392k views

How to add and get Header values in WebApi

I need to create a POST method in WebApi so I can send data from application to WebApi method. I'm not able to get header value. Here I have added header values in the application: using (var client = ...
user avatar
132 votes
6 answers
373k views

Convert from List into IEnumerable format

IEnumerable<Book> _Book_IE List<Book> _Book_List How shall I do in order to convert _Book_List into IEnumerable format?
HelloWorld1's user avatar
  • 13.9k
132 votes
10 answers
107k views

C# operator overload for `+=`?

I am trying to do operator overloads for +=, but I can't. I can only make an operator overload for +. How come? Edit The reason this is not working is that I have a Vector class (with an X and Y ...
Mathias Lykkegaard Lorenzen's user avatar
131 votes
5 answers
127k views

What and When to use Tuple? [duplicate]

May someone please explain what a Tuple is and how to use it in a Real World Scenario. I would like to find out how this can enrich my coding experience?
Donald N. Mafa's user avatar
128 votes
11 answers
85k views

method overloading vs optional parameter in C# 4.0 [duplicate]

which one is better? at a glance optional parameter seems better (less code, less XML documentation, etc), but why do most MSDN library classes use overloading instead of optional parameters? Is ...
Louis Rhys's user avatar
  • 35.1k
127 votes
6 answers
142k views

Redirect from asp.net web api post action

I'm very new to ASP.NET 4.0 Web API. Can we redirect to another URL at the end of the POST action?, something like ... Response.Redirect(url) Actually I upload file from a MVC application (say www....
Shahdat's user avatar
  • 5,433
127 votes
6 answers
32k views

_=> what does this underscore mean in Lambda expressions?

What does an lambda expression like _=> expr mean? What is the purpose of _ as input to lambda? Example: int count = 0; list.ForEach(_ => count += 1);
Prasad's user avatar
  • 1,357
125 votes
5 answers
117k views

Why does 0.ToString("#.##") return an empty string instead of 0.00 or at least 0?

Why does 0.ToString("#.##") return an empty string? Shouldn't it be 0.00 or at least 0?
Imran Qadir Baksh - Baloch's user avatar
125 votes
7 answers
158k views

how to check if string value is in the Enum list?

In my query string, I have an age variable ?age=New_Born. Is there a way I can check if this string value New_Born is in my Enum list [Flags] public enum Age { New_Born = 1, Toddler = 2, ...
qinking126's user avatar
  • 11.6k
124 votes
3 answers
187k views

Only Add Unique Item To List

I'm adding remote devices to a list as they announce themselves across the network. I only want to add the device to the list if it hasn't previously been added. The announcements are coming across ...
Oli's user avatar
  • 3,076
124 votes
8 answers
27k views

To Workflow or Not to Workflow? [closed]

I am responsible for a team of developers who will are about to start development of a light weight insurance claims system. The system involves a lot of manual tasks and business workflows and we are ...
Kane's user avatar
  • 16.6k
109 votes
16 answers
74k views

Getting all messages from InnerException(s)?

Is there any way to write a LINQ style "short hand" code for walking to all levels of InnerException(s) of Exception thrown? I would prefer to write it in place instead of calling an extension ...
Jimmy's user avatar
  • 3,254
109 votes
2 answers
37k views

How is Generic Covariance & Contra-variance Implemented in C# 4.0?

I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, List<string> can be assigned to List<object>. How could ...
Morgan Cheng's user avatar
  • 74.9k
103 votes
3 answers
29k views

Extension method and dynamic object

I am going to summarize my problem into the following code snippet. List<int> list = new List<int>() { 5, 56, 2, 4, 63, 2 }; Console.WriteLine(list.First()); Above code is working fine. ...
santosh singh's user avatar
102 votes
15 answers
72k views

Ignore milliseconds when comparing two datetimes

I am comparing the LastWriteTime of two files, however it is always failing because the file I downloaded off the net always has milliseconds set at 0, and my original file has an actual value. Is ...
Eros Nikolli's user avatar
  • 1,061
101 votes
4 answers
154k views

Convert int (number) to string with leading zeros? (4 digits) [duplicate]

Possible Duplicate: Number formatting: how to convert 1 to "01", 2 to "02", etc.? How can I convert int to string using the following scheme? 1 converts to 0001 123 converts to 0123 Of course, ...
Ehsan's user avatar
  • 3,461
101 votes
8 answers
121k views

How to get .exe file version number from file path

I am using .Net 3.5/4.0 with code in C#. I am trying to get a version number of an exe file on my C: drive. For example path is: c:\Program\demo.exe. If the version number of demo.exe is 1.0. How ...
NoviceMe's user avatar
  • 3,166
101 votes
6 answers
42k views

Field vs Property. Optimisation of performance

Please note this question related to performance only. Lets skip design guidelines, philosophy, compatibility, portability and anything what is not related to pure performance. Thank you. Now to the ...
Boppity Bop's user avatar
  • 9,985

1
2 3 4 5
517