C# Make everything following public / private like

2019-02-16 22:54发布

I recently started learning C#, but I have some background in C++. I was wondering how I would do something like

class employee
{
    public:
       ....
  ... methods ...
       ....

    private:
       ....
    ... private member variables ....
       ....
}

I tried doing this in C#, but it doesn't like the "public: ..." and "private: ..." to make everything after it either public or private.

Also, I've seen that theres this get and set thing in C#, so you don't need to do things in the way of making a private member variable, then making a function to return that variable?

While i'm at it, how does one make subclasses in C#? In C# new classes get opened in different tabs, so I'm confused as to how I would do it.

4条回答
疯言疯语
2楼-- · 2019-02-16 23:11

You can't make "blocks" public or private in C# as you would in C++, you'll have to add the visibility (and implementation) to each member. In C++, you'd normally do;

public:
  memberA();
  memberB();
private:
  memberC();

...and implement your members elsewhere, while in C#, you'd need to do;

public  memberA() { ...implement your function here... }
public  memberB() { ...implement your function here... }
private memberC() { ...implement your function here... }

As for properties, see them as auto implemented set and get methods which you can choose to implement yourself or have the compiler implement them. If you want to implement them yourself, you'll still need the field to store your data in, if you leave it up to the compiler, it will also generate the field.

Inheritance works exactly the same as it would if you put things in the same file (which is probably not even a good idea for bigger C++ projects). Just inherit as usual, as long as you're in the same namespace or have the namespace of the base class imported, you can just inherit seamlessly;

using System.Collections;  // Where IEnumerable is defined

public class MyEnumerable : IEnumerable {  // Just inherit like it 
   ...                                     // was in the same file.
}
查看更多
仙女界的扛把子
3楼-- · 2019-02-16 23:14
  1. No, you can't. In C# you must specify accessor for each member.

  2. No you don't, it's called Property

  3. Write it other class

    class SomeClass {

    } class SubClass:SomeClass {}

查看更多
Deceive 欺骗
4楼-- · 2019-02-16 23:35

1) Access modifiers in C# are different from C++ in that you need to explicitly specify one per class member.

http://msdn.microsoft.com/en-us/library/wxh6fsc7(v=vs.71).aspx

2) The get, set you are mentioning refer to C# Properties:

class User
{
    private string userName;

    public string UserName
    {
        get { return this.userName; }
        set { this.userName = value; }
    }
}

Pls note that you can also use auto-implemented properties http://msdn.microsoft.com/en-us/library/bb384054.aspx

3) Subclassing in C# is done like so

class Manager : Employee 
{
    //implementation goes here as usual
}
查看更多
地球回转人心会变
5楼-- · 2019-02-16 23:36

in C# you'll have to specify the access specifier for each method or property. If you don't specify it takes the default access specifier.
Default access specifier for class members are private and default for things outside a class is internal

class MyClass{ //MyClass will be internal
     int MyProperty {get; set;} // MyProperty will be privare
}
查看更多
登录 后发表回答