Blog Home  Home Feed your aggregator (RSS 2.0)  
kevin Mocha - DotNet | CSharp
Bookmarks collected from web.
 
 Wednesday, August 11, 2010
 Monday, July 19, 2010
 Friday, July 02, 2010
 Tuesday, June 22, 2010
 Friday, June 18, 2010

http://www.vbinfozine.com/t_wfdlg.shtml

But what if I want to do some validation WITHIN the button's Click event handler and if the validation fails, I DON'T WANT to close the dialog?

 

First solution: Set OKCmd.DialogResult = DialogResult.None (string "None" in the designer) and close the form manually:

 

Second solution: Leave the OKCmd.DialogResult = DialogResult.OK, but prevent the form to automatically close when the user enters invalid logon information. Here is how:

Friday, June 18, 2010 8:09:52 PM UTC  #    Comments [0]    |  Trackback
 Monday, June 14, 2010

http://stackoverflow.com/questions/135020/advantages-to-using-private-static-methods

After you mark the methods as static, the compiler will emit non-virtual call sites to these members. Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null. This can result in a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.

Static methods are useful, because just by looking at its signature, you know that the calling it doesn't use or modify the current instance's state.

Monday, June 14, 2010 8:12:46 PM UTC  #    Comments [0]    |  Trackback
 Friday, June 04, 2010

http://www.albahari.com/nutshell/predicatebuilder.aspx

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Visitor class

    /// <summary>
    /// A visitor that filters visited items
    /// </summary>
    /// <typeparam name="T">The type of item to be visited</typeparam>
    public class FilterVisitor<T> : IVisitor<T>
    {
        #region Private Members

        /// <summary>
        /// The function to use for filtering items
        /// </summary>
        private readonly Func<T, bool> _filterFunction;

        /// <summary>
        /// The filtered items
        /// </summary>
        private readonly List<T> _result;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="filterFunction">The function to use for filtering items</param>
        public FilterVisitor(Func<T, bool> filterFunction)
        {
            _filterFunction = filterFunction;
            _result = new List<T>();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="expression">The lambda expression to use for filtering items</param>
        public FilterVisitor(Expression<Func<T, bool>> expression)
        {
            _filterFunction = expression.Compile();
            _result = new List<T>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// The filtered items
        /// </summary>
        public List<T> Result
        {
            get
            {
                return _result;
            }
        }

        #endregion

        #region IVisitor<T> Members

        /// <summary>
        /// Visits the specified item, and adds the item to the result if the filter function is true
        /// </summary>
        /// <param name="item">The item to be evaluated</param>
        public void Visit(T item)
        {
            if (_filterFunction(item))
            {
                _result.Add(item);
            }
        }

        #endregion
    }
}
Friday, June 04, 2010 9:38:48 PM UTC  #    Comments [0]    |  Trackback
 Friday, May 28, 2010

http://msdn.microsoft.com/en-us/library/bb335710.aspx

            // Lambda expression as executable code.
            Func<int, bool> deleg = i => i < 5;
            // Invoke the delegate and display the output.
            Console.WriteLine("deleg(4) = {0}", deleg(4));

            // Lambda expression as data in the form of an expression tree.
            System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
            // Compile the expression tree into executable code.
            Func<int, bool> deleg2 = expr.Compile();
            // Invoke the method and print the output.
            Console.WriteLine("deleg2(4) = {0}", deleg2(4));

            /*  This code produces the following output:

                deleg(4) = True
                deleg2(4) = True
            */
Friday, May 28, 2010 7:18:01 PM UTC  #    Comments [0]    |   |  Trackback
public IPrintableDisplayableDocument CreateReport(BillSummaryReportData reportData)
        {
            Word2007ReportWriter writer = new Word2007ReportWriter(reportData, ResourceTransform.BillSummaryReport);
           
            Word2007Document document = writer.CreateReport();

            using (var stream = new MemoryStream())
            {
                stream.Write(document.Content, 0, document.Content.Length);

                // insert bill summary rtf chunk
                var wordDoc = WordprocessingDocument.Open(stream, true);
                Stream streamImportRtf =
                    new MemoryStream(UTF8Encoding.Default.GetBytes(reportData.BillSummaryReportBody.BillSummary));
                AlternativeFormatImportPart chunkRtf =
                    wordDoc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Rtf,
                                                                            "BillSummaryRtfChunk");
                chunkRtf.FeedData(streamImportRtf);
                wordDoc.Close();

                document.Content = stream.ToArray();

            }

            return document;
        }
Friday, May 28, 2010 7:12:51 PM UTC  #    Comments [0]    |   |  Trackback

http://weblogs.asp.net/whaggard/archive/2004/09/23/233535.aspx
Stream s = new MemoryStream(UTF8Encoding.Default.GetBytes("Test String"));

Friday, May 28, 2010 12:49:48 PM UTC  #    Comments [0]    |  Trackback
 Thursday, May 27, 2010
 Monday, May 24, 2010

http://randypatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/

http://en.wikipedia.org/wiki/Fluent_interface#C.23

#region Fluent Example
 
    public interface IConfigurationFluent
    {
        IConfigurationFluent SetColor(string color);
        IConfigurationFluent SetHeight(int height);
        IConfigurationFluent SetLength(int length);
        IConfigurationFluent SetDepth(int depth);
    }
 
    public class ConfigurationFluent : IConfigurationFluent
    {
        string color;
        int height;
        int length;
        int depth;
 
        public IConfigurationFluent SetColor(string color)
        {
            this.color = color;
            return this;
        }
 
        public IConfigurationFluent SetHeight(int height)
        {
            this.height = height;
            return this;
        }
 
        public IConfigurationFluent SetLength(int length)
        {
            this.length = length;
            return this;
        }
 
        public IConfigurationFluent SetDepth(int depth)
        {
            this.depth = depth;
            return this;
        }
    }
 
    #endregion
 
    public class ExampleProgram
    {
        public static void Main(string[] args)
        {
            //Standard Example
            IConfiguration config = new Configuration
            {
                Color = "blue",
                Height = 1,
                Length = 2,
                Depth = 3
            };
 
            //Fluent Example
            IConfigurationFluent fluentConfig =
                  new ConfigurationFluent().SetColor("blue")
                                           .SetHeight(1)
                                           .SetLength(2)
                                           .SetDepth(3);
        }
    }
Monday, May 24, 2010 3:32:43 PM UTC  #    Comments [0]    |   |  Trackback
 Friday, May 21, 2010

http://richnewman.wordpress.com/2007/04/08/top-level-exception-handling-in-windows-forms-applications-part-1/

Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException);

Friday, May 21, 2010 2:13:00 PM UTC  #    Comments [0]    |  Trackback
 Wednesday, April 14, 2010

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx

If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised. If you have more than one BackgroundWorker, you should not reference any of them directly, as this would couple your DoWork event handler to a specific instance of BackgroundWorker. Instead, you should access your BackgroundWorker by casting the sender parameter in your DoWork event handler.

http://blogs.msdn.com/thottams/archive/2009/04/11/asynchronous-delegates-and-calback.aspx

When you do a BeginInvoke the delegate is invoked on a thread from the thread pool and on completion the callback is also called on the same thread pool thread. Now, any exceptions that is thrown on this thread cannot be caught by the main thread and so will crash the process. If you want to handle the exception, then you have to catch the expected exception in the callback and store it to some state and rethrow the exception from the main thread so that the callers above you can handle it gracefully.

Wednesday, April 14, 2010 1:33:47 PM UTC  #    Comments [0]    |   |  Trackback
 Sunday, April 11, 2010

http://www.albahari.com/threading/part2.aspx

image

Passing Data to a Thread



The easiest way to pass arguments to a thread’s target method is to execute a lambda
expression that calls the method with the desired arguments:

static void Main()
{
Thread t = new Thread ( () => Print ("Hello from t!") );
t.Start();
}

static void Print (string message) { Console.WriteLine (message); }

With this approach, you can pass in any number of arguments to the method. You
can even wrap the entire implementation in a multistatement lambda:

new Thread (() =>
{
Console.WriteLine ("I'm running on another thread!");
Console.WriteLine ("This is so easy!");
}).Start();

You can do the same thing almost as easily in C# 2.0 with anonymous methods:

new Thread (delegate()
{
...
}).Start();

Another technique is to pass an argument into Thread’s Start method:

 

static void Main()
{
Thread t = new Thread (Print);
t.Start ("Hello from t!");
}
static void Print (object messageObj)
{
string message = (string) messageObj; // We need to cast here
Console.WriteLine (message);
}

This works because Thread’s constructor is overloaded to accept either of two
delegates:

