/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
namespace Client.Chapter_1___Common_Type_System
{
public class UsingFloatsChapter_1___Common_Type_System {
static void Main(string[] args)
{
float MyFloat = 3.281f;
double MyDouble = 5E-02;
}
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Int and float value
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class IntFloatTester
{
public static void Main()
{
int smallInt = 5;
int largeInt = 12;
int intQuotient;
intQuotient = largeInt / smallInt;
Console.WriteLine("Dividing integers. {0} / {1} = {2}",
largeInt, smallInt, intQuotient);
float smallFloat = 5;
float largeFloat = 12;
float FloatQuotient;
FloatQuotient = largeFloat / smallFloat;
Console.WriteLine("Dividing floats. {0} / {1} = {2}",
largeFloat, smallFloat, FloatQuotient);
}
}
Format a float number in ToString method
using System;
public class MainClass {
public static void Main() {
Console.WriteLine(10.3.ToString("C", null)); // $10.30
}
}
Get Enum Description
//http://facebooktoolkit.codeplex.com/
//http://facebooktoolkit.codeplex.com/license
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
namespace Facebook.Utility
{
///<summary>
/// Contains helper functions used to help when using enums inside of collections and serialization
///</summary>
public class EnumHelper
{
private EnumHelper()
{
}
///<summary>
///</summary>
///<param name="enumeratedType"></param>
///<typeparam name="T"></typeparam>
///<returns></returns>
///<exception cref="ArgumentException"></exception>
public static string GetEnumDescription<T>(T enumeratedType)
{
var description = enumeratedType.ToString();
var enumType = typeof (T);
// Can't use type constraints on value types, so have to do check like this
if (enumType.BaseType != typeof (Enum))
throw new ArgumentException("T must be of type System.Enum");
var fieldInfo = enumeratedType.GetType().GetField(enumeratedType.ToString());
if (fieldInfo != null)
{
var attributes = fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
description = ((DescriptionAttribute) attributes[0]).Description;
}
}
return description;
}
///<summary>
///</summary>
///<param name="enums"></param>
///<typeparam name="T"></typeparam>
///<returns></returns>
///<exception cref="ArgumentException"></exception>
public static string GetEnumCollectionDescription<T>(Collection<T> enums)
{
var sb = new StringBuilder();
var enumType = typeof (T);
// Can't use type constraints on value types, so have to do check like this
if (enumType.BaseType != typeof (Enum))
throw new ArgumentException("T must be of type System.Enum");
foreach (var enumeratedType in enums)
{
sb.AppendLine(GetEnumDescription(enumeratedType));
}
return sb.ToString();
}
}
}
Enum To Array
#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace TextGlow.Control.Utilities
{
public static class EnumUtils
{
public static void sdf()
{
}
public static T[] EnumToArray
{
//get the enumeration type
Type et = typeof(T);
T defaultValue = default(T);
//get the public static fields (members of the enum)
FieldInfo[] fi = et.GetFields(BindingFlags.Static | BindingFlags.Public);
//create a new enum array
T[] values = new T[fi.Length];
//populate with the values
for (int i = 0; i < fi.Length; i++)
{
values[i] = (T)fi[i].GetValue(defaultValue);
}
//return the array
return values;
}
}
}
[/csharp]
Try to parse Enum
namespace System
{
public static class EnumExtensions
{
/// <summary>
/// Tries the parse.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="theEnum">The enum.</param>
/// <param name="valueToParse">The value to parse.</param>
/// <param name="returnValue">The return value.</param>
/// <returns></returns>
public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
{
returnValue = default(T);
int intEnumValue;
if (Int32.TryParse(valueToParse, out intEnumValue))
{
if (Enum.IsDefined(typeof (T), intEnumValue))
{
returnValue = (T) (object) intEnumValue;
return true;
}
}
return false;
}
}
}
Wrap an enumerable so that clients can't get to the underlying implementation via a down-case
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2007, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
using System;
using System.Collections;
using System.Collections.Generic;
namespace Wintellect.PowerCollections
{
/// <summary>
/// A holder class for various internal utility functions that need to be shared.
/// </summary>
internal static class Util
{
/// <summary>
/// Wrap an enumerable so that clients can't get to the underlying
/// implementation via a down-case
/// </summary>
/// <param name="wrapped">Enumerable to wrap.</param>
/// <returns>A wrapper around the enumerable.</returns>
public static IEnumerable<T> CreateEnumerableWrapper<T>(IEnumerable<T> wrapped)
{
return new WrapEnumerable<T>(wrapped);
}
/// <summary>
/// Wrap an enumerable so that clients can't get to the underlying
/// implementation via a down-cast.
/// </summary>
class WrapEnumerable<T> : IEnumerable<T>
{
IEnumerable<T> wrapped;
/// <summary>
/// Create the wrapper around an enumerable.
/// </summary>
/// <param name="wrapped">IEnumerable to wrap.</param>
public WrapEnumerable(IEnumerable<T> wrapped)
{
this.wrapped = wrapped;
}
public IEnumerator<T> GetEnumerator()
{
return wrapped.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)wrapped).GetEnumerator();
}
}
}
}