// A skeleton of a C# program
using System;
namespace MyNamespace
{
class MyClass{}
struct MyStruct{}
interface IMyInterface {}
delegate int MyDelegate();
enum MyEnum {}
namespace MyNestedNamespace{struct MyStruct{}}
class MyMainClass { static void Main(string[] args)
{/*My program starts here...*/}
}
}
/***********************
Example
************************/
public class Person //A class defines a type of object, but it is not an object itself
{
// Field
public string name; //public : anyone can create objects from this class.
// Constructor that takes no arguments.
public Person()
{
name = "unknown";
}
// Constructor that takes one argument.
public Person(string nm)
{
name = nm;
}
// Method
public void SetName(string newName)
{
name = newName;
}
}
class TestPerson
{
static void Main()
{
// Call the constructor that has no parameters.
/*Create Objects. An object is a concrete entity based on a class, */
Person person1 = new Person(); //Objects can be created by using the new keyword
Console.WriteLine(person1.name);
person1.SetName("John Smith");
Console.WriteLine(person1.name);
// Call the constructor that has one parameter.
Person person2 = new Person("Sarah Jones");
Console.WriteLine(person2.name);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
// Output:
// unknown
// John Smith
// Sarah Jones
沒有留言:
張貼留言