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
25,835
questions
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....
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 ...
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
...
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 ...
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":"...
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, ...
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....
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>(...
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?
251
votes
11
answers
132k
views
C# 4.0 optional out/ref arguments
Does C# 4.0 allow optional out or ref arguments?
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 ...
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 ...
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:
...
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 ...
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[] ...
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 ...
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 ...
170
votes
6
answers
36k
views
Difference between Covariance & Contra-variance
I am having trouble understanding the difference between covariance and contravariance.
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",
...
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();
......
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 + ...
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&...
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 ...
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 ...
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?
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<...
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....
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 ...
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 ...
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 ...
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 ...
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'
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 = ...
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?
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 ...
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?
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 ...
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....
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);
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?
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,
...
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 ...
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 ...
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 ...
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 ...
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.
...
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 ...
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, ...
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 ...
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 ...