You searched for how does c# work. We found 81 results in 0.272 seconds.

limit or audit the use of an interface

I have 3 assemblies: A defines an interface B references A and uses that interface C also references A how can I make sure C does not use that interface? splitting...
Jon Skeet
people
quotationmark

You could make the interface internal, and use InternalsVisibleToAttribute to allow B access to the internal members of A (by adding the attribute to A). It's very coarse-grained though - you can't do it for individual members; it's all or... more

people

.NET target framework compatibility and the compiler on .NET 4.5 or higher

I'm fairly experienced in .NET development but today I was forced to wrap my head around something I'd never thought about before: How do the installed .NET Framework, the .NET...
Jon Skeet
people
quotationmark

How does the compiler know that I can't use SystemDefault on .NET 4.5 but can use it on 4.7? Is this done via some kind of API file known to the compiler? Yes, I'd expect it to be done via the reference assemblies. A reference... more

people

"Cannot convert lambda expression to type 'string' because it is not a delegate type" querying dataset in C#

I have this code, which compiles fine in VB.NET: Imports System Imports System.Data Imports System.Data.Entity Imports System.Data.SqlClient Imports System.Linq Imports...
Jon Skeet
people
quotationmark

It looks like VB has built-in support for DataTable. Here's a short but complete example: Option Strict On Imports System Imports System.Data Imports System.Linq Public Class LinqTest Shared Sub Main() Dim table as... more

people

Custom Contains for List<ReferenceObject> c#

I'm trying to use List.Contains in a List My objects to compare come from a Service Reference in C# and their Equals method doesn't suit my needs. I've been looking into...
Jon Skeet
people
quotationmark

It sounds like you just want to implement IEqualityComparer<ActivitiesActivity>: public class ActivitiesActivityEqualityComparer : IEqualityComparer<ActivitiesActivity> { public bool Equals(ActivitiesActivity x,... more

people

C# unable to get value from lambda expression

Our company has purchased an app written in .NET and I have got a privilege to support it. I have never worked with .NET therefore I need some guidance with how to use...
Jon Skeet
people
quotationmark

I think you're confused by what Func<T, TResult> is meant to be. The first parameter (T) is the input to the delegate; TResult is the output. So you probably want: Func<Appointment, DateTime> appointmentFunction = x =>... more

people

Can the C# compiler distinguish between I/O bound and computational tasks?

Consider a snippet of code such as this: public async Task<Bitmap> DownloadDataAndRenderImageAsync( CancellationToken cancellationToken) { var imageData = await...
Jon Skeet
people
quotationmark

No. In the example you've given, the compiler will only use TaskCompletionSource<T> (indirectly) for the overall asynchronous operation (DownloadDataAndRenderImageAsync). It's up to the two methods that are called to decide how... more

people

C#: Code Optimization for Date Formatting

Using C#, I am trying to populate a variable with a known filename + date. The catch is that the date must always be the last day of the previous month. It must also have no...
Jon Skeet
people
quotationmark

Sounds like you want something more like: // Warning: you should think about time zones... DateTime today = DateTime.Today; DateTime startOfMonth = new DateTime(today.Year, today.Month, 1); DateTime endOfPreviousMonth =... more

people

How to efficiently ensure a decimal value has at least N decimal places

I want to efficiently ensure a decimal value has at least N (=3 in the example below) places, prior to doing arithmetic operations. Obviouly I could format with...
Jon Skeet
people
quotationmark

If you're nervous that the compiler will optimize out the operator (although I doubt that it would ever do so) you could just call the Add method directly. Note that you don't need to add and then subtract - you can just add 0.000m. So for... more

people

How to figure out dynamically all methods with custom attribute

I have a simple challenge. I dynamically need to figure out all methods with a specific attribute in C#. I'm going to load the assemblies dynamically from another application and...
Jon Skeet
people
quotationmark

The simplest approach is to use MethodInfo.IsDefined - quite possibly with LINQ as well: var testMethods = from assembly in assemblies from type in assembly.GetTypes() from method in type.GetMethods() ... more

people

NullReferenceExeption in C# when the container is initialized

In the code listed below, I get a "NullReferenceExeption" with the error : "Object reference not set to an instance of an object". I am completely new to C#, but guess the error...
Jon Skeet
people
quotationmark

The problem is that the VTEST_FSUB<V> constructor body is executing before the VTEST_RUN constructor body. So when do_virtual is called, localSMT is still null. Then do_virtual tries to call a method on localSMT, hence the... more

people