/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Copy a struct.
using System;
// Define a structure.
struct MyStruct {
public int x;
}
// Demonstrate structure assignment.
public class StructAssignment {
public static void Main() {
MyStruct a;
MyStruct b;
a.x = 10;
b.x = 20;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
a = b;
b.x = 30;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Demonstrate a structure
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate a structure.
using System;
// Define a structure.
struct Book {
public string author;
public string title;
public int copyright;
public Book(string a, string t, int c) {
author = a;
title = t;
copyright = c;
}
}
// Demonstrate Book structure.
public class StructDemo1 {
public static void Main() {
Book book1 = new Book("Herb Schildt",
"C# A Beginner's Guide",
2001); // explicit constructor
Book book2 = new Book(); // default constructor
Book book3; // no constructor
Console.WriteLine(book1.title + " by " + book1.author +
", (c) " + book1.copyright);
Console.WriteLine();
if(book2.title == null)
Console.WriteLine("book2.title is null.");
// now, give book2 some info
book2.title = "Brave New World";
book2.author = "Aldous Huxley";
book2.copyright = 1932;
Console.Write("book2 now contains: ");
Console.WriteLine(book2.title + " by " + book2.author +
", (c) " + book2.copyright);
Console.WriteLine();
// Console.WriteLine(book3.title); // error, must initialize first
book3.title = "Red Storm Rising";
Console.WriteLine(book3.title); // now OK
}
}
Define struct and use it
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
public struct MyStruct
{
public int MyInt;
public long MyLong;
public string MyString;
}
public class StructsChapter_3___Structs__Enums__Arrays_and_Classes
{
static void Main(string[] args)
{
MyStruct TheStruct;
TheStruct.MyInt = 0;
TheStruct.MyLong = 0;
TheStruct.MyString = "Hello World";
}
}
}
Structs And Enums
using System;
using System.Collections.Generic;
using System.Text;
struct Date {
public Date(int ccyy, Month mm, int dd) {
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
public override string ToString() {
return this.month + " " + (this.day + 1) + " " + (this.year + 1900);
}
private int year;
private Month month;
private int day;
}
enum Month {
January, February, March, April,
May, June, July, August,
September, October, November, December
}
class Program {
static void Entrance() {
Month first = Month.December;
Console.WriteLine(first);
first++;
Console.WriteLine(first);
Date defaultDate = new Date();
Console.WriteLine(defaultDate);
Date halloween = new Date(2008, Month.October, 31);
Console.WriteLine(halloween);
}
static void Main() {
try {
Entrance();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
Use static method to initialize field
using System;
internal class MyClass {
public int iField1 = FuncA();
public int iField2 = FuncC();
public int iField3 = FuncB();
public static int FuncA() {
Console.WriteLine("MyClass.FuncA");
return 0;
}
public static int FuncB() {
Console.WriteLine("MyClass.FuncB");
return 1;
}
public static int FuncC() {
Console.WriteLine("MyClass.FuncC");
return 2;
}
}
public class Starter {
public static void Main() {
MyClass obj = new MyClass();
}
}
Demonstrates use of static constructor
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// SysInfo.cs -- Demonstrates use of static constructor
//
// Compile this program with the following command line:
// C:>csc SysInfo.cs
//
namespace nsSysInfo
{
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class SysInfo
{
static public void Main()
{
Console.WriteLine ("Current user is " +
clsSystemInfo.User);
Console.WriteLine ("Current Time Zone is " +
clsSystemInfo.TZ);
Console.WriteLine ("Current domain is " +
clsSystemInfo.Domain);
Console.WriteLine ("Current Host is " +
clsSystemInfo.Host);
Console.WriteLine ("Command interpreter is " +
clsSystemInfo.ComSpec);
}
}
class clsSystemInfo
{
private clsSystemInfo () {}
[DllImport ("kernel32.dll")]
static extern public long GetEnvironmentVariable (string name,
byte [] value, long size);
static clsSystemInfo ()
{
m_User = SystemInformation.UserName;
m_Host = SystemInformation.ComputerName;
DateTime now = DateTime.Now;
TimeZone tz = TimeZone.CurrentTimeZone;
m_TimeZone = tz.IsDaylightSavingTime(now)
? tz.DaylightName : tz.StandardName;
m_Domain = SystemInformation.UserDomainName;
byte [] comspec = new byte [256];
if (GetEnvironmentVariable ("COMSPEC", comspec, 256) > 0)
{
foreach (byte ch in comspec)
{
if (ch == 0)
break;
m_ComSpec += (char) ch;
}
}
}
static public string User
{
get
{
return (m_User);
}
}
static public string TZ
{
get
{
return (m_TimeZone);
}
}
static public string Domain
{
get
{
return (m_Domain);
}
}
static public string Host
{
get
{
return (m_Host);
}
}
static public string ComSpec
{
get
{
return (m_ComSpec);
}
}
private static string m_User;
private static string m_TimeZone;
private static string m_Domain;
private static string m_Host;
private static string m_ComSpec;
}
}
Demonstrates how a static field is shared by multiple instances of a class
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Static.cs — Demonstrates how a static field is shared by
// multiple instances of a class.
//
// Compile this program with the following command line:
// C:>csc Static.cs
//
namespace nsStatic
{
using System;
public class clsMainStatic
{
static public void Main ()
{
for (int i = 0; i < 20; ++i)
{
clsStatic inst = new clsStatic();
}
Console.WriteLine ("Created {0} instance of clsStatic",
clsStatic.Count);
}
}
class clsStatic
{
static public int Count
{
get {return (m_Count);}
}
static private int m_Count = 0;
public clsStatic ()
{
++m_Count;
}
}
}
[/csharp]