首页 文章 精选 留言 我的

精选列表

搜索[c/c ],共10000篇文章
优秀的个人博客,低调大师

C#获取枚举描述代码

public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚举对象</param> /// <returns>得到对应描述信息</returns> public String GetEnumDesc(Enum e) { FieldInfo EnumInfo = e.GetType().GetField(e.ToString()); if (EnumInfo == null) { return ""; } DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (EnumAttributes.Length > 0) { return EnumAttributes[0].Description; } return e.ToString(); } /// <summary> /// 将含有描述信息的枚举绑定到列表控件中 /// </summary> /// <param name="listControl"></param> /// <param name="enumType"></param> private Dictionary<string, string> BindDesEnumToListControl(System.Type enumType) { Dictionary<string, string> dic = new Dictionary<string, string>(); foreach (object enumValue in Enum.GetValues(enumType)) { Enum e = (Enum)enumValue; dic.Add(((int)enumValue).ToString(), GetEnumDesc(e)); } return dic; } } 还有一个是根据传入的枚举的数字索引直接获取 public static string GetEnumDesc<T>(Object obj) { obj = (T)obj; if (obj == null) throw new ArgumentNullException("参数不能为null"); if (!obj.GetType().IsEnum) throw new Exception("参数类型不正确"); FieldInfo fieldinfo = obj.GetType().GetField(obj.ToString()); string str = string.Empty; Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (objs != null && objs.Length != 0) { DescriptionAttribute des = (DescriptionAttribute)objs[0]; str = des.Description; } return str; } 本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/4846916.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

