「C#」structで構造体を定義するサンプル

構文
[修飾子] struct <構造体名>{
public string 変数1;
public int 変数2;
}
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
namespace TestDemo
{
class Program
{
//構造体を定義
public struct Emp
{
public string username;
public int age;
public string tel;
}
static void Main(string[] args)
{
//構造体を生成して値をセット
Emp u = new Emp();
u.username = "山田";
u.age = 24;
u.tel = "03-56787";
//User.nameの値を表示
Console.WriteLine(u.username);
Console.WriteLine(u.age);
}
}
}
using System; namespace TestDemo { class Program { //構造体を定義 public struct Emp { public string username; public int age; public string tel; } static void Main(string[] args) { //構造体を生成して値をセット Emp u = new Emp(); u.username = "山田"; u.age = 24; u.tel = "03-56787"; //User.nameの値を表示 Console.WriteLine(u.username); Console.WriteLine(u.age); } } }
using System;

namespace TestDemo
{
  class Program
  {
  
    //構造体を定義
   public struct Emp
   {
     public string username;
     public int age;
     public string tel;
   }
  
    static void Main(string[] args)
    {
     //構造体を生成して値をセット
     Emp u = new Emp();
     u.username = "山田";
     u.age = 24;
     u.tel = "03-56787";

     //User.nameの値を表示
     Console.WriteLine(u.username);
     Console.WriteLine(u.age);
    }
  }
}

実行結果
山田
24

 

C#

Posted by arkgame