/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A simple property example. using System; class SimpProp { int prop; // field being managed by myprop public SimpProp() { prop = 0; } /* This is the property that supports access to the private instance variable prop. It allows only positive values. */ public int myprop { get { return prop; } set { if(value >= 0) prop = value; } } } // Demonstrate a property. public class PropertyDemo { public static void Main() { SimpProp ob = new SimpProp(); Console.WriteLine("Original value of ob.myprop: " + ob.myprop); ob.myprop = 100; // assign value Console.WriteLine("Value of ob.myprop: " + ob.myprop); // Can't assign negative value to prop Console.WriteLine("Attempting to -10 assign to ob.myprop"); ob.myprop = -10; Console.WriteLine("Value of ob.myprop: " + ob.myprop); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
enum based attribute
using System; public enum RemoteServers { A, B, C } public class RemoteObjectAttribute : Attribute { public RemoteObjectAttribute(RemoteServers Server) { this.server = Server; } protected RemoteServers server; public string Server { get { return RemoteServers.GetName( typeof(RemoteServers), this.server); } } } [RemoteObject(RemoteServers.C)] class MyRemotableClass { } class Test { [STAThread] static void Main(string[] args) { Type type = typeof(MyRemotableClass); foreach (Attribute attr in type.GetCustomAttributes(true)) { RemoteObjectAttribute remoteAttr = attr as RemoteObjectAttribute; if (null != remoteAttr) { Console.WriteLine(remoteAttr.Server); } } } }
Override Properties
using System; using System.Collections; abstract class Employee { protected Employee(int employeeId, int hoursWorked) { this.employeeId = employeeId; HoursWorked = hoursWorked; } protected int employeeId; public int EmployeeId { get { return employeeId; } } protected int HoursWorked; protected double hourlyCost = -1; // dummy init value public abstract double HourlyCost { get; } } class ContractEmployee : Employee { public ContractEmployee(int employeeId, double hourlyWage, int hoursWorked) : base(employeeId, hoursWorked) { HourlyWage = hourlyWage; } protected double HourlyWage; public override double HourlyCost { get { return HourlyWage; } } } class SalariedEmployee : Employee { public SalariedEmployee(int employeeId, double salary, int hoursWorked) : base(employeeId, hoursWorked) { Salary = salary; } protected double Salary; public override double HourlyCost { get { return (Salary / 52) / HoursWorked; } } } class OverrideProperties { public static ArrayList employees = new ArrayList(); public static void PrintEmployeesHourlyCostToCompany() { foreach (Employee employee in employees) { Console.WriteLine("{0} employee (id={1}) costs {2}" + " per hour", employee, employee.EmployeeId, employee.HourlyCost); } } public static void Main() { ContractEmployee c = new ContractEmployee(1, 50, 40); employees.Add(c); SalariedEmployee s = new SalariedEmployee(2, 100000, 65); employees.Add(s); PrintEmployeesHourlyCostToCompany(); Console.ReadLine(); } }
MyClass class that contains a private courseName instance variable, and a property to get and set its value.
using System; public class MyClass { private string courseName; public string CourseName { get { return courseName; } set { courseName = value; } } public void DisplayMessage() { Console.WriteLine( CourseName ); } } public class MyClassTest { public static void Main( string[] args ) { MyClass myMyClass = new MyClass(); Console.WriteLine( "Initial course name is: '{0}' ",myMyClass.CourseName ); Console.WriteLine( "Please enter the course name:" ); string theName = Console.ReadLine(); myMyClass.CourseName = theName; Console.WriteLine(); myMyClass.DisplayMessage(); } }
Using variable-length argument lists.
using System; public class VarargsTest { public static double Average( params double[] numbers ) { double total = 0.0; // initialize total foreach ( double d in numbers ) total += d; return total / numbers.Length; } public static void Main( string[] args ) { double d1 = 10.0; double d2 = 20.0; double d3 = 30.0; double d4 = 40.0; Console.WriteLine("d1 = {0:F1} d2 = {1:F1} d3 = {2:F1} d4 = {3:F1} ",d1, d2, d3, d4 ); Console.WriteLine( "Average of d1 and d2 is {0:F1}",Average( d1, d2 ) ); Console.WriteLine( "Average of d1, d2 and d3 is {0:F1}",Average( d1, d2, d3 ) ); Console.WriteLine( "Average of d1, d2, d3 and d4 is {0:F1}",Average( d1, d2, d3, d4 ) ); } }
Test Polymorphism Virtual Functions
/* A Programmer's Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3 */ // 01 - Object-Oriented BasicsPolymorphism and Virtual Functions // copyright 2000 Eric Gunnerson using System; public PolymorphismVirtualFunctions { public static void CallPlay(MusicServer ms) { ms.Play(); } public static void Main() { MusicServer ms = new WinAmpServer(); CallPlay(ms); ms = new MediaServer(); CallPlay(ms); } } public abstract class MusicServer { public abstract void Play(); } public class WinAmpServer: MusicServer { public override void Play() { Console.WriteLine("WinAmpServer.Play()"); } } public class MediaServer: MusicServer { public override void Play() { Console.WriteLine("MediaServer.Play()"); } }
Method override 3
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; class Dog { private int weight; // constructor public Dog(int weight) { this.weight = weight; } // override Object.ToString public override string ToString() { return weight.ToString(); } } public class TesterOverride { static void Main() { int i = 5; Console.WriteLine("The value of i is: {0}", i.ToString()); Dog milo = new Dog(62); Console.WriteLine("My dog Milo weighs {0} pounds", milo.ToString()); } }