65.9K
CodeProject 正在变化。 阅读更多。
Home

.NET 常见问题 (小而有效)

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.26/5 (37投票s)

2008年10月14日

CPOL

3分钟阅读

viewsIcon

37738

downloadIcon

204

小而重要的问题

引言

作为人类,我们倾向于忽略小事情。我们专注于实现目标,在此过程中,我们会错过生活中一些非常重要的事情。在这里,我尝试上传一些 .Net 常见问题。即使这些问题很简短,我认为它们也会有效。我希望这对我们所有人都有帮助。我会尝试不断添加问题。如果您有一些常见问题要添加,请发送给我。

.NET 常见问题

我们是否能够在 .NET 中定义空接口(没有任何方法签名)?

答案:是的,在 .NET 中可以存在空接口。

public partial class EmptyInterFace : System.Web.UI.Page, IEmptyInterface
{
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Write("Working......... Empty Interface is possible.");
    }
}

interface IEmptyInterface
{
    //Empty Intetrface
}

抽象类是否可以没有抽象函数?

答案:是的,在 .NET 中,抽象类可以没有抽象函数。

public partial class AbstractClass_ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(
            "Working.........abstract Class is possible without abstract function.");        
    }
}
// Abstract class without any member function
abstract class MyAbstractClassEmpty
{

}
// Abstract class without Abstract function, having simple member function.
abstract class MyAbstractClassWithFunction
{
    public string MyFun()
    {
        return ("I Enjoyed..........");
    }
}

Try & Finally 块是否可以没有 catch 块?

答案:是的,try 和 catch 块可以没有 catch 块。

public partial class TryAndFinallyWithoutCatch_ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // try and finally Block without catch block.
        try
        {
            Response.Write("It's Working..........");
        }
        finally
        {
            Response.Write(" Catch Block Not Necessary With Try & Finally.");
        }
    }
}

单个 try 块是否可以有多个 catch 块?

答案:是的,我们可以在单个 try 块中使用多个 catch 块。但是,每个 catch 块的异常类型应该是唯一的。更多信息请参见下面的代码

public partial class MultipleCatchBlockWithSingleTry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            // We can use it "To Handle different type of Exception."
        }
        catch (ArrayTypeMismatchException ex)
        {
            // Like here we handle ArrayTypeMismatchException exception.
        }
        catch (IndexOutOfRangeException ex)
        {
            // Like here we handle IndexOutOfRangeException exception.
        }
        finally
        {
            Response.Write("Yes, Multiple catch block is possible with 
      single try block.");
        }
 
       /*// It will not Work. 
        try
        {
            //In each Catch Block we need to Mention the Exception type 
            //if we have multiple catch block.
        }
        catch
        {
            //Each Catch block Exception type should be unique
        }
        catch 
        {
            //Each Catch block Exception type should be unique
        }*/
    }
}

在 try, catch 块中,如何跳过 finally 块?

答案:根据我的理解,在 .net 中没有办法跳过 finally 块。如果您不想使用 finally 块,则无需编写 finally 块。

public partial class SkipFinallyBlock : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string str = 
            "Even exception occur again in catch block still finally block is Working";
        try
        {            
            throw new IndexOutOfRangeException();
        }
        catch
        {
            throw new IndexOutOfRangeException();
        }
        finally
        {
            Response.Redirect("Error.aspx?str="+ str);
        }
    }
}
注意:运行代码时,如果发生 IndexOutOfRangeException,请按继续按钮。这里我想展示即使在 catch 块中出现异常,finally 块也会执行。

如何为具有相同函数名和相同签名的两个接口实现一个公共函数?

答案:请参见下面的代码。

public partial class CommonFunctionImplementationForMultipleInterface : 
    System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        myClass obj = new myClass();
        Response.Write(obj.MyFun());
    }
}

 interface MyInterface
{
    String MyFun();
}
interface YourInterface
{
    String MyFun();
}

public class myClass : MyInterface, YourInterface
{
    public String MyFun()
    {
        return ("Wow..... This is Common Method for Multiple Interface");
    }
}

在 C# 中开发一个程序集,该程序集包含一个类,该类具有两个具有相同名称和相同签名的函数,但第一个函数名是小写,第二个函数名是大写。即:

public class TestClass
 {
     public string myfunction()
     {
         return ("lower case function");
     }
     public string MYFUNCTION()
     {
         return ("UPPER CASE FUNCTIOM");
     }
 }

在 VB.Net 中使用上面的程序集并调用 CSharp 类 Hello 函数。将调用哪个函数(hello 或 HELLO)?

答案:请参见下面的 VB.NET 代码。

Partial Class CSharpAssemblyUsedInVB
    Inherits System.Web.UI.Page
 
    Dim obj As New SameSignatureFunctions.TestClass
    Dim val As Object 
 
    Protected Sub Page_Load(ByVal sender As Object,
        ByVal e As System.EventArgs) Handles Me.Load
        val = obj.GetType.InvokeMember("myfunction", _
              System.Reflection.BindingFlags.Instance Or _
              System.Reflection.BindingFlags.Public Or System.Reflection.BindingFlags.InvokeMethod, _
              Nothing, obj, Nothing, Nothing, Nothing, Nothing)
        Response.Write(val)
    End Sub
End Class

待续........

如果您有任何想要添加的问题,请发送邮件至 Dheerajindian@gmail.com。

查找更多 VS.NET 问题

有关更多 Dot Net 问题,您可以访问以下链接。

http://msdotnetsupport.blogspot.com/2007/02/biztalk-server-common-questions-and.html
http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html
http://msdotnetsupport.blogspot.com/2006/08/net-windows-forms-interview-questions.html
http://msdotnetsupport.blogspot.com/2006/08/net-remoting-interview-questions.html
http://msdotnetsupport.blogspot.com/2006/08/c-interview-questions.html
http://msdotnetsupport.blogspot.com/2006/08/aspnet-interview-questions.html
http://msdotnetsupport.blogspot.com/2007/02/net-server-server-adonet-assembly_13.html
http://www.syncfusion.com/faq/aspnet/default.aspx
http://www.aspnetfaq.com
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4081&lngWId=10
http://blogs.crsw.com/mark/articles/254.aspx
http://blog.daveranck.com/archive/2005/01/20/355.aspx
http://www.techinterviews.com/?p=176
http://www.techinterviews.com/?p=193
http://www.dotnetspider.com
http://basittanveer.blogspot.com/2006/05/aspnet-interview-questions.html
http://groups.msn.com/MumbaiUserGroup/aspnetfaqs.msnwx
http://www.c-sharpcorner.com/faq.asp
http://aspalliance.com/891
http://forums.aspfree.com/attachment.php?attachmentid=459
http://forums.aspfree.com/attachment.php?attachmentid=460
http://forums.aspfree.com/attachment.php?attachmentid=461
http://www.toqc.com/entropy/TheAnswers1.html
http://www.toqc.com/entropy/TheAnswers2.html
http://www.akaas.net/jobs/asp-net-interview-questions.htm
http://www.akaas.net/dot-net-faqs.htm
http://www.akaas.net/jobs/asp-net-interview-questions.htm
http://moredotnet.googlepages.com
http://www.interviewcorner.com/
http://www.kyapoocha.com
http://www.coolinterview.com/
http://www.geekinterview.com
http://aspnetinfo.googlepages.com
http://dotnet-question-answer.blogspot.com/
http://vikasnetdev.blogspot.com/2006/06/nice-interview-questions-found-in-job.html
http://www.mytechsky.com
http://www.dotnetquestion.info/dot_net/interview.htm
http://moredotnet.googlepages.com
http://www.coolinterview.com/
http://aspalliance.com/891_Microsoft_NET_Terminologies_at_a_Glance
http://aspalliance.com/929_Operating_Systems_Concepts_and_Terminologies
http://www.techinterviews.com/?p=50
http://www.kyapoocha.com/category/aspnet-interview-questions/
http://dev.fyicenter.com/interview/dotnet.html
http://www.megasolutions.net/kb/ASP_Net_InterView_Questions_1a.aspx

     祝您求职顺利...........


© . All rights reserved.