public delegate void ThreadStart();
public delegate void ParameterizedThreadStart (object obj);

The limitation of ParameterizedThreadStart is that it accepts only one argument.
And because it’s of type object, it usually needs to be cast.

 

 

Foreground and Background Threads

 

When a process terminates in this manner, any finally blocks in the execution stack
of background threads are circumvented. This is a problem if your program employs
finally (or using) blocks to perform cleanup work such as releasing resources or
deleting temporary files. To avoid this, you can explicitly wait out such background
threads upon exiting an application. There are two ways to accomplish this:

• If you’ve created the thread yourself, call Join on the thread.
• If you’re on a pooled thread (see “Thread Pooling” on page 800) use an event
wait handle (see “Signaling with Event Wait Handles” on page 832).

 

Thread Pooling

 

There are a number of ways to enter the thread pool:
• Via the Task Parallel Library or PLINQ (from Framework 4.0)
• By calling ThreadPool.QueueUserWorkItem
• Via asynchronous delegates
• Via BackgroundWorker

 

 

Here’s how you start a worker task via an asynchronous delegate:
1. Instantiate a delegate targeting the method you want to run in parallel (typically
one of the predefined Func delegates).
2. Call BeginInvoke on the delegate, saving its IAsyncResult return value.
BeginInvoke returns immediately to the caller. You can then perform other activities
while the pooled thread is working.
3. When you need the results, call EndInvoke on the delegate, passing in the saved
IAsyncResult object.

 

Don’t confuse asynchronous delegates with asynchronous
methods (methods starting with Begin or End, such as
File.BeginRead/File.EndRead). Asynchronous methods follow
a similar protocol outwardly, but they exist to solve a much
more difficult problem, which we describe in Chapter 23.

 

Synchronization

 

Nonblocking Synchronization

 

The following implicitly generate full fences:
• C#’s lock statement (Monitor.Enter/Monitor.Exit)
• All methods on the Interlocked class (we’ll cover these soon)
• Asynchronous callbacks that use the thread pool—these include asynchronous delegates, APM callbacks (Chapter 23), and Task continuations (Chapter 22)
• Setting and waiting on a signaling construct
• Anything that relies on signaling, such as starting or waiting on a Task

 

 

Memory barriers and locking

C#'s lock statement is in fact a syntactic shortcut for a call to the methods Monitor.Enter and Monitor.Exit, with a try-finally block. Here's what's actually happening within the Go method of the preceding example:

Monitor.Enter (locker);
try
{
  if (val2 != 0) Console.WriteLine (val1 / val2);
  val2 = 0;
}
finally { Monitor.Exit (locker); 

As we said earlier, Monitor.Enter and Monitor.Exit both generate full fences. So if
we ignore a lock’s mutual exclusion guarantee, we could say that this:

lock (someField) { ... }

is equivalent to this:

Thread.MemoryBarrier(); { ... } Thread.MemoryBarrier();

A Mutex is like a C# lock, but it can work across multiple processes. In other words, Mutex can be computer-wide as well as application-wide.

With a Mutex class, you call the WaitOne method to lock and ReleaseMutex to unlock. Just as with the lock statement, a Mutex can be released only from the same thread that obtained it.
A common use for a cross-process Mutex is to ensure that only one instance of a program can run at a time.

 

A Semaphore is like a nightclub: it has a certain capacity, enforced by a bouncer. Once it's full, no more people can enter and a queue builds up outside. Then, for each person that leaves, one person enters from the head of the queue. The constructor requires a minimum of two arguments: the number of places currently available in the nightclub and the club's total capacity.


A Semaphore with a capacity of one is similar to a Mutex or lock, except that the Semaphore has no "owner"—it's thread-agnostic. Any thread can call Release on a Semaphore, whereas with Mutex and lock, only the thread that obtained the lock can release it.


Semaphores can be useful in limiting concurrency—preventing too many threads from executing a particular piece of code at once. In the following example, five threads try to enter a nightclub that allows only three threads in at once:

 

 

 

Interlocked

Interlocked.Increment (ref _sum);
Interlocked.Decrement (ref _sum);

 

Interlocked’s mathematical operations are restricted to Increment, Decrement, and Add.

 

 

Signaling with Event Wait Handles

 

class BasicWaitHandle
{
  static EventWaitHandle wh = new AutoResetEvent (false);

  static void Main(  )
  {
    new Thread (Waiter).Start(  );
    Thread.Sleep (1000);                  // Pause for a second...
    wh.Set(  );                             // Wake up the Waiter.
  }

  static void Waiter(  )
  {
    Console.WriteLine ("Waiting...");
    wh.WaitOne(  );                        // Wait for notification
    Console.WriteLine ("Notified");
  }
}

 

Thread.Interrupt and Abort

Calling Interrupt on a blocked thread forcibly releases it, throwing a ThreadInterruptedException
Interrupting a thread does not cause the thread to end, unless the ThreadInterruptedException is unhandled.

The big difference between Interrupt and Abort is what happens when it's called on a thread that is not blocked. Whereas Interrupt waits until the thread next blocks before doing anything, Abort throws an exception on the thread right where it's executing (unmanaged code excepted). This is a problem because .NET Framework code might be aborted; code that is not abort-safe. This rules out using Abort in almost any nontrivial context.

 

An alternative to aborting another thread is to implement a pattern whereby the worker periodically checks a cancel flag, exiting if the flag is true. To abort, the instigator simply sets the flag, and then waits for the worker to comply.

 

 

BackgroundWorker

BackgroundWorker is a helper class in the System.ComponentModel namespace for managing a worker thread. It provides the following features:

  • A cancel flag for signaling a worker to end without using Abort

  • A standard protocol for reporting progress, completion, and cancellation

  • An implementation of IComponent allowing it be sited in Visual Studio's designer

  • Exception handling on the worker thread

  • The ability to update Windows Forms or WPF controls in response to worker progress or completion

    bw = new BackgroundWorker(  );
    bw.WorkerReportsProgress = true;
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += bw_DoWork;
    bw.ProgressChanged += bw_ProgressChanged;
    bw.RunWorkerCompleted += bw_RunWorkerCompleted;

    bw.RunWorkerAsync ("Hello to worker");

ReadWRiterLockSlim

With both classes, there are two basic kinds of lock—a read lock and a write lock:

  • A write lock is universally exclusive.

  • A read lock is compatible with other read locks.

So, a thread holding a write lock blocks all other threads trying to obtain a read or write lock (and vice versa). But if no thread holds a write lock, any number of threads may concurrently obtain a read lock.

ReaderWriterLockSlim defines the following methods for obtaining and releasing read/write locks:

public void EnterReadLock(  );
public void ExitReadLock(  );
public void EnterWriteLock(  );
public void ExitWriteLock(  );
Timer

The .NET Framework provides four timers. Two of these are general-purpose multithreaded timers:

  • System.Threading.Timer

  • System.Timers.Timer

The other two are special-purpose single-threaded timers:

  • System.Windows.Forms.Timer (Windows Forms timer)

  • System.Windows.Threading.DispatcherTimer (WPF timer)

Both are like System.Timers.Timer in the members that they expose (Interval, Tick, Start, and Stop) and are used in a similar manner. However, they differ in how they work internally. Instead of using the thread pool to generate timer events, the Windows Forms and WPF timers rely on the message pumping mechanism of their underlying user interface model. This means that the Tick event always fires on the same thread that originally created the timer—which, in a normal application, is the same thread used to manage all user interface elements and controls. This has a number of benefits:

  • You can forget about thread safety.

  • A fresh Tick will never fire until the previous Tick has finished processing.

  • You can update user interface elements and controls directly from Tick event handling code, without calling Control.Invoke or Dispatcher.Invoke.

Sunday, April 11, 2010 9:35:30 PM UTC  #    Comments [0]    |   |  Trackback

http://msdn.microsoft.com/en-us/library/ms229603.aspx

Each control that accepts free-form user input has a Validating event that will occur whenever the control requires data validation. In the Validating event-handling method, you can validate user input in several ways.

The Validating event is supplied an object of type CancelEventArgs. If you determine that the control's data is not valid, you can cancel the Validating event by setting this object's Cancel property to true. If you do not set the Cancel property, Windows Forms will assume that validation succeeded for that control, and raise the Validated event.

When you use data binding, the data in your control is synchronized with the data source during execution of the Validating event. If you cancel the Validating event, the data will not be synchronized with the data source

Implicit and Explicit Validation

So when does a control's data get validated? This is up to you, the developer. You can use either implicit or explicit validation, depending on the needs of your application.

Implicit Validation

The implicit validation approach validates data as the user enters it. You can validate the data as the data is entered in a control by reading the keys as they are pressed, or more commonly whenever the user takes the input focus away from one control and moves to the next. This approach is useful when you want to give the user immediate feedback about the data as they are working.

If you want to use implicit validation for a control, you must set that control's AutoValidate property to true. If you cancel the Validating event, the behavior of the control will be determined by what value that you assigned to AutoValidate. If you assigned EnablePreventFocusChange, canceling the event will cause the Validated event not to occur. Input focus will remain on the current control until the user changes the data to a valid input. If you assigned EnableAllowFocusChange, the Validated event will not occur when you cancel the event, but focus will still change to the next control.

Assigning Disable to the AutoValidate property prevents implicit validation altogether. To validate your controls, you will have to use explicit validation.

Explicit Validation

The explicit validation approach validates data at one time. You can validate the data in response to a user action, such as clicking a Save button or a Next link. When the user action occurs, you can trigger explicit validation in one of the following ways:

  • Call Validate to validate the last control to have lost focus.

  • Call ValidateChildren to validate all child controls in a form or container control.

  • Call a custom method to validate the data in the controls manually.

image

Sunday, April 11, 2010 6:25:46 PM UTC  #    Comments [0]    |  Trackback
 Thursday, December 03, 2009
 Monday, November 09, 2009

Understand how event increase the runtime coupling among objects.

Event-based communication loosens the static coupling between types, but it comes at the cost of tighter runtime coupling between the event generator and the event subscribers. The multicast nature of events means that all subscribers must agree on a protocol for responding to the event source. The event model, in which the event source holds a reference to all subscribers, means that all subscribers must either (1) remove event handlers when the subscriber wants to be disposed of or (2) simply cease to exist. Also, the event source must unhook all event handlers when the source should cease to exist. You must factor those issues into your design decision to use events.

public class DoesWorkThatMightFail
{
    public bool TryDoWork()
    {
        if (!TestConditions())
            return false;
        Work(); // may throw on failures, but unlikely
        return true;
    }

    public void DoWork()
    {
        Work(); // will throw on failures.
    }

    private bool TestConditions()
    {
        // body elided
        // Test conditions here
        return true;
    }

    private void Work()
    {
        // elided
        // Do the work here
    }
}

 

 

 

You should expand your set of design choices and use both composition and inheritance. When you create types that reuse implementation from other types, you should use composition. If your types model an Is A relationship in every way, inheritance is the better choice. Composition requires more work to expose the implementation from the inner objects, but the payoff is more control over the coupling between your type and the type whose implementation you wish to reuse. Using inheritance means that your derived type is a special case of the base class in every way.

Extension methods provide a mechanism for C# developers to define behavior in interfaces. You can define an interface with minimal capabilities and then create a set of extension methods defined on that interface to extend its capabilities. In particular, you can add behavior instead of just defining an API.

 

In short, it's best to declare local variables using var unless developers (including you, in the future) need to see the declared type to understand the code. The title of this item says "prefer," not "always." I recommend explicitly declaring all numeric types (int, float, double, and others) rather than use a var declaration. In addition, use the type parameter in generics (for example T, tresult) rather than var. For everything else, just use var. Merely typing more keystrokes—to explicitly declare the type—doesn't promote type safety or improve readability. You may also introduce inefficiencies that the compiler will avoid if you pick the wrong declared type.

 

Anonymous types aren't as exotic as they seem, and they don't harm readability when they are used correctly. If you have interim results that you need to keep track of and if they're modeled well with an immutable type, then you should use anonymous types. When you need to define behaviors on those types, that's when you need to create concrete types to represent those concepts. In the meantime, the compiler can generate all the boilerplate code you need. You clearly communicate to other developers that the type is used only within the context of that method and those generic methods it calls.

Monday, November 09, 2009 9:13:48 PM UTC  #    Comments [0]    |  Trackback
 Monday, April 21, 2008

http://www.developerfusion.co.uk/show/2936/4/

   1:  using System;
   2:  using System.IO;
   3:  using System.Runtime.Serialization.Formatters.Soap;
   4:  using System.Reflection;
   5:  using System.Collections;
   6:   
   7:  [Serializable]
   8:  public class User{
   9:      [ValidLength(4,8,Message="UserID should be between 4 and 8 characters long")]
  10:      public string userID;
  11:   
  12:      [ValidLength(4,8,Message="Password should be between 4 and 6 characters long")]
  13:      public string password;
  14:   
  15:      [ValidLength(4,60)]
  16:      public string email;
  17:      public string city;
  18:   
  19:      public void Save(string fileName){
  20:           FileStream s=new FileStream(fileName,FileMode.Create);
  21:           SoapFormatter sf=new SoapFormatter();
  22:           sf.Serialize(s,this);
  23:      }
  24:   
  25:      static void Main(string[] args){
  26:           User u=new User();
  27:           u.userID="first";
  28:           u.password="Zxfd12Qs";
  29:           u.email=".com";
  30:           u.city="";
  31:           Validator v=new Validator();
  32:           if(!v.IsValid(u)){
  33:                foreach(string message in v.Messages)
  34:                     Console.WriteLine(message);
  35:           }
  36:           else {u.Save("user.txt");}
  37:      }
  38:  }
 
   1:  [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
   2:  public class ValidLengthAttribute : Attribute{
   3:      private int _min;
   4:      private int _max;
   5:      private string _message;
   6:   
   7:      public ValidLengthAttribute(int min,int max){
   8:           _min=min;
   9:           _max=max;
  10:      }
  11:   
  12:      public string Message{
  13:           get {return(_message);}
  14:           set {_message=value;}
  15:      }
  16:   
  17:      public string Min{
  18:           get{return _min.ToString();}
  19:      }
  20:   
  21:      public string Max{
  22:           get{return _max.ToString();}
  23:      }
  24:   
  25:      public bool IsValid(string theValue){
  26:           int length=theValue.Length;
  27:           if(length >= _min && length <= _max) return true;
  28:           return false;
  29:      }
  30:  }

 

   1:  public class Validator{
   2:      public ArrayList Messages=new ArrayList();
   3:   
   4:      public bool IsValid(object anObject){
   5:           bool isValid=true;
   6:           FieldInfo[] fields = anObject.GetType().GetFields(BindingFlags.Public|BindingFlags.Instance);
   7:                foreach (FieldInfo field in fields)
   8:                     if(!isValidField(field,anObject)) isValid=false;
   9:                return isValid;
  10:      }
  11:   
  12:      private bool isValidField(FieldInfo aField,object anObject){
  13:           object[] attributes=aField.GetCustomAttributes(typeof(ValidLengthAttribute),true);
  14:           if(attributes.GetLength(0) ==0) return true;
  15:           return isValidField(aField,anObject,(ValidLengthAttribute)attributes[0]);
  16:      }
  17:   
  18:      private bool isValidField(FieldInfo aField, object anObject,ValidLengthAttribute anAttr){
  19:           string theValue=(string)aField.GetValue(anObject);
  20:           if (anAttr.IsValid(theValue)) return true;
  21:           addMessages(aField,anAttr);
  22:           return false;
  23:      }
  24:   
  25:      private void addMessages(FieldInfo aField,ValidLengthAttribute anAttr){
  26:           if(anAttr.Message !=null){
  27:                Messages.Add(anAttr.Message);
  28:                return;
  29:           }
  30:           Messages.Add("Invalid range for "+aField.Name+". Valid range is between "+anAttr.Min+" and "+anAttr.Max);
  31:      }
  32:  }
 
Monday, April 21, 2008 3:35:05 PM UTC  #    Comments [0]    |  Trackback
 Friday, September 28, 2007

When you click datasource in .net, it will drive you crazy when error message "object reference not set to an instance of an object" pops out. It is caused by VS.Net can not load data source configuration file correctly. Solution is that you have to go into exclude the datasource configuration file from your project. Normally it is put under the properties directory in the project.

Friday, September 28, 2007 8:02:42 PM UTC  #    Comments [0]    |   |  Trackback
 Tuesday, July 03, 2007

From :

http://www.cnblogs.com/reonlyrun/archive/2007/04/05/CSharp_25_Question.html

C#基础概念二十五问

注:本文部份资料来自网络,如有侵权,请与我联系,我会在第一时间声明引用或将其删除!

    当初学 C# 时是找个人大概问了一下数据类型和分支语句就开始做项目了。这两天又全面的看了一下相关的基础知识(学而时习之嘛),总结了25个问题:

1.静态成员和非静态成员的区别?
2.const 和 static readonly 区别?
3.extern 是什么意思?
4.abstract 是什么意思?
5.internal 修饰符起什么作用?
6.sealed 修饰符是干什么的?
7.override 和 overload 的区别?
8.什么是索引指示器?
9.new 修饰符是起什么作用?
10.this 关键字的含义?
11.可以使用抽象函数重写基类中的虚函数吗?
12.密封类可以有虚函数吗?
13.什么是属性访问器?
14.abstract 可以和 virtual 一起使用吗?可以和 override 一起使用吗?
15.接口可以包含哪些成员?
16.类和结构的区别?
17.接口的多继承会带来哪些问题?
18.抽象类和接口的区别?
19.别名指示符是什么?
20.如何手工释放资源?
21.P/Invoke是什么?
22.StringBuilder 和 String 的区别?
23.explicit 和 implicit 的含义?
24.params 有什么用?
25.什么是反射?

以下是我做的一份参考答案(C# 语言范畴之内),如果有不准确、不全面的,欢迎各位朋友指正!

1.静态成员和非静态成员的区别?

答:

静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问

不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对象进行访问

一个类的所有实例的同一静态变量都是同一个值,同一个类的不同实例的同一非静态变量可以是不同的值

静态函数的实现里不能使用非静态成员,如非静态变量、非静态函数等

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example01
{
    class Program
    {
        class Class1
        {
            public static String staticStr = "Class";
            public String notstaticStr = "Obj";
        }
        static void Main(string[] args)
        {
            //静态变量通过类进行访问,该类所有实例的同一静态变量都是同一个值
            Console.WriteLine("Class1's staticStr: {0}", Class1.staticStr);
 
            Class1 tmpObj1 = new Class1();
            tmpObj1.notstaticStr = "tmpObj1";
            Class1 tmpObj2 = new Class1();
            tmpObj2.notstaticStr = "tmpObj2";
 
            //非静态变量通过对象进行访问,不同对象的同一非静态变量可以有不同的值
            Console.WriteLine("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr);
            Console.WriteLine("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr);
 
            Console.ReadLine();
        }
    }
}

结果:
Class1's staticStr: Class
tmpObj1's notstaticStr: tmpObj1
tmpObj2's notstaticStr: tmpObj2

2.const 和 static readonly 区别?

答:

const

用 const 修饰符声明的成员叫常量,是在编译期初始化并嵌入到客户端程序

static readonly

用 static readonly 修饰符声明的成员依然是变量,只不过具有和常量类似的使用方法:通过类进行访问、初始化后不可以修改。但与常量不同的是这种变量是在运行期初始化

示例:

测试类:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example02Lib
{
    public class Class1
    {
        public const String strConst = "Const";
        public static readonly String strStaticReadonly = "StaticReadonly";
        //public const String strConst = "Const Changed";
        //public static readonly String strStaticReadonly = "StaticReadonly Changed";
    }
}
 
客户端代码:
using System;
using System.Collections.Generic;
using System.Text;
using Example02Lib;
 
namespace Example02
{
    class Program
    {
        static void Main(string[] args)
        {
            //修改Example02中Class1的strConst初始值后,只编译Example02Lib项目
            //然后到资源管理器里把新编译的Example02Lib.dll拷贝Example02.exe所在的目录,执行Example02.exe
            //切不可在IDE里直接调试运行因为这会重新编译整个解决方案!!
 
            //可以看到strConst的输出没有改变,而strStaticReadonly的输出已经改变
            //表明Const变量是在编译期初始化并嵌入到客户端程序,而StaticReadonly是在运行时初始化的
            Console.WriteLine("strConst : {0}", Class1.strConst);
            Console.WriteLine("strStaticReadonly : {0}", Class1.strStaticReadonly);
 
            Console.ReadLine();
        }
    }
}

结果:
strConst : Const
strStaticReadonly : StaticReadonly

修改后的示例:

测试类:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example02Lib
{
    public class Class1
    {
        //public const String strConst = "Const";
        //public static readonly String strStaticReadonly = "StaticReadonly";
        public const String strConst = "Const Changed";
        public static readonly String strStaticReadonly = "StaticReadonly Changed";
    }
}

结果

strConst : Const
strStaticReadonly : StaticReadonly Changed

3.extern 是什么意思?

答:

extern 修饰符用于声明由程序集外部实现的成员函数

经常用于系统API函数的调用(通过 DllImport )。注意,和DllImport一起使用时要加上 static 修饰符

也可以用于对于同一程序集不同版本组件的调用(用 extern 声明别名)

不能与 abstract 修饰符同时使用

示例:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
 
namespace Example03
{
    class Program
    {
        //注意DllImport是一个Attribute Property,在System.Runtime.InteropServices命名空间中定义
        //extern与DllImport一起使用时必须再加上一个static修饰符
        [DllImport("User32.dll")]
        public static extern int MessageBox(int Handle, string Message, string Caption, int Type);
 
        static int Main()
        {
            string myString;
            Console.Write("Enter your message: ");
            myString = Console.ReadLine();
            return MessageBox(0, myString, "My Message Box", 0);
        }
    }
}

结果:

4.abstract 是什么意思?

答:

abstract 修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽象成员

abstract 不可以和 static 、virtual 一起使用

声明为 abstract 成员可以不包括实现代码,但只要类中还有未实现的抽象成员(即抽象类),那么它的对象就不能被实例化,通常用于强制继承类必须实现某一成员

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example04
{
    #region 基类,抽象类
    public abstract class BaseClass
    {
        //抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写
        public abstract String Attribute
        {
            get;
            set;
        }
 
        //抽象方法,传入一个字符串参数无返回值
        public abstract void Function(String value);
 
        //抽象事件,类型为系统预定义的代理(delegate):EventHandler
        public abstract event EventHandler Event;
 
        //抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读
        public abstract Char this[int Index]
        {
            get;
        }
    }
    #endregion
 
    #region 继承类
    public class DeriveClass : BaseClass
    {
        private String attribute;
 
        public override String Attribute
        {
            get
            {
                return attribute;
            }
            set
            {
                attribute = value;
            }
        }
        public override void Function(String value)
        {
            attribute = value;
            if (Event != null)
            {
                Event(this, new EventArgs());
            }
        }
        public override event EventHandler Event;
        public override Char this[int Index]
        {
            get
            {
                return attribute[Index];
            }
        }
    }
    #endregion
 
    class Program
    {
        static void OnFunction(object sender, EventArgs e)
        {
            for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)
            {
                Console.WriteLine(((DeriveClass)sender)[i]);
            }
        }
        static void Main(string[] args)
        {
            DeriveClass tmpObj = new DeriveClass();
 
            tmpObj.Attribute = "1234567";
            Console.WriteLine(tmpObj.Attribute);
 
            //将静态函数OnFunction与tmpObj对象的Event事件进行关联
            tmpObj.Event += new EventHandler(OnFunction);
 
            tmpObj.Function("7654321");
 
            Console.ReadLine();
        }
    }
}

结果:
1234567
7
6
5
4
3
2
1

5.internal 修饰符起什么作用?

答:

internal 修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问

接口的成员不能使用 internal 修饰符

值得注意的是,如果为 internal 成员加上了 protected 修饰符,这时的访问级别为 internal 或 protected。只是看字面意思容易弄错,许多人认为 internal protected 应该是“只有同一个程序集中的子类可以访问”,但其实它表示“同一个程序集中的所有类,以及所有程序集中的子类都可以访问”

示例

Example05Lib 项目的 Class1

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example05Lib
{
    public class Class1
    {
        internal String strInternal = null;
        public String strPublic;
        internal protected String strInternalProtected = null;
    }
}

结果
Example05Lib 项目的 Class2 类可以访问到 Class1 的 strInternal 成员,当然也可以访问到 strInternalProtected 成员,因为他们在同一个程序集里

Example05 项目里的 Class3 类无法访问到 Class1 的 strInternal 成员,因为它们不在同一个程序集里。但却可以访问到 strInternalProtected 成员,因为 Class3 是 Class1 的继承类

Example05 项目的 Program 类既无法访问到 Class1 的 strInternal 成员,也无法访问到 strInternalProtected 成员,因为它们既不在同一个程序集里也不存在继承关系

6.sealed 修饰符是干什么的?

答:

sealed 修饰符表示密封

用于类时,表示该类不能再被继承,不能和 abstract 同时使用,因为这两个修饰符在含义上互相排斥

用于方法和属性时,表示该方法或属性不能再被继承,必须和 override 关键字一起使用,因为使用 sealed 修饰符的方法或属性肯定是基类中相应的虚成员

通常用于实现第三方类库时不想被客户端继承,或用于没有必要再继承的类以防止滥用继承造成层次结构体系混乱

恰当的利用 sealed 修饰符也可以提高一定的运行效率,因为不用考虑继承类会重写该成员

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example06
{
    class Program
    {
        class A
        {
            public virtual void F()
            {
                Console.WriteLine("A.F");
            }
            public virtual void G()
            {
                Console.WriteLine("A.G");
            }
        }
        class B : A
        {
            public sealed override void F()
            {
                Console.WriteLine("B.F");
            }
            public override void G()
            {
                Console.WriteLine("B.G");
            }
        }
        class C : B
        {
            public override void G()
            {
                Console.WriteLine("C.G");
            }
        }
        static void Main(string[] args)
        {
            new A().F();
            new A().G();
            new B().F();
            new B().G();
            new C().F();
            new C().G();
 
            Console.ReadLine();
        }
    }
}

结果:
类 B 在继承类 A 时可以重写两个虚函数,如图所示:

由于类 B 中对 F 方法进行了密封, 类 C 在继承类 B 时只能重写一个函数,如图所示:

控制台输出结果,类 C 的方法 F 只能是输出 类B 中对该方法的实现:

A.F
A.G
B.F
B.G
B.F
C.G

7.override 和 overload 的区别?

答:

override 表示重写,用于继承类对基类中虚成员的实现

overload 表示重载,用于同一个类中同名方法不同参数(包括类型不同或个数不同)的实现

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example07
{
    class Program
    {
        class BaseClass
        {
            public virtual void F()
            {
                Console.WriteLine("BaseClass.F");
            }
        }
        class DeriveClass : BaseClass
        {
            public override void F()
            {
                base.F();
                Console.WriteLine("DeriveClass.F");
            }
            public void Add(int Left, int Right)
            {
                Console.WriteLine("Add for Int: {0}", Left + Right);
            }
            public void Add(double Left, double Right)
            {
                Console.WriteLine("Add for int: {0}", Left + Right);
            }
        }
        static void Main(string[] args)
        {
            DeriveClass tmpObj = new DeriveClass();
            tmpObj.F();
            tmpObj.Add(1, 2);
            tmpObj.Add(1.1, 2.2);
 
            Console.ReadLine();
        }
    }
}

结果:
BaseClass.F
DeriveClass.F
Add for Int: 3
Add for int: 3.3

8.什么是索引指示器?

答:

实现索引指示器(indexer)的类可以象数组那样使用其实例后的对象,但与数组不同的是索引指示器的参数类型不仅限于int

简单来说,其本质就是一个含参数属性

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example08
{
    public class Point
    {
        private double x, y;
        public Point(double X, double Y)
        {
            x = X;
            y = Y;
        }
        //重写ToString方法方便输出
        public override string ToString()
        {
            return String.Format("X: {0} , Y: {1}", x, y);
        }
    }
    public class Points
    {
        Point[] points;
        public Points(Point[] Points)
        {
            points = Points;
        }
        public int PointNumber
        {
            get 
            { 
                return points.Length; 
            }
        }    
        //实现索引访问器
        public Point this[int Index]
        {
            get
            {
                return points[Index];
            }
        }
    }
 
    //感谢watson hua(http://huazhihao.cnblogs.com/)的指点
    //索引指示器的实质是含参属性,参数并不只限于int
    class WeatherOfWeek
    {
        public string this[int Index]
        {
            get
            {
                //注意case段使用return直接返回所以不需要break
                switch (Index)
                {
                    case 0:
                        {
                            return "Today is cloudy!";
                        }
                    case 5:
                        {
                            return "Today is thundershower!";
                        }
                    default:
                        {
                            return "Today is fine!";
                        }
                }
            }
        }
        public string this[string Day]
        {
            get
            {
                string TodayWeather = null;
                //switch的标准写法
                switch (Day)
                {
                    case "Sunday":
                        {
                            TodayWeather = "Today is cloudy!";
                            break;
                        }
                    case "Friday":
                        {
                            TodayWeather = "Today is thundershower!";
                            break;
                        }
                    default:
                        {
                            TodayWeather = "Today is fine!";
                            break;
                        }
                }
                return TodayWeather;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Point[] tmpPoints = new Point[10];
            for (int i = 0; i < tmpPoints.Length; i++)
            {
                tmpPoints[i] = new Point(i, Math.Sin(i));
            }
 
            Points tmpObj = new Points(tmpPoints);
            for (int i = 0; i < tmpObj.PointNumber; i++)
            {
                Console.WriteLine(tmpObj[i]);
            }
 
 
            string[] Week = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday"};
            WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek();
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(tmpWeatherOfWeek[i]);
            }
            foreach (string tmpDay in Week)
            {
                Console.WriteLine(tmpWeatherOfWeek[tmpDay]);
            }
 
            Console.ReadLine();
        }
    }
}

结果:
X: 0 , Y: 0
X: 1 , Y: 0.841470984807897
X: 2 , Y: 0.909297426825682
X: 3 , Y: 0.141120008059867
X: 4 , Y: -0.756802495307928
X: 5 , Y: -0.958924274663138
X: 6 , Y: -0.279415498198926
X: 7 , Y: 0.656986598718789
X: 8 , Y: 0.989358246623382
X: 9 , Y: 0.412118485241757
Today is cloudy!
Today is fine!
Today is fine!
Today is fine!
Today is fine!
Today is thundershower!
Today is cloudy!
Today is fine!
Today is fine!
Today is fine!
Today is fine!
Today is thundershower!
Today is fine!

9.new 修饰符是起什么作用?

答:

new 修饰符与 new 操作符是两个概念

new 修饰符用于声明类或类的成员,表示隐藏了基类中同名的成员。而new 操作符用于实例化一个类型

new 修饰符只能用于继承类,一般用于弥补基类设计的不足

new 修饰符和 override 修饰符不可同时用在一个成员上,因为这两个修饰符在含义上互相排斥

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example09
{
    class BaseClass
    {
        //基类设计者声明了一个PI的公共变量,方便进行运算
        public static double PI = 3.1415;
    }
    class DervieClass : BaseClass
    {
        //继承类发现该变量的值不能满足运算精度,于是可以通过new修饰符显式隐藏基类中的声明
        public new static double PI = 3.1415926;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(BaseClass.PI);
            Console.WriteLine(DervieClass.PI);
 
            Console.ReadLine();
        }
    }
}

结果:
3.1415
3.1415926

10.this 关键字的含义?

答:

this 是一个保留字,仅限于构造函数和方法成员中使用

在类的构造函数中出现表示对正在构造的对象本身的引用,在类的方法中出现表示对调用该方法的对象的引用,在结构的构造上函数中出现表示对正在构造的结构的引用,在结构的方法中出现表示对调用该方法的结果的引用

this 保留字不能用于静态成员的实现里,因为这时对象或结构并未实例化

在 C# 系统中,this 实际上是一个常量,所以不能使用 this++ 这样的运算

this 保留字一般用于限定同名的隐藏成员、将对象本身做为参数、声明索引访问器、判断传入参数的对象是否为本身

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example10
{
    class Class1
    {
        private double c;
        private string value;
 
        public double C
        {
            get
            {
                return c;
            }
        }
        public Class1(double c)
        {
            //限定同名的隐藏成员
            this.c = c;
        }
        public Class1(Class1 value)
        {
            //用对象本身实例化自己没有意义
            if (this != value)
            {
                c = value.C;
            }
        }
        public override string ToString()
        {
            //将对象本身做为参数
            return string.Format("{0} Celsius = {1} Fahrenheit", c, UnitTransClass.C2F(this));
        }
 
        //由于好奇,在这做了一个效率测试,想看看到底哪种方式访问成员变量更快,结论:区别不大。。。
        public string Test1()
        {
            long vTickCount = Environment.TickCount;
            for (int i = 0; i < 10000000; i++)
                this.value = i.ToString();
            return string.Format("Have this.: {0} MSEL", Environment.TickCount - vTickCount);
        }
        public string Test2()
        {
            long vTickCount = Environment.TickCount;
            for (int i = 0; i < 10000000; i++)
                value = i.ToString();
            return string.Format("Don't have this.: {0} MSEL", Environment.TickCount - vTickCount);
        }
    }
    class UnitTransClass
    {
        public static double C2F(Class1 value)
        {
            //摄氏到华氏的转换公式
            return 1.8 * value.C + 32;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 tmpObj = new Class1(37.5);
 
            Console.WriteLine(tmpObj);
 
            Console.WriteLine(tmpObj.Test1());
            Console.WriteLine(tmpObj.Test2());
 
            Console.ReadLine();
        }
    }
}

结果:
37.5 Celsius = 99.5 Fahrenheit
Have this.: 4375 MSEL
Don't have this.: 4406 MSEL

11.可以使用抽象函数重写基类中的虚函数吗?

答:

可以

需使用 new 修饰符显式声明,表示隐藏了基类中该函数的实现

或增加 override 修饰符,表示抽象重写了基类中该函数的实现

示例:

    class BaseClass
    {
        public virtual void F()
        {
            Console.WriteLine("BaseClass.F");
        }
    }
    abstract class  DeriveClass1 : BaseClass
    {
        public abstract new void F();
    }
 
    //感谢watson hua(http://huazhihao.cnblogs.com/)的指点
    //是他提醒了我还可以用这种方法抽象重写基类的虚方法
    abstract class DeriveClass2 : BaseClass
    {
        public abstract override void F();
    }

12.密封类可以有虚函数吗?

答:

可以,基类中的虚函数将隐式的转化为非虚函数,但密封类本身不能再增加新的虚函数

示例:

    class BaseClass
    {
        public virtual void F()
        {
            Console.WriteLine("BaseClass.F");
        }
    }
    sealed class DeriveClass : BaseClass
    {
        //基类中的虚函数F被隐式的转化为非虚函数
 
        //密封类中不能再声明新的虚函数G
        //public virtual void G()
        //{
        //    Console.WriteLine("DeriveClass.G");
        //}
    }

13.什么是属性访问器?

答:

属性访问器(Property Accessor),包括 get 访问器和 set 访问器分别用于字段的读写操作

其设计目的主要是为了实现面向对象(OO)中的封装思想。根据该思想,字段最好设为private,一个精巧的类最好不要直接把字段设为公有提供给客户调用端直接访问

另外要注意属性本身并不一定和字段相联系

14.abstract 可以和 virtual 一起使用吗?可以和 override 一起使用吗?

答:

abstract 修饰符不可以和 static、virtual 修饰符一起使用

abstract 修饰符可以和 override 一起使用,参见第11点

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example14
{
    class BaseClass
    {
        public virtual void F()
        {
            Console.WriteLine("BaseClass.F");
        }
    }
    abstract class DeriveClass1 : BaseClass
    {
        //在这里, abstract是可以和override一起使用的
        public abstract override void F();
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

15.接口可以包含哪些成员?

答:

接口可以包含属性、方法、索引指示器和事件,但不能包含常量、域、操作符、构造函数和析构函数,而且也不能包含任何静态成员

16.类和结构的区别?

答:
类:

类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存

类有构造和析构函数

类可以继承和被继承

结构:

结构是值类型在栈上分配(虽然栈的访问速度比较堆要快,但栈的资源有限放),结构的赋值将分配产生一个新的对象。

结构没有构造函数,但可以添加。结构没有析构函数

结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口

示例:

根据以上比较,我们可以得出一些轻量级的对象最好使用结构,但数据量大或有复杂处理逻辑对象最好使用类。

如:Geoemtry(GIS 里的一个概论,在 OGC 标准里有定义) 最好使用类,而 Geometry 中点的成员最好使用结构

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example16
{
    interface IPoint
    {
        double X
        {
            get;
            set;
        }
        double Y
        {
            get;
            set;
        }
        double Z
        {
            get;
            set;
        }
    }
    //结构也可以从接口继承
    struct Point: IPoint
    {
        private double x, y, z;
        //结构也可以增加构造函数
        public Point(double X, double Y, double Z)
        {
            this.x = X;
            this.y = Y;
            this.z = Z;
        }
        public double X
        {
            get { return x; }
            set { x = value; }
        }
        public double Y
        {
            get { return x; }
            set { x = value; }
        }
        public double Z
        {
            get { return x; }
            set { x = value; }
        }
    }
    //在此简化了点状Geometry的设计,实际产品中还包含Project(坐标变换)等复杂操作
    class PointGeometry
    {
        private Point value;
        
        public PointGeometry(double X, double Y, double Z)
        {
            value = new Point(X, Y, Z);
        }
        public PointGeometry(Point value)
        {
            //结构的赋值将分配新的内存
            this.value = value;
        }
        public double X
        {
            get { return value.X; }
            set { this.value.X = value; }
        }
        public double Y
        {
            get { return value.Y; }
            set { this.value.Y = value; }
        }
        public double Z
       {
            get { return value.Z; }
            set { this.value.Z = value; }
        }
        public static PointGeometry operator +(PointGeometry Left, PointGeometry Rigth)
        {
            return new PointGeometry(Left.X + Rigth.X, Left.Y + Rigth.Y, Left.Z + Rigth.Z);
        }
        public override string ToString()
        {
            return string.Format("X: {0}, Y: {1}, Z: {2}", value.X, value.Y, value.Z);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Point tmpPoint = new Point(1, 2, 3);
 
            PointGeometry tmpPG1 = new PointGeometry(tmpPoint);
            PointGeometry tmpPG2 = new PointGeometry(tmpPoint);
            tmpPG2.X = 4;
            tmpPG2.Y = 5;
            tmpPG2.Z = 6;
 
            //由于结构是值类型,tmpPG1 和 tmpPG2 的坐标并不一样
            Console.WriteLine(tmpPG1);
            Console.WriteLine(tmpPG2);
 
            //由于类是引用类型,对tmpPG1坐标修改后影响到了tmpPG3
            PointGeometry tmpPG3 = tmpPG1;
            tmpPG1.X = 7;
            tmpPG1.Y = 8;
            tmpPG1.Z = 9;
            Console.WriteLine(tmpPG1);
            Console.WriteLine(tmpPG3);
 
            Console.ReadLine();
        }
    }
}

结果:
X: 1, Y: 2, Z: 3
X: 4, Y: 5, Z: 6
X: 7, Y: 8, Z: 9
X: 7, Y: 8, Z: 9

17.接口的多继承会带来哪些问题?

答:

C# 中的接口与类不同,可以使用多继承,即一个子接口可以有多个父接口。但如果两个父成员具有同名的成员,就产生了二义性(这也正是 C# 中类取消了多继承的原因之一),这时在实现时最好使用显式的声明

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example17
{
    class Program
    {
        //一个完整的接口声明示例
        interface IExample
        {
            //属性
            string P
            {
                get;
                set;
            }
            //方法
            string F(int Value);
            //事件
            event EventHandler E;
            //索引指示器
            string this[int Index]
            {
                get;
                set;
            }
        }
        interface IA
        {
            int Count { get; set;}
        }
        interface IB
        {
            int Count();
        }
        //IC接口从IA和IB多重继承
        interface IC : IA, IB
        {
        }
        class C : IC
        {
            private int count = 100;
            //显式声明实现IA接口中的Count属性
            int IA.Count
            {
                get { return 100; }
                set { count = value; }
            }
            //显式声明实现IB接口中的Count方法
            int IB.Count()
            {
                return count * count;
            }
        }
        static void Main(string[] args)
        {
            C tmpObj = new C();
 
            //调用时也要显式转换
            Console.WriteLine("Count property: {0}", ((IA)tmpObj).Count);
            Console.WriteLine("Count function: {0}", ((IB)tmpObj).Count());
 
            Console.ReadLine();
        }
    }
}

结果:
Count property: 100
Count function: 10000

18.抽象类和接口的区别?

答:

抽象类(abstract class)可以包含功能定义和实现,接口(interface)只能包含功能定义

抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性

分析对象,提炼内部共性形成抽象类,用以表示对象本质,即“是什么”

为外部提供调用或功能需要扩充时优先使用接口

19.别名指示符是什么?

答:

通过别名指示符我们可以为某个类型起一个别名

主要用于解决两个命名空间内有同名类型的冲突或避免使用冗余的命名空间

别名指示符在所有命名空间最外层定义,作用域为整个单元文件。如果定义在某个命名空间内,那么它只在直接隶属的命名空间内起作用

示例:

Class1.cs:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01
{
    class Class1
    {
        public override string ToString()
        {
            return "com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01's Class1";
        }
    }
}

Class2.cs:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02
{
    class Class1
    {
        public override string ToString()
        {
            return "com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02's Class1";
        }
    }
}

主单元(Program.cs):

using System;
using System.Collections.Generic;
using System.Text;
 
//使用别名指示符解决同名类型的冲突
//在所有命名空间最外层定义,作用域为整个单元文件
using Lib01Class1 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;
using Lib02Class2 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02.Class1;
 
namespace Example19
{
    namespace Test1
    {
        //Test1Class1在Test1命名空间内定义,作用域仅在Test1之内
        using Test1Class1 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;
 
        class Class1
        {
            //Lib01Class1和Lib02Class2在这可以正常使用
            Lib01Class1 tmpObj1 = new Lib01Class1();
            Lib02Class2 tmpObj2 = new Lib02Class2();
            //TestClass1在这可以正常使用
            Test1Class1 tmpObj3 = new Test1Class1();
        }
    }
    namespace Test2
    {
        using Test1Class2 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;
 
        class Program
        {
            static void Main(string[] args)
            {
                //Lib01Class1和Lib02Class2在这可以正常使用
                Lib01Class1 tmpObj1 = new Lib01Class1();
                Lib02Class2 tmpObj2 = new Lib02Class2();
 
                //注意这里,TestClass1在这不可以正常使用。
                //因为,在Test2命名空间内不能使用Test1命名空间定义的别名
                //Test1Class1 tmpObj3 = new Test1Class1();
                
                //TestClass2在这可以正常使用
                Test1Class2 tmpObj3 = new Test1Class2();
 
                Console.WriteLine(tmpObj1);
                Console.WriteLine(tmpObj2);
                Console.WriteLine(tmpObj3);
 
                Console.ReadLine();
            }
        }
    }
}


结果:

com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01's Class1
com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02's Class1
com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01's Class1

20.如何手工释放资源?

答:

 .NET 平台在内存管理方面提供了GC(Garbage Collection),负责自动释放托管资源和内存回收的工作。但在以下两种情况需要我们手工进行资源释放:一、由于它无法对非托管资源进行释放,所以我们必须自己提供方法来释放对象内分配的非托管资源,比如你在对象的实现代码中使用了一个COM对象;二、你的类在运行是会产生大量实例(象 GIS 中的Geometry),必须自己手工释放这些资源以提高程序的运行效率

最理想的办法是通过实现一个接口显式的提供给客户调用端手工释放对象,System 命名空间内有一个 IDisposable 接口,拿来做这事非常合适,省得我们自己再声明一个接口了

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example20
{
    class Program
    {
        class Class1 : IDisposable
        {
            //析构函数,编译后变成 protected void Finalize(),GC会在回收对象前会调用调用该方法
            ~Class1()
            {
                Dispose(false);
            }
 
            //通过实现该接口,客户可以显式地释放对象,而不需要等待GC来释放资源,据说那样会降低效率
            void IDisposable.Dispose()
            {
                Dispose(true);
            }
 
            //将释放非托管资源设计成一个虚函数,提供在继承类中释放基类的资源的能力
            protected virtual void ReleaseUnmanageResources()
            {
                //Do something...
            }
 
            //私有函数用以释放非托管资源
            private void Dispose(bool disposing)
            {
                ReleaseUnmanageResources();
 
                //为true时表示是客户显式调用了释放函数,需通知GC不要再调用对象的Finalize方法
                //为false时肯定是GC调用了对象的Finalize方法,所以没有必要再告诉GC你不要调用我的Finalize方法啦
                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }
            } 
        }
        static void Main(string[] args)
        {
            //tmpObj1没有手工释放资源,就等着GC来慢慢的释放它吧
            Class1 tmpObj1 = new Class1();
 
            //tmpObj2调用了Dispose方法,传说比等着GC来释放它效率要调一些
            //个人认为是因为要逐个对象的查看其元数据,以确认是否实现了Dispose方法吧
            //当然最重要的是我们可以自己确定释放的时间以节省内存,优化程序运行效率
            Class1 tmpObj2 = new Class1();
            ((IDisposable)tmpObj2).Dispose();
        }
    }
}

21.P/Invoke是什么?

答:

在受控代码与非受控代码进行交互时会产生一个事务(transition) ,这通常发生在使用平台调用服务(Platform Invocation Services),即P/Invoke

如调用系统的 API 或与 COM 对象打交道,通过 System.Runtime.InteropServices 命名空间

虽然使用 Interop 非常方便,但据估计每次调用事务都要执行 10 到 40 条指令,算起来开销也不少,所以我们要尽量少调用事务

如果非用不可,建议本着一次调用执行多个动作,而不是多次调用每次只执行少量动作的原则

22.StringBuilder 和 String 的区别?

答:

String 在进行运算时(如赋值、拼接等)会产生一个新的实例,而 StringBuilder 则不会。所以在大量字符串拼接或频繁对某一字符串进行操作时最好使用 StringBuilder,不要使用 String

另外,对于 String 我们不得不多说几句:

1.它是引用类型,在堆上分配内存

2.运算时会产生一个新的实例

3.String 对象一旦生成不可改变(Immutable)

3.定义相等运算符(==!=)是为了比较 String 对象(而不是引用)的值

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example22
{
    class Program
    {
        static void Main(string[] args)
        {
            const int cycle = 10000;
 
            long vTickCount = Environment.TickCount;
            String str = null;
            for (int i = 0; i < cycle; i++)
                str += i.ToString();
            Console.WriteLine("String: {0} MSEL", Environment.TickCount - vTickCount);
 
            vTickCount = Environment.TickCount;
            //看到这个变量名我就生气,奇怪为什么大家都使它呢? :)
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cycle; i++)
                sb.Append(i);
            Console.WriteLine("StringBuilder: {0} MSEL", Environment.TickCount - vTickCount);
 
            string tmpStr1 = "A";
            string tmpStr2 = tmpStr1;
            Console.WriteLine(tmpStr1);
            Console.WriteLine(tmpStr2);
            //注意后面的输出结果,tmpStr1的值改变并未影响到tmpStr2的值
            tmpStr1 = "B";
            Console.WriteLine(tmpStr1);
            Console.WriteLine(tmpStr2);
 
            Console.ReadLine();
        }
    }
}

结果:
String: 375 MSEL
StringBuilder: 16 MSEL
A
A
B
A

23.explicit 和 implicit 的含义?

答:

explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换

explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A)

implicit 表示隐式转换,如从 B -> A 只需直接赋值(A = B)

隐式转换可以让我们的代码看上去更漂亮、更简洁易懂,所以最好多使用 implicit 运算符。不过!如果对象本身在转换时会损失一些信息(如精度),那么我们只能使用 explicit 运算符,以便在编译期就能警告客户调用端

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example23
{
    class Program
    {
        //本例灵感来源于大话西游经典台词“神仙?妖怪?”--主要是我实在想不出什么好例子了
        class Immortal
        {
            public string name;
            public Immortal(string Name)
            {
                name = Name;
            }
            public static implicit operator Monster(Immortal value)
            {
                return new Monster(value.name + ":神仙变妖怪?偷偷下凡即可。。。");
            }
        }
        class Monster
        {
            public string name;
            public Monster(string Name)
            {
                name = Name;
            }
            public static explicit operator Immortal(Monster value)
            {
                return new Immortal(value.name + ":妖怪想当神仙?再去修炼五百年!");
            }
        }
        static void Main(string[] args)
        {
            Immortal tmpImmortal = new Immortal("紫霞仙子");
            //隐式转换
            Monster tmpObj1 = tmpImmortal;
            Console.WriteLine(tmpObj1.name);
 
            Monster tmpMonster = new Monster("孙悟空");
            //显式转换
            Immortal tmpObj2 = (Immortal)tmpMonster;
            Console.WriteLine(tmpObj2.name);
 
            Console.ReadLine();
        }
    }
}

结果:
紫霞仙子:神仙变妖怪?偷偷下凡即可。。。
孙悟空:妖怪想当神仙?再去修炼五百年!


24.params 有什么用?

答:

params 关键字在方法成员的参数列表中使用,为该方法提供了参数个数可变的能力

它在只能出现一次并且不能在其后再有参数定义,之前可以

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication1
{
    class App
    {
        //第一个参数必须是整型,但后面的参数个数是可变的。
        //而且由于定的是object数组,所有的数据类型都可以做为参数传入
        public static void UseParams(int id, params object[] list)
        {
            Console.WriteLine(id);
            for (int i = 0; i < list.Length; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
 
        static void Main()
        {
            //可变参数部分传入了三个参数,都是字符串类型
            UseParams(1, "a", "b", "c");
            //可变参数部分传入了四个参数,分别为字符串、整数、浮点数和双精度浮点数数组
            UseParams(2, "d", 100, 33.33, new double[] { 1.1, 2.2 });
 
            Console.ReadLine();
        }
    }
}

结果:
1
a
b
c
2
d
100
33.33
System.Double[]


25.什么是反射?

答:

反射,Reflection,通过它我们可以在运行时获得各种信息,如程序集、模块、类型、字段、属性、方法和事件

通过对类型动态实例化后,还可以对其执行操作

简单来说就是用string可以在runtime为所欲为的东西,实际上就是一个.net framework内建的万能工厂

一般用于插件式框架程序和设计模式的实现,当然反射是一种手段可以充分发挥其能量来完成你想做的任何事情(前面好象见过一位高人用反射调用一个官方类库中未说明的函数。。。)

示例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Example25Lib
{
    public class Class1
    {
        private string name;
        private int age;
 
        //如果显式的声明了无参数构造函数,客户端只需要用程序集的CreateInstance即可实例化该类
        //在此特意不实现,以便在客户调用端体现构造函数的反射实现
        //public Class1()
        //{
        //}
        public Class1(string Name, int Age)
        {
            name = Name;
            age = Age;
        }
        public void ChangeName(string NewName)
        {
            name = NewName;
        }
        public void ChangeAge(int NewAge)
        {
            age = NewAge;
        }
        public override string ToString()
        {
            return string.Format("Name: {0}, Age: {1}", name, age);
        }
    }
}

反射实例化对象并调用其方法,属性和事件的反射调用略去

using System;
using System.Collections.Generic;
using System.Text;
 
//注意添加该反射的命名空间
using System.Reflection;
 
namespace Example25
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载程序集
            Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll");
 
            //遍历程序集内所有的类型,并实例化
            Type[] tmpTypes = tmpAss.GetTypes();
            foreach (Type tmpType in tmpTypes)
            {
                //获取第一个类型的构造函数信息
                ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
                foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
                {
                    //为构造函数生成调用的参数集合
                    ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters(); 
                    object[] tmpParams = new object[tmpParamInfos.Length];
                    for (int i = 0; i < tmpParamInfos.Length; i++)
                    {
                        tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
                        if (tmpParamInfos[i].ParameterType.FullName == "System.String")
                        {
                            tmpParams[i] = "Clark";
                        }
                    }
 
                    //实例化对象
                    object tmpObj = tmpConsInfo.Invoke(tmpParams);
                    Console.WriteLine(tmpObj);
 
                    //获取所有方法并执行
                    foreach (MethodInfo tmpMethod in tmpType.GetMethods())
                    {
                        //为方法的调用创建参数集合
                        tmpParamInfos = tmpMethod.GetParameters();
                        tmpParams = new object[tmpParamInfos.Length];
                        for (int i = 0; i < tmpParamInfos.Length; i++)
                        {
                            tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
                            if (tmpParamInfos[i].ParameterType.FullName == "System.String")
                            {
                                tmpParams[i] = "Clark Zheng";
                            }
                            if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")
                            {
                                tmpParams[i] = 27;
                            }
                        }
                        tmpMethod.Invoke(tmpObj, tmpParams);
                    }
 
                    //调用完方法后再次打印对象,比较结果
                    Console.WriteLine(tmpObj);
                }
            }
 
            Console.ReadLine();
        }
    }
}

结果:
Name: Clark, Age: 0
Name: Clark Zheng, Age: 27

示例下载:http://www.cnblogs.com/Files/reonlyrun/CSharp25QExample07.rar

如果你认为还有哪些概念比较重要或容易混淆,可以在回复中提出,我会及时更新这篇随笔

Tuesday, July 03, 2007 6:41:39 PM UTC  #    Comments [0]    |  Trackback
 Thursday, April 05, 2007

http://en.csharp-online.net/BCL_Generics%E2%80%94S...
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace SortedListTest
{
    public class Customer
    {
        private int id;
        private string name;
        private string category;
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                if (id == value)
                    return;
                id = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name == value)
                    return;
                name = value;
            }
        }
        public string Category
        {
            get
            {
                return category;
            }
            set
            {
                if (category == value)
                    return;
                category = value;
            }
        }

        /// <summary>
        /// Creates a new instance of customer
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="category"></param>
        public Customer(int id, string name, string category)
        {
            this.id = id;
            this.name = name;
            this.category = category;
        }

        public override string ToString()
        {
            return string.Format("{0}-{1}-{2}", id, name, category);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> collCustList =
                new List<Customer>();
            collCustList.Add(new Customer(99,
               "Happy Gillmore", "Platinum"));
            collCustList.Add(new Customer(77,
               "Billy Madison", "Gold"));
            collCustList.Add(new Customer(55,
               "Bobby Boucher", "Gold"));
            collCustList.Add(new Customer(88,
               "Barry Egan", "Platinum"));
            collCustList.Add(new Customer(11,
               "Longfellow Deeds", "Other"));

            Console.Out.WriteLine("Before:");
            foreach (Customer cust in collCustList)
                Console.Out.WriteLine(cust);

            collCustList.Sort
               (delegate(Customer cust1,
               Customer cust2)
            {
                return Comparer<int>.Default.Compare
                   (cust1.Id, cust2.Id);
            });

            Console.Out.WriteLine("After:");
            foreach (Customer cust in collCustList)
                Console.Out.WriteLine(cust);

            collCustList.Reverse();

            Console.Out.WriteLine("Reversed:");
            foreach (Customer cust in collCustList)
                Console.Out.WriteLine(cust);

            Console.In.ReadLine();
        }

   
    }
}
Thursday, April 05, 2007 9:47:57 PM UTC  #    Comments [0]    |  Trackback
 Monday, April 02, 2007
 Tuesday, March 27, 2007

Normally you cannot get the size of an object in the managed heap. This is because that size doesn't make sense in the managed world due to different reasons. One of them could be that the size of the same object is not guaranteed to be the same between two starts of the application. The JIT compiler is free to layout the objects as it sees fit. The layouting can be different due to different configuration of the machine, different version of the CLR, etc. Furthermore, your type may reference other reference types. Even two objects can cross-reference the same object. How can you possibly tell what is the size of the object Using Marshal.SizeOf returns the size of the marshaled (unmanaged)
equivalent of the provided type. I said "normally" back then because there is an operator that returns the managed size for a type (only for valuetypes). In C# it is represented by the *sizeof* operator. However *sizeof* operator has some limitations in addition to its syntax I don't think that is what you need.

About sizeof operator you can read at MSDN
ms-help://MS.MSDNQTR.2003FEB.1033/csref/html/vclrfSizeofPG.htm
To demonstrate the difference between Marshal.SizeOf and *sizeof* operator run the following simple code

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
unsafe static void Main(string[] args)
{
Console.WriteLine("Managed size: {0}", sizeof(char));
Console.WriteLine("Unmanaged size: {0}", Marshal.SizeOf(typeof(char)));
Console.ReadLine();
}
}


The result is:
Managed size: 2
Unmanaged size: 1

Unmanaged size is 1 because System.Char struct is set to be marshaled as ANSI char by default.

Tuesday, March 27, 2007 9:57:14 PM UTC  #    Comments [0]    |  Trackback
 Monday, March 12, 2007

// XMLsample.cs
// compile with: /doc:XMLsample.xml
using System;

/// <summary>
/// Class level summary documentation goes here.</summary>
/// <remarks>
/// Longer comments can be associated with a type or member 
/// through the remarks tag</remarks>
public class SomeClass
{
   /// <summary>
   /// Store for the name property</summary>
   private string myName = null;

   /// <summary>
   /// The class constructor. </summary>
   public SomeClass()
   {
       // TODO: Add Constructor Logic here
   }
   
   /// <summary>
   /// Name property </summary>
   /// <value>
   /// A value tag is used to describe the property value</value>
   public string Name
   {
      get 
      {
         if ( myName == null )
         {
            throw new Exception("Name is null");
         }
             
         return myName;
      }
   }

   /// <summary>
   /// Description for SomeMethod.</summary>
   /// <param name="s"> Parameter description for s goes here</param>
   /// <seealso cref="String">
   /// You can use the cref attribute on any tag to reference a type or member 
   /// and the compiler will check that the reference exists. </seealso>
   public void SomeMethod(string s)
   {
   }

   /// <summary>
   /// Some other method. </summary>
   /// <returns>
   /// Return results are described through the returns tag.</returns>
   /// <seealso cref="SomeMethod(string)">
   /// Notice the use of the cref attribute to reference a specific method </seealso>
   public int SomeOtherMethod()
   {
      return 0;
   }

   /// <summary>
   /// The entry point for the application.
   /// </summary>
   /// <param name="args"> A list of command line arguments</param>
   public static int Main(String[] args)
   {
      // TODO: Add code to start application here

       return 0;
   }
}
Monday, March 12, 2007 10:28:03 PM UTC  #    Comments [0]    |  Trackback
Copyright © 2010 Kevin Mocha. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: