Answers for "class in c#"

C#
4

class in c#

public class Car
{  
    // fields
    public bool isDriving = false;
    
    // constructor
    public Car( string make )
    {
        Make = make;
    }
	
    // properties
    private string _make = string.Empty;
    public string Make
    {
        get { return _make; }
        set { _make = value; } 
    }
    
    // methods
    public void drive()
    {
        if( isDriving )
        {
			// Car is already moving
        }
        else
		{
            // start driving the car
            isDriving = true;
    	}        
    }

    public void stop()
    {
		if( isDriving )
		{
			// stop the car 
			isDriving = false;
		}
        else
        {
			// car is already not moving
        }        
    }      
}

// ---
// An example of using this class in a console app

using System;
					
public class Program
{
	public static void Main()
	{		
        // construct a new class of type Car and set the Make
      	// property to "VW" using the constructor.
		Car newCar = new Car( "VW" );
		
      	// display the make of our new car
		Console.WriteLine( newCar.Make );
		
      	// call the drive method of the car class
		newCar.drive();		
		
      	// display the value of the isDriving property to
		Console.WriteLine( newCar.isDriving );
		
      	// call the stop method of the car class
		newCar.stop();
		
      	// display the value of the isDriving property
		Console.WriteLine( newCar.isDriving );
	}
}

// the class 
public class Car
{  
    // fields
    public bool isDriving = false;
    
    // constructor w
    public Car( string make )
    {
        Make = make;
    }
	
    // properties
    private string _make = string.Empty;
    public string Make
    {
        get { return _make; }
        set { _make = value; } 
    }
    
    // methods
    public void drive()
    {
        if( isDriving )
        {
        		// Car is already moving
        }
        else
		{
            // start driving the car
            isDriving = true;
    	}        
    }

    public void stop()
    {
		if( isDriving )
		{
			// stop the car 
			isDriving = false;
		}
        else
        {
			// car is already not moving
        }        
    }      
}
Posted by: Guest on February-18-2020
1

C# Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyClass
{
	public class MyClass
	{
      public string MyString;
      public string MyNewString { get; set; }
      string MyThirdString = "Number Three";
      private const int K = 9;
      
      public static Int32 MyStatic;
      public static int MyNewStatic;
        #region
          readoly string JustaString;
          #endregion 
      double MyDouble = 4.68;
    }
}
Posted by: Guest on June-29-2021
1

creating a class in c#

//Declaring an object of type MyClass.
MyClass mc = new MyClass();

//Declaring another object of the same type, assigning it the value of the first object.
MyClass mc2 = mc;
Posted by: Guest on June-24-2021
1

classes c#

public class x
    {
		(condition)
    }
Posted by: Guest on December-09-2020
1

how to create class in C#

class ClassName
{
  
}
Posted by: Guest on November-11-2020
0

c# classes

using System;

class Book
  {
    public string title;
    public string author;
    public int pages;
  }


class MainClass {
  public static void Main (string[] args) {
    Book book1 = new Book();
    book1.title = "Harry Potter";
    book1.author = "JK Rowling";
    book1.pages = 400;
    Console.WriteLine(book1.title);
  }
}
Posted by: Guest on March-17-2021

C# Answers by Framework

Browse Popular Code Answers by Language