Questions tagged [.net]

Do NOT use for questions about .NET Core - use [.net-core] instead. The .NET framework is a software framework designed mainly for the Microsoft Windows operating system. It includes an implementation of the Base Class Library, Common Language Runtime (commonly referred to as CLR), Common Type System (commonly referred to as CTS) and Dynamic Language Runtime. It supports many programming languages, including C#, VB.NET, F# and C++/CLI.

Filter by
Sorted by
Tagged with
1871 votes
27 answers
2.1m views

What is a NullReferenceException, and how do I fix it?

I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do to fix this error?
225 votes
5 answers
308k views

What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?

I have some code and when it executes, it throws a IndexOutOfRangeException, saying, Index was outside the bounds of the array. What does this mean, and what can I do about it? Depending on ...
1565 votes
47 answers
807k views

How do I update the GUI from another thread?

Which is the simplest way to update a Label from another Thread? I have a Form running on thread1, and from that I'm starting another thread (thread2). While thread2 is processing some files I would ...
  • 18.1k
1269 votes
8 answers
342k views

How do I use reflection to call a generic method?

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the ...
  • 43.4k
1654 votes
22 answers
283k views

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when ...
  • 48.6k
2583 votes
58 answers
977k views

Deep cloning objects

I want to do something like: MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); And then make changes to the new object that are not reflected in the ...
  • 48.4k
1163 votes
31 answers
1.1m views

Deserialize JSON into C# dynamic object?

Is there a way to deserialize JSON content into a C# dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer.
  • 16.1k
951 votes
26 answers
650k views

Sending email in .NET through Gmail

Instead of relying on my host to send an email, I was thinking of sending the email messages using my Gmail account. The emails are personalized emails to the bands I play on my show. Is it possible ...
  • 20.8k
2128 votes
47 answers
1.3m views

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
969 votes
12 answers
590k views

How do I force my .NET application to run as administrator?

Once my program is installed on a client machine, how do I force my program to run as an administrator on Windows 7?
  • 59.6k
965 votes
20 answers
682k views

Is there a way to check if a file is in use?

I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to ...
  • 10.2k
66 votes
15 answers
521k views

What is the best way to parse html in C#? [closed]

I'm looking for a library/method to parse an html file with more html specific features than generic xml parsing libraries.
  • 18.5k
1374 votes
26 answers
1.2m views

How and when to use ‘async’ and ‘await’

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perform long duration logic? I'...
  • 32.1k
745 votes
29 answers
808k views

Encrypt and decrypt a string in C#? [closed]

How can I encrypt and decrypt a string in C#?
  • 31.5k
2390 votes
18 answers
1.2m views

Difference between decimal, float and double in .NET?

What is the difference between decimal, float and double in .NET? When would someone use one of these?
user avatar
1436 votes
16 answers
2.3m views

Send HTTP POST request in .NET

How can I make an HTTP POST request and send data in the body?
  • 28.4k
1857 votes
20 answers
407k views

Proper use of the IDisposable interface

I know from reading Microsoft documentation that the "primary" use of the IDisposable interface is to clean up unmanaged resources. To me, "unmanaged" means things like database ...
  • 25.9k
683 votes
10 answers
619k views

How do you do a deep copy of an object in .NET? [duplicate]

I want a true deep copy. In Java, this was easy, but how do you do it in C#?
  • 10.7k
21 votes
2 answers
34k views

MSI vs nuget packages: which are is better for continuous delivery?

Let's discuss following topic. There is application which currently is being deployed with good to know xcopy method.This approach makes difficult to manage dependencies, file updates etc. There is ...
373 votes
30 answers
333k views

What Are Some Good .NET Profilers?

What profilers have you used when working with .net programs, and which would you particularly recommend?
112 votes
16 answers
99k views

What is the difference between a reference type and value type in c#?

Some guy asked me this question couple of months ago and I couldn't explain it in detail. What is the difference between a reference type and a value type in C#? I know that value types are int, bool,...
  • 57.1k
912 votes
14 answers
542k views

How to enable assembly bind failure logging (Fusion) in .NET

How do I enable assembly bind failure logging (Fusion) in .NET?
  • 10.2k
196 votes
2 answers
62k views

Understanding garbage collection in .NET

Consider the below code: public class Class1 { public static int c; ~Class1() { c++; } } public class Class2 { public static void Main() { { var c1=...
744 votes
35 answers
372k views

Implementing INotifyPropertyChanged - does a better way exist?

Microsoft should have implemented something snappy for INotifyPropertyChanged, like in the automatic properties, just specify {get; set; notify;} I think it makes a lot of sense to do it. Or are there ...
319 votes
21 answers
461k views

Upload files with HTTPWebrequest (multipart/form-data)

Is there any class, library or some piece of code which will help me to upload files with HTTPWebrequest? Edit 2: I do not want to upload to a WebDAV folder or something like that. I want to ...
  • 26.8k
1678 votes
27 answers
241k views

Why not inherit from List<T>?

When planning out my programs, I often start with a chain of thought like so: A football team is just a list of football players. Therefore, I should represent it with: var football_team = new ...
  • 24.9k
7448 votes
66 answers
1.3m views

What is the difference between String and string in C#?

What are the differences between these two and which one should I use? string s = "Hello world!"; String s = "Hello world!";
344 votes
9 answers
64k views

Why use async and return await, when you can return Task<T> directly?

Is there any scenario where writing method like this: public async Task<SomeResult> DoSomethingAsync() { // Some synchronous code might or might not be here... // return await ...
  • 4,846
2202 votes
74 answers
788k views

How do I calculate someone's age based on a DateTime type birthday?

Given a DateTime representing a person's birthday, how do I calculate their age in years?
732 votes
39 answers
253k views

What is the correct way to create a single-instance WPF application?

Using C# and WPF under .NET (rather than Windows Forms or console), what is the correct way to create an application that can only be run as a single instance? I know it has something to do with some ...
  • 12.4k
838 votes
23 answers
778k views

How to read embedded resource text file

How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text ...
  • 8,431
28 votes
2 answers
17k views

Why does WPF support binding to properties of an object, but not fields?

I've got a WCF service that passes around status updates via a struct like so: [DataContract] public struct StatusInfo { [DataMember] public int Total; [DataMember] public string Authority; } ...
750 votes
16 answers
1.3m views

Run Command Prompt Commands

Is there any way to run command prompt commands from within a C# application? If so how would I do the following: copy /b Image1.jpg + Archive.rar Image2.jpg This basically embeds an RAR file ...
  • 16.2k
533 votes
22 answers
290k views

How do I remove diacritics (accents) from a string in .NET?

I'm trying to convert some strings that are in French Canadian and basically, I'd like to be able to take out the French accent marks in the letters while keeping the letter. (E.g. convert é to e, so ...
  • 6,389
798 votes
31 answers
1.9m views

How can I convert String to Int?

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database. How can I do this?
  • 8,049
243 votes
22 answers
413k views

MVVM: Tutorial from start to finish?

I'm a C#/Windows Forms programmer with more than 5 years experience. I've been investigating WPF using the MVVM (Model-View-ViewModel) design pattern. I have searched the Internet for tutorials. I ...
3 votes
5 answers
12k views

Windows service NOT shown in add remove programs under control panel

I installed my windows service via (name of the service: Testing Service) InstallUtil.exe present in .NET Framework (:\Windows\Microsoft.NET\FrameWork64\v4) in a windows 7 computer. I can see my ...
  • 8,665
53 votes
3 answers
47k views

What do Option Strict and Option Explicit do?

I saw this post: Typos… Just use option strict and explicit please.. during one software development project, which I was on as a consultant, they were getting ridiculous amounts of errors ...
  • 7,924
667 votes
41 answers
1.4m views

How do I make a textbox that only accepts numbers?

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I've done this kind of validation by overloading the KeyPress event and just removing ...
  • 13k
275 votes
4 answers
144k views

Entity Framework and Connection Pooling

I've recently started to use the Entity Framework 4.0 in my .NET 4.0 application and am curious about a few things relating to pooling. Connection pooling as I know is managed by the ADO.NET data ...
  • 143k
72 votes
7 answers
37k views

What's wrong with using Thread.Abort()

So I know that you shouldn't use Thread.Abort() But I've never been given a good explanation. Is there a performance penalty or some hidden gotcha? I know you can't ignore/swallow the ...
13 votes
2 answers
19k views

Why saving changes to a database fails?

I have following C# code in a console application. Whenever I debug the application and run the query1 (which inserts a new value into the database) and then run query2 (which displays all the ...
  • 2,483
693 votes
20 answers
408k views

C# difference between == and Equals()

I have a condition in a silverlight application that compares 2 strings, for some reason when I use == it returns false while .Equals() returns true. Here is the code: if (((ListBoxItem)lstBaseMenu....
  • 12.2k
712 votes
18 answers
487k views

Embedding DLLs in a compiled executable

Is it possible to embed a pre-existing DLL into a compiled C# executable (so that you only have one file to distribute)? If it is possible, how would one go about doing it? Normally, I'm cool with ...
  • 8,766
151 votes
7 answers
302k views

How do you do Impersonation in .NET?

Is there a simple out of the box way to impersonate a user in .NET? So far I've been using this class from code project for all my impersonation requirements. Is there a better way to do it by using ...
  • 14.4k
554 votes
20 answers
292k views

Can't specify the 'async' modifier on the 'Main' method of a console app

I am new to asynchronous programming with the async modifier. I am trying to figure out how to make sure that my Main method of a console application actually runs asynchronously. class Program { ...
  • 9,027
232 votes
32 answers
522k views

Reading Excel files from C#

Is there a free or open source library to read Excel files (.xls) directly from a C# program? It does not need to be too fancy, just to select a worksheet and read the data as strings. So far, I've ...
  • 12.5k
49 votes
21 answers
279k views

BadImageFormatException. This will occur when running in 64 bit mode with the 32 bit Oracle client components installed

I am getting this error while on of my .Net application are trying to make a connection to oracle database. The error says that This problem will occur when running in 64 bit mode with the 32 bit ...
  • 14.3k
514 votes
19 answers
338k views

Asynchronously wait for Task<T> to complete with timeout

I want to wait for a Task<T> to complete with some special rules: If it hasn't completed after X milliseconds, I want to display a message to the user. And if it hasn't completed after ...
  • 211k
680 votes
13 answers
166k views

C# Interfaces. Implicit implementation versus Explicit implementation

What are the differences in implementing interfaces implicitly and explicitly in C#? When should you use implicit and when should you use explicit? Are there any pros and/or cons to one or the other?...
  • 26.1k

1
2 3 4 5
982