C#中函数的使用

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static int MaxValue(int[] intArray) { int maxVal = intArray[0]; for (int i = 1;i<intArray.Length;i++) { if (intArray[i] > maxVal) { maxVal = intArray[i]; } } return maxVal; } static void Main(string[] args) { int[] myArray = { 1, 3, 5, 8, 2, 10, 9, 100, 40 }; int maxVal = MaxValue(myArray); Console.WriteLine("最大值为:{0}",maxVal); Console.ReadKey(); } } } params传参 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static int MaxValue(params int[] intArray) { int maxVal = intArray[0]; for (int i = 1;i<intArray.Length;i++) { if (intArray[i] > maxVal) { maxVal = intArray[i]; } } return maxVal; } static void Main(string[] args) { int maxVal = MaxValue(1,3,4,100,200,3); Console.WriteLine("最大值为:{0}",maxVal); Console.ReadKey(); } } } out使用,类似于callback。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static int MaxValue(out int maxIndex,params int[] intArray) { int maxVal = intArray[0]; maxIndex = 0; for (int i = 1;i<intArray.Length;i++) { if (intArray[i] > maxVal) { maxVal = intArray[i]; maxIndex = i; } } return maxVal; } static void Main(string[] args) { int maxIndex; int maxVal = MaxValue(out maxIndex,1,3,4,100,200,3); Console.WriteLine("最大值为:{0}",maxVal); Console.WriteLine("索引为:{0}", maxIndex); Console.ReadKey(); } } } 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6728921.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C#中的类型转换

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static void Main(string[] args) { int val = 97 ; Console.WriteLine( "{0}" ,val); // 97 bool boolVal = Convert.ToBoolean(val); Console.WriteLine( "{0}" , boolVal); // true byte byteVal = Convert.ToByte(val); Console.WriteLine( "{0}" , byteVal); // 97 char charVal = Convert.ToChar(val); Console.WriteLine( "{0}" ,charVal); // a decimal decimalVal = Convert.ToDecimal(val); Console.WriteLine( "{0}" , decimalVal); // 97 double doubleVal = Convert.ToDouble(val); Console.WriteLine( "{0}" , doubleVal); // 97 short shortVal = Convert.ToInt16(val); Console.WriteLine( "{0}" , shortVal); // 97 int intVal = Convert.ToInt32(val); Console.WriteLine( "{0}" , intVal); // 97 long longVal = Convert.ToInt64(val); Console.WriteLine( "{0}" , longVal); // 97 sbyte sbyteVal = Convert.ToSByte(val); Console.WriteLine( "{0}" , sbyteVal); // 97 float floatVal = Convert.ToSingle(val); Console.WriteLine( "{0}" , floatVal); // 97 string stringVal = Convert.ToString(val); Console.WriteLine( "{0}" , stringVal); // 97 ushort ushortVal = Convert.ToUInt16(val); Console.WriteLine( "{0}" , ushortVal); // 97 uint uintVal = Convert.ToUInt32(val); Console.WriteLine( "{0}" , uintVal); // 97 ulong ulongVal = Convert.ToUInt64(val); Console.WriteLine( "{0}" , ulongVal); // 97 Console.ReadKey(); } } } 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6728339.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C#中数组的使用

1.for using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static void Main(string[] args) { string[] friendNames = { "张三","李四","王五" }; int i; Console.WriteLine("我有{0}个朋友!",friendNames.Length); for (i = 0;i<friendNames.Length;i++) { Console.WriteLine(friendNames[i]); } Console.ReadKey(); } } } 2.foreach using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static void Main(string[] args) { string[] friendNames = { "张三","李四","王五" }; Console.WriteLine("我有{0}个朋友!",friendNames.Length); foreach(string friendName in friendNames) { Console.WriteLine(friendName); } Console.ReadKey(); } } } 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6728651.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C#变量的作用域

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace Demo{ class Program { static string text; static void Main(string[] args) { int i; //string text=""; for (i=0;i<10;i++) { text = "Line" + Convert.ToString(i); Console.WriteLine("{0}",text); } Console.WriteLine("Last text output in loop:{0}",text); Console.ReadKey(); } } } 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6748003.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

c++ template学习总结3

和往常一样,先来看一段代码: #include <stdexcept> template <typename T, int MAXSIZE> class Stack { private: T elems[MAXSIZE]; // elements int numElems; // current number of elements public: Stack(); // constructor void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const { // return whether the stack is empty return numElems == 0; } bool full() const { // return whether the stack is full return numElems == MAXSIZE; } }; // constructor template <typename T, int MAXSIZE> Stack<T,MAXSIZE>::Stack () : numElems(0) // start with no elements { // nothing else to do } template <typename T, int MAXSIZE> void Stack<T,MAXSIZE>::push (T const& elem) { if (numElems == MAXSIZE) { throw std::out_of_range("Stack<>::push(): stack is full"); } elems[numElems] = elem; // append element ++numElems; // increment number of elements } template<typename T, int MAXSIZE> void Stack<T,MAXSIZE>::pop () { if (numElems <= 0) { throw std::out_of_range("Stack<>::pop(): empty stack"); } --numElems; // decrement number of elements } template <typename T, int MAXSIZE> T Stack<T,MAXSIZE>::top () const { if (numElems <= 0) { throw std::out_of_range("Stack<>::top(): empty stack"); } return elems[numElems-1]; // return last element } 接下来编写我们的测试函数: #include <iostream> #include <string> #include <cstdlib> #include "stack4.hpp" int main() { try { Stack<int,20> int20Stack; // stack of up to 20 ints Stack<int,40> int40Stack; // stack of up to 40 ints Stack<std::string,40> stringStack; // stack of up to 40 strings // manipulate stack of up to 20 ints int20Stack.push(7); std::cout << int20Stack.top() << std::endl; int20Stack.pop(); // manipulate stack of up to 40 strings stringStack.push("hello"); std::cout << stringStack.top() << std::endl; stringStack.pop(); stringStack.pop(); } catch (std::exception const& ex) { std::cerr << "Exception: " << ex.what() << std::endl; return EXIT_FAILURE; // exit program with ERROR status } } 大家要注意int20Stack和int40Stack是两个不同的类型,他们是不能进行硬是或者显示的转换的。也不能够相互赋值。 你还可以为函数模板定义非型别参数: template <typename T, int VAL> T addValue (T const& x) { return x + VAL; } 当我们想把【函数】和某种操作作为参数传递的时候,就非常有用,比如在使用STL时,你可能有下面的代码: std::transform (source.begin(), source.end(), // start and end of source dest.begin(), // start of destination addValue<int,5>); // operation 有时候我们为了使得编译器推导的时候简单一些,可以使用: std::transform (source.begin(), source.end(), // start and end of source dest.begin(), // start of destination (int(*)(int const&)) addValue<int,5>); // operation 但是我们要注意,非类型模板参数也有自己的局限,通常来说他们只能是常整数,包括枚举,或者指向外部链接的指针。如果我们用浮点数或者class-type objects作为非类型模板参数是错误的: template <double VAT> // ERROR: floating-point values are not double process (double v) // allowed as template parameters { return v * VAT; } template <std::string name> // ERROR: class-type objects are not class MyClass { // allowed as template parameters … }; 由于字串字面常熟是一种采用内部链接的物件,也就是说不通模组中的两个同值的字串字面常数其实是两个不同的东西,所以他们也不能用来作为模板参数: template <char const* name> class MyClass { … }; MyClass<"hello"> x; // ERROR: string literal "hello" not allowed 此外全局指针也是不行的: template <char const* name> class MyClass { … }; char const* s = "hello"; MyClass<s> x; // ERROR: s is pointer to object with internal linkage 但是下面的例子是可以的: template <char const* name> class MyClass { … }; extern char const s[] = "hello"; MyClass<s> x; // OK 在这个例子中,s是一个外部连接的部件。 ============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/08/2384762.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

c++ template归纳学习5

双重模板参数: 我们还是以前几篇中的Stack为例子i:代码如下: template <typename T, template <typename ELEM> class CONT = std::deque > class Stack { private: CONT<T> elems; // elements public: void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const { // return whether the stack is empty return elems.empty(); } }; 在这段代码中,我们的第二个模板参数修改为:template <typename ELEM> class CONT 和往常一样,你可以使用class替代typename,但是CONT必须是class。 template <typename T, template <class ELEM> class CONT = std::deque> // OK class Stack { … }; but the following is not: template <typename T, template <typename ELEM> typename CONT = std::deque> class Stack { // ERROR … }; 由于上面例子中的ELEM实际中没有用到,其实是可以省略的。 我们实现一个成员函数看看: template <typename T, template <typename> class CONT> void Stack<T,CONT>::push (T const& elem) { elems.push_back(elem); // append copy of passed elem } 但是如果试图使用上面的stack,编译器会说deque不符合CONT的要求,问题在于,std中的deque要求不只是一个参数,第二个参数是一个配置器,他虽然有预设值,但是当它被用来匹配CONT参数的时候,预设值被编译器忽略了。 对于这个问题,我们可以修改,使得CONT参数要求一个带两个参数的容器。 template <typename T, template <typename ELEM, typename ALLOC = std::allocator<ELEM> > class CONT = std::deque> class Stack { private: CONT<T> elems; // elements … }; 最终版本如下: // basics/stack8.hpp #ifndef STACK_HPP #define STACK_HPP #include <deque> #include <stdexcept> #include <allocator> template <typename T, template <typename ELEM, typename = std::allocator<ELEM> > class CONT = std::deque> class Stack { private: CONT<T> elems; // elements public: void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const { // return whether the stack is empty return elems.empty(); } // assign stack of elements of type T2 template<typename T2, template<typename ELEM2, typename = std::allocator<ELEM2> >class CONT2> Stack<T,CONT>& operator= (Stack<T2,CONT2> const&); }; template <typename T, template <typename,typename> class CONT> void Stack<T,CONT>::push (T const& elem) { elems.push_back(elem); // append copy of passed elem } template<typename T, template <typename,typename> class CONT> void Stack<T,CONT>::pop () { if (elems.empty()) { throw std::out_of_range("Stack<>::pop(): empty stack"); } elems.pop_back(); // remove last element } template <typename T, template <typename,typename> class CONT> T Stack<T,CONT>::top () const { if (elems.empty()) { throw std::out_of_range("Stack<>::top(): empty stack"); } return elems.back(); // return copy of last element } template <typename T, template <typename,typename> class CONT> template <typename T2, template <typename,typename> class CONT2> Stack<T,CONT>& Stack<T,CONT>::operator= (Stack<T2,CONT2> const& op2) { if ((void*)this == (void*)&op2) { // assignment to itself? return *this; } Stack<T2> tmp(op2); // create a copy of the assigned stack elems.clear(); // remove existing elements while (!tmp.empty()) { // copy all elements elems.push_front(tmp.top()); tmp.pop(); } return *this; } #endif // STACK_HPP 测试如下: // basics/stack8test.cpp #include <iostream> #include <string> #include <cstdlib> #include <vector> #include "stack8.hpp" int main() { try { Stack<int> intStack; // stack of ints Stack<float> floatStack; // stack of floats // manipulate int stack intStack.push(42); intStack.push(7); // manipulate float stack floatStack.push(7.7); // assign stacks of different type floatStack = intStack; // print float stack std::cout << floatStack.top() << std::endl; floatStack.pop(); std::cout << floatStack.top() << std::endl; floatStack.pop(); std::cout << floatStack.top() << std::endl; floatStack.pop(); } catch (std::exception const& ex) { std::cerr << "Exception: " << ex.what() << std::endl; } // stack for ints using a vector as an internal container Stack<int,std::vector> vStack; … vStack.push(42); vStack.push(7); std::cout << vStack.top() << std::endl; vStack.pop(); } 程序的输出为: 7 42 Exception: Stack<>::top(): empty stack 7 ============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/08/2384921.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册