以前用VB,现在开始学C#,菜鸟问题请大家不要见笑。
private string test(string a)
{
   test="hello:" + a;
}
然后调用它
string  a="name";
string  b=test(a);
我原指望b能得到 hello: name ,但提示出错,说在不带括号的情况下引用了......
在VB 中类似的写法是可以得到结果的,我不知在C# 中该怎么写?

解决方案 »

  1.   

    private string test(string a)
    {
       return "hello:" + a;
    }
      

  2.   

    private string test(string a)
    {
      return test="hello:" + a;
    }
    你只有计算结果,而没有返回,加个RETURN 即可
      

  3.   

    private string test(string a)
    {
       test="hello:" + a;
    }
    在C#中叫做方法。string 是这个方法的返回类型。要想得到返回值就必须要使用return.
    private string test(string a)
    {
       return "hello:" + a;
    }
    按上面这样写就可以得到你想要的结果了。
      

  4.   

    也不见得非得需要返回值private void test(string a)
    {
       test="hello:" + a;
    }
    string test;
    string  a="name";
    b=test(a);
    此时,test已经被赋值变成了:test="hello: name ";
    Response.Write(test);
      

  5.   

    你test变量都没有定义嘛,也没有返回值,改为下面试看看private string test(string a)
    {
    strin test;
    test = "hello:"+a;
    return test;
